Skip to content

Commit 86ee400

Browse files
committed
Adding lesson 6 challenges
1 parent a039624 commit 86ee400

File tree

5 files changed

+251
-0
lines changed

5 files changed

+251
-0
lines changed

lessons/L6_NumberOfDiscIntersections.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ function solution(A) {
7272
while (openings[j] <= closings[i] && j < N) {
7373
j++
7474
}
75+
76+
// We count the number of openings which have not been closed yet
7577
result += j - i - 1
7678
}
7779

lessons/L7_Brackets.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
3+
A string S consisting of N characters is considered to be properly nested if any of the following conditions is true:
4+
- S is empty;
5+
- S has the form "(U)" or "[U]" or "{U}" where U is a properly nested string;
6+
- S has the form "VW" where V and W are properly nested strings.
7+
8+
For example, the string "{[()()]}" is properly nested but "([)()]" is not.
9+
10+
Write a function:
11+
function solution(S);
12+
13+
that, given a string S consisting of N characters, returns 1 if S is properly nested and 0 otherwise.
14+
15+
For example, given S = "{[()()]}", the function should return 1 and given S = "([)()]", the function should return 0, as explained above.
16+
17+
Write an efficient algorithm for the following assumptions:
18+
- N is an integer within the range [0..200,000];
19+
- string S is made only of the following characters: '(', '{', '[', ']', '}' and/or ')'.
20+
21+
Copyright 2009–2023 by Codility Limited. All Rights Reserved.
22+
Unauthorized copying, publication or disclosure prohibited.
23+
24+
*/
25+
26+
function solution(S) {
27+
const arrayS = [...S]
28+
29+
const opening = ['(', '{', '[']
30+
const closing = [']', '}', ')']
31+
32+
const map = {
33+
'(': ')',
34+
'[': ']',
35+
'{': '}',
36+
}
37+
38+
let result = 1
39+
const closingStack = []
40+
arrayS.forEach((s) => {
41+
if (opening.includes(s)) {
42+
closingStack.push(map[s])
43+
}
44+
if (closing.includes(s)) {
45+
if (closingStack.pop() != s) {
46+
result = 0
47+
}
48+
}
49+
})
50+
51+
if (closingStack.length > 0) {
52+
result = 0
53+
}
54+
55+
return result
56+
}

lessons/L7_Fish.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
3+
You are given two non-empty arrays A and B consisting of N integers.
4+
Arrays A and B represent N voracious fish in a river, ordered downstream along the flow of the river.
5+
6+
The fish are numbered from 0 to N − 1. If P and Q are two fish and P < Q, then fish P is initially upstream of fish Q.
7+
Initially, each fish has a unique position.
8+
9+
Fish number P is represented by A[P] and B[P]. Array A contains the sizes of the fish.
10+
All its elements are unique. Array B contains the directions of the fish. It contains only 0s and/or 1s, where:
11+
- 0 represents a fish flowing upstream,
12+
- 1 represents a fish flowing downstream.
13+
14+
If two fish move in opposite directions and there are no other (living) fish between them, they will eventually meet each other.
15+
Then only one fish can stay alive − the larger fish eats the smaller one.
16+
More precisely, we say that two fish P and Q meet each other when P < Q, B[P] = 1 and B[Q] = 0,
17+
and there are no living fish between them. After they meet:
18+
- If A[P] > A[Q] then P eats Q, and P will still be flowing downstream,
19+
- If A[Q] > A[P] then Q eats P, and Q will still be flowing upstream.
20+
21+
We assume that all the fish are flowing at the same speed. That is, fish moving in the same direction never meet. The goal is to calculate the number of fish that will stay alive.
22+
23+
For example, consider arrays A and B such that:
24+
A[0] = 4 B[0] = 0
25+
A[1] = 3 B[1] = 1
26+
A[2] = 2 B[2] = 0
27+
A[3] = 1 B[3] = 0
28+
A[4] = 5 B[4] = 0
29+
30+
Initially all the fish are alive and all except fish number 1 are moving upstream. Fish number 1 meets fish number 2 and eats it, then it meets fish number 3 and eats it too. Finally, it meets fish number 4 and is eaten by it. The remaining two fish, number 0 and 4, never meet and therefore stay alive.
31+
32+
Write a function:
33+
function solution(A, B);
34+
35+
that, given two non-empty arrays A and B consisting of N integers, returns the number of fish that will stay alive.
36+
For example, given the arrays shown above, the function should return 2, as explained above.
37+
38+
Write an efficient algorithm for the following assumptions:
39+
- N is an integer within the range [1..100,000];
40+
- each element of array A is an integer within the range [0..1,000,000,000];
41+
- each element of array B is an integer that can have one of the following values: 0, 1;
42+
- the elements of A are all distinct.
43+
44+
Copyright 2009–2023 by Codility Limited. All Rights Reserved.
45+
Unauthorized copying, publication or disclosure prohibited.
46+
47+
*/
48+
49+
// O(N)
50+
function solution(A, B) {
51+
const N = A.length
52+
53+
let result = 0
54+
const downStreamStack = []
55+
56+
for (let i = 0; i < N; i++) {
57+
result++
58+
59+
// If we find an downstream-going fish, we add it to the stack
60+
if (B[i]) {
61+
downStreamStack.push(A[i])
62+
}
63+
64+
// If we find an upstream-going fish we iterate over all previous downstream-going fish and battle them
65+
if (B[i] == 0) {
66+
let contestant = downStreamStack.pop()
67+
while (contestant && contestant < A[i]) {
68+
result--
69+
contestant = downStreamStack.pop()
70+
}
71+
if (contestant && contestant > A[i]) {
72+
result--
73+
downStreamStack.push(contestant)
74+
}
75+
}
76+
}
77+
78+
return result
79+
}

lessons/L7_Nesting.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
3+
A string S consisting of N characters is called properly nested if:
4+
- S is empty;
5+
- S has the form "(U)" where U is a properly nested string;
6+
- S has the form "VW" where V and W are properly nested strings.
7+
8+
For example, string "(()(())())" is properly nested but string "())" isn't.
9+
10+
Write a function:
11+
function solution(S);
12+
13+
that, given a string S consisting of N characters, returns 1 if string S is properly nested and 0 otherwise.
14+
15+
For example, given S = "(()(())())", the function should return 1 and given S = "())", the function should return 0, as explained above.
16+
17+
Write an efficient algorithm for the following assumptions:
18+
- N is an integer within the range [0..1,000,000];
19+
- string S is made only of the characters '(' and/or ')'.
20+
21+
Copyright 2009–2023 by Codility Limited. All Rights Reserved.
22+
Unauthorized copying, publication or disclosure prohibited.
23+
24+
*/
25+
26+
function solution(S) {
27+
const arrayS = [...S]
28+
29+
let debt = 0
30+
for (let i = 0; i < S.length; i++) {
31+
if (S[i] == '(') {
32+
debt++
33+
}
34+
if (S[i] == ')') {
35+
debt--
36+
}
37+
if (debt < 0) {
38+
return 0
39+
}
40+
}
41+
42+
return debt == 0 ? 1 : 0
43+
}

lessons/L7_StoneWall.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
3+
You are going to build a stone wall. The wall should be straight and N meters long, and its thickness should be constant;
4+
however, it should have different heights in different places.
5+
The height of the wall is specified by an array H of N positive integers.
6+
H[I] is the height of the wall from I to I+1 meters to the right of its left end.
7+
In particular, H[0] is the height of the wall's left end and H[N-1] is the height of the wall's right end.
8+
9+
The wall should be built of cuboid stone blocks (that is, all sides of such blocks are rectangular).
10+
Your task is to compute the minimum number of blocks needed to build the wall.
11+
12+
Write a function:
13+
function solution(H);
14+
15+
that, given an array H of N positive integers specifying the height of the wall,
16+
returns the minimum number of blocks needed to build it.
17+
18+
For example, given array H containing N = 9 integers:
19+
H[0] = 8 H[1] = 8 H[2] = 5
20+
H[3] = 7 H[4] = 9 H[5] = 8
21+
H[6] = 7 H[7] = 4 H[8] = 8
22+
23+
the function should return 7. The figure shows one possible arrangement of seven blocks.
24+
25+
Write an efficient algorithm for the following assumptions:
26+
- N is an integer within the range [1..100,000];
27+
- each element of array H is an integer within the range [1..1,000,000,000].
28+
29+
Copyright 2009–2023 by Codility Limited. All Rights Reserved.
30+
Unauthorized copying, publication or disclosure prohibited.
31+
32+
*/
33+
34+
// O(N)
35+
function solution(H) {
36+
let result = 0
37+
const segmentStack = []
38+
39+
H.forEach((h) => {
40+
const M = segmentStack.length
41+
42+
// if the stack is empty or if the current segment is higher than the previous one,
43+
// add the current segment to the stack and increment the counter
44+
if (M == 0 || h > segmentStack[M - 1]) {
45+
segmentStack.push(h)
46+
result++
47+
}
48+
49+
// If the current segment is lower than the previous one, pop the stack
50+
// until we find a value equal or lower than the current one
51+
if (h < segmentStack[M - 1]) {
52+
let previousSegment = segmentStack.pop()
53+
while (h < previousSegment) {
54+
previousSegment = segmentStack.pop()
55+
}
56+
57+
// After the previous step, if the previous segment is equal to the current one, the counter does not increase
58+
// however, if it's lower, we need to add back the previous segment, and increase the counter
59+
// (if the stack was empty, we simply increment the counter)
60+
if (previousSegment == null || previousSegment < h) {
61+
previousSegment && segmentStack.push(previousSegment)
62+
result++
63+
}
64+
65+
// And, regardless, we add the decreasing segment to the stack
66+
segmentStack.push(h)
67+
}
68+
})
69+
70+
return result
71+
}

0 commit comments

Comments
 (0)