Skip to content

Commit 6aa05c5

Browse files
author
Patakfalvi Krisztian Ext
committed
Added 7. lesson.
1 parent ab49a59 commit 6aa05c5

File tree

6 files changed

+107
-0
lines changed

6 files changed

+107
-0
lines changed

07-stacks-and-queues/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## [Lesson 7 - Stacks and Queues](https://app.codility.com/programmers/lessons/7-stacks_and_queues/)
2+
3+
- [**Brackets**](https://app.codility.com/programmers/lessons/7-stacks_and_queues/brackets/) - Determine whether a given string of parentheses (multiple types) is properly nested ([brackets.js](./brackets.js)) ([Codility Report](https://app.codility.com/demo/results/trainingRC3JDM-DZU/))
4+
5+
- [**Fish**](https://app.codility.com/programmers/lessons/7-stacks_and_queues/fish/) - N voracious fish are moving along a river. Calculate how many fish are alive ([fish.js](./fish.js)) ([Codility Report](https://app.codility.com/demo/results/training5PYZPV-R4D/))
6+
7+
- [**Nesting**](https://app.codility.com/programmers/lessons/7-stacks_and_queues/nesting/) - Determine whether a given string of parentheses (single type) is properly nested ([nesting.js](./nesting.js)) ([Codility Report](https://app.codility.com/demo/results/trainingSPCCTC-AUG/))
8+
9+
- [**StoneWall**](https://app.codility.com/programmers/lessons/7-stacks_and_queues/stone_wall/) - Cover "Manhattan skyline" using the minimum number of rectangles ([stoneWall.js](./stoneWall.js)) ([Codility Report](https://app.codility.com/demo/results/training6QQDU3-GW5/))

07-stacks-and-queues/brackets.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function solution(S) {
2+
const BRACKETS = { "(": ")", "{": "}", "[": "]" };
3+
const OPENINGS = Object.keys(BRACKETS);
4+
const stack = [];
5+
6+
for (let i = 0; i < S.length; i++) {
7+
const char = S[i];
8+
9+
if (OPENINGS.includes(char)) {
10+
stack.push(char);
11+
} else if (BRACKETS[stack.pop()] !== char) {
12+
return 0;
13+
}
14+
}
15+
16+
if (stack.length !== 0) {
17+
return 0;
18+
}
19+
20+
return 1;
21+
}

07-stacks-and-queues/fish.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function solution(A, B) {
2+
const N = A.length;
3+
const stack = [];
4+
5+
let count = 0;
6+
7+
for (let i = 0; i < N; i++) {
8+
if (B[i] === 1) {
9+
stack.push(A[i]);
10+
} else {
11+
if (stack.length === 0) {
12+
count++;
13+
} else {
14+
while (stack.length && stack[stack.length - 1] < A[i]) {
15+
stack.pop();
16+
}
17+
18+
if (stack.length === 0) {
19+
count++;
20+
}
21+
}
22+
}
23+
}
24+
25+
return count + stack.length;
26+
}

07-stacks-and-queues/nesting.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function solution(S) {
2+
const N = S.length;
3+
const stack = [];
4+
5+
for (let i = 0; i < N; i++) {
6+
if (S[i] === "(") {
7+
stack.push(")");
8+
} else {
9+
if (stack.length === 0) {
10+
return 0;
11+
} else {
12+
stack.pop();
13+
}
14+
}
15+
}
16+
17+
if (stack.length !== 0) {
18+
return 0;
19+
}
20+
21+
return 1;
22+
}

07-stacks-and-queues/stoneWall.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function solution(H) {
2+
const N = H.length;
3+
const stack = [];
4+
5+
let bricks = 0;
6+
7+
for (let i = 0; i < N; i++) {
8+
while (stack.length && stack[stack.length - 1] > H[i]) {
9+
stack.pop();
10+
}
11+
12+
if (stack.length === 0 || stack[stack.length - 1] < H[i]) {
13+
stack.push(H[i]);
14+
bricks++;
15+
}
16+
}
17+
18+
return bricks;
19+
}

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,13 @@ My JavaScript solutions to exercises and tests at [Codility](https://codility.co
4949
- [**NumberOfDiscIntersections**](https://app.codility.com/programmers/lessons/6-sorting/number_of_disc_intersections/) - Compute the number of intersections in a sequence of discs ([numberOfDiscIntersections.js](./06-sorting/numberOfDiscIntersections.js)) ([Codility Report](https://app.codility.com/demo/results/trainingVHCHHU-EDP/))
5050

5151
- [**Triangle**](https://app.codility.com/programmers/lessons/6-sorting/triangle/) - Determine whether a triangle can be built from a given set of edges ([triangle.js](./06-sorting/triangle.js)) ([Codility Report](https://app.codility.com/demo/results/training35MRMB-C9Q/))
52+
53+
## [Lesson 7 - Stacks and Queues](https://app.codility.com/programmers/lessons/7-stacks_and_queues/)
54+
55+
- [**Brackets**](https://app.codility.com/programmers/lessons/7-stacks_and_queues/brackets/) - Determine whether a given string of parentheses (multiple types) is properly nested ([brackets.js](./07-stacks-and-queues/brackets.js)) ([Codility Report](https://app.codility.com/demo/results/trainingRC3JDM-DZU/))
56+
57+
- [**Fish**](https://app.codility.com/programmers/lessons/7-stacks_and_queues/fish/) - N voracious fish are moving along a river. Calculate how many fish are alive ([fish.js](./07-stacks-and-queues/fish.js)) ([Codility Report](https://app.codility.com/demo/results/training5PYZPV-R4D/))
58+
59+
- [**Nesting**](https://app.codility.com/programmers/lessons/7-stacks_and_queues/nesting/) - Determine whether a given string of parentheses (single type) is properly nested ([nesting.js](./07-stacks-and-queues/nesting.js)) ([Codility Report](https://app.codility.com/demo/results/trainingSPCCTC-AUG/))
60+
61+
- [**StoneWall**](https://app.codility.com/programmers/lessons/7-stacks_and_queues/stone_wall/) - Cover "Manhattan skyline" using the minimum number of rectangles ([stoneWall.js](./07-stacks-and-queues/stoneWall.js)) ([Codility Report](https://app.codility.com/demo/results/training6QQDU3-GW5/))

0 commit comments

Comments
 (0)