Skip to content

Commit 9fee776

Browse files
committed
add Fish
1 parent 2a9ce4d commit 9fee776

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
- ~~NumberOfDiscIntersections~~
3333
- [Lesson 7 - Stacks and Queues](#s_7)
3434
- [Brackets](#s_7_brackets)
35-
- ~~Fish~~
35+
- [Fish](#s_7_fish)
3636
- [Nesting](#s_7_nesting)
3737
- [Licence](#licence)
3838

@@ -304,6 +304,18 @@ https://github.com/talgat-ruby/codility-lessons-javascript/blob/master/lesson7_s
304304

305305
https://app.codility.com/demo/results/trainingDYK3SU-2EQ/
306306

307+
<a name="s_7_fish"></a>
308+
309+
#### Fish
310+
311+
##### File
312+
313+
https://github.com/talgat-ruby/codility-lessons-javascript/blob/master/lesson7_stacks_and_queues/Fish.js
314+
315+
##### Link to Report
316+
317+
https://app.codility.com/demo/results/trainingZRRPA4-NVP/
318+
307319
<a name="s_7_nesting"></a>
308320

309321
#### Nesting

lesson7_stacks_and_queues/Fish.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function solution(A, B) {
2+
const len = A.length;
3+
const down = []; // B[i] === 1
4+
let counter = 0;
5+
for (let i = 0; i < len; i++) {
6+
const size = A[i];
7+
const direction = B[i];
8+
if (direction === 1) {
9+
down.push(size);
10+
} else {
11+
while (down.length > 0 && size > down[down.length - 1]) {
12+
down.pop();
13+
}
14+
if (down.length === 0) {
15+
counter++;
16+
}
17+
}
18+
}
19+
return counter + down.length;
20+
}
21+
22+
module.exports = solution;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const solution = require("./Fish");
2+
3+
describe("test Fish", () => {
4+
test("([4, 3, 2, 1, 5], [0, 1, 0, 0, 0]) -> 2", () => {
5+
expect(solution([4, 3, 2, 1, 5], [0, 1, 0, 0, 0])).toBe(2);
6+
});
7+
});

0 commit comments

Comments
 (0)