Skip to content

Commit c63fd3c

Browse files
committed
add MaxProductOfThree
1 parent 46f942e commit c63fd3c

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
- [GenomicRangeQuery](#s_5_genomic_range_query)
2626
- [MinAvgTwoSlice](#s_5_min_avg_two_slice)
2727
- [CountDiv](#s_5_count_div)
28+
- [Lesson 6 - Sorting](#s_6)
29+
- [MaxProductOfThree](#s_6_max_product_of_three)
2830
- [Licence](#licence)
2931

3032
<a name="installation"></a>
@@ -239,6 +241,22 @@ https://github.com/talgat-ruby/codility-lessons-javascript/blob/master/lesson5_p
239241

240242
https://app.codility.com/demo/results/trainingP6XQJ7-KM7/
241243

244+
<a name="s_6"></a>
245+
246+
### Lesson 6 - Sorting
247+
248+
<a name="s_6_max_product_of_three"></a>
249+
250+
#### MaxProductOfThree
251+
252+
##### File
253+
254+
https://github.com/talgat-ruby/codility-lessons-javascript/blob/master/lesson6_sorting/MaxProductOfThree.js
255+
256+
##### Link to Report
257+
258+
https://app.codility.com/demo/results/trainingWJKGRA-8JV/
259+
242260
<a name="licence"></a>
243261

244262
## License
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function solution(A) {
2+
let max1 = -1001;
3+
let max2 = -1001;
4+
let max3 = -1001;
5+
let min1 = 1001;
6+
let min2 = 1001;
7+
8+
for (const a of A) {
9+
if (a > max1) {
10+
[max3, max2, max1] = [max2, max1, a];
11+
} else if (a > max2) {
12+
[max3, max2] = [max2, a];
13+
} else if (a > max3) {
14+
max3 = a;
15+
}
16+
17+
if (a < min1) {
18+
[min2, min1] = [min1, a];
19+
} else if (a < min2) {
20+
min2 = a;
21+
}
22+
}
23+
24+
const tot1 = max2 * max3;
25+
const tot2 = min1 * min2;
26+
27+
if (max1 >= 0 && tot2 > tot1) {
28+
return tot2 * max1;
29+
} else {
30+
return tot1 * max1;
31+
}
32+
}
33+
34+
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("./MaxProductOfThree");
2+
3+
describe("test MaxProductOfThree", () => {
4+
test("[-3, 1, 2, -2, 5, 6] -> 60", () => {
5+
expect(solution([-3, 1, 2, -2, 5, 6])).toBe(60);
6+
});
7+
});

0 commit comments

Comments
 (0)