Skip to content

Commit b21f0ec

Browse files
committed
add StoneWall
1 parent 9fee776 commit b21f0ec

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
- [Brackets](#s_7_brackets)
3535
- [Fish](#s_7_fish)
3636
- [Nesting](#s_7_nesting)
37+
- [StoneWall](#s_7_stone_wall)
3738
- [Licence](#licence)
3839

3940
<a name="installation"></a>
@@ -328,6 +329,18 @@ https://github.com/talgat-ruby/codility-lessons-javascript/blob/master/lesson7_s
328329

329330
https://app.codility.com/demo/results/trainingG7NMTC-FKF/
330331

332+
<a name="s_7_stone_wall"></a>
333+
334+
#### StoneWall
335+
336+
##### File
337+
338+
https://github.com/talgat-ruby/codility-lessons-javascript/blob/master/lesson7_stacks_and_queues/StoneWall.js
339+
340+
##### Link to Report
341+
342+
https://app.codility.com/demo/results/trainingDRUBXQ-4DF/
343+
331344
<a name="licence"></a>
332345

333346
## License
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function solution(H) {
2+
let counter = 1;
3+
let set = new Set([H[0]]);
4+
let min = null;
5+
6+
for (let i = 1; i < H.length; i++) {
7+
const h = H[i];
8+
if (H[i - 1] < h) {
9+
if (min !== null) {
10+
const arr = [...set];
11+
const ind = arr.findIndex(a => a > min);
12+
set = new Set([...arr.slice(0, ind), min]);
13+
min = null;
14+
}
15+
set.add(h);
16+
counter++;
17+
} else if (H[i - 1] > h) {
18+
min = h;
19+
if (!set.has(h)) {
20+
counter++;
21+
}
22+
}
23+
}
24+
25+
return counter;
26+
}
27+
28+
module.exports = solution;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const solution = require("./StoneWall");
2+
3+
describe("test StoneWall", () => {
4+
test("[8, 8, 5, 7, 9, 8, 7, 4, 8] -> 7", () => {
5+
expect(solution([8, 8, 5, 7, 9, 8, 7, 4, 8])).toBe(7);
6+
});
7+
test("[2, 5, 1, 4, 6, 7, 9, 10, 1] -> 8", () => {
8+
expect(solution([2, 5, 1, 4, 6, 7, 9, 10, 1])).toBe(8);
9+
});
10+
});

0 commit comments

Comments
 (0)