Skip to content

Commit 46f942e

Browse files
committed
add MinAvgTwoSlice
1 parent b7a204b commit 46f942e

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
- [Lesson 5 - Prefix Sums](#s_5)
2424
- [PassingCars](#s_5_passing_cars)
2525
- [GenomicRangeQuery](#s_5_genomic_range_query)
26+
- [MinAvgTwoSlice](#s_5_min_avg_two_slice)
2627
- [CountDiv](#s_5_count_div)
2728
- [Licence](#licence)
2829

@@ -214,6 +215,18 @@ https://github.com/talgat-ruby/codility-lessons-javascript/blob/master/lesson5_p
214215

215216
https://app.codility.com/demo/results/trainingNRWKMW-CSJ/
216217

218+
<a name="s_5_min_avg_two_slice"></a>
219+
220+
#### MinAvgTwoSlice
221+
222+
##### File
223+
224+
https://github.com/talgat-ruby/codility-lessons-javascript/blob/master/lesson5_prefix_sums/MinAvgTwoSlice.js
225+
226+
##### Link to Report
227+
228+
https://app.codility.com/demo/results/trainingH4MVH3-G94/
229+
217230
<a name="s_5_count_div"></a>
218231

219232
#### CountDiv
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function solution(A) {
2+
const len = A.length - 1;
3+
let i = 0;
4+
let minStart = 0;
5+
let minAvg = 10001;
6+
7+
while (i < len) {
8+
const sum1 = A[i] + A[i + 1];
9+
const avg1 = sum1 / 2;
10+
if (minAvg > avg1) {
11+
minAvg = avg1;
12+
minStart = i;
13+
}
14+
15+
if (Number.isInteger(A[i + 2])) {
16+
const avg2 = (sum1 + A[i + 2]) / 3;
17+
if (minAvg > avg2) {
18+
minAvg = avg2;
19+
minStart = i;
20+
}
21+
}
22+
i++;
23+
}
24+
return minStart;
25+
}
26+
27+
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("./MinAvgTwoSlice");
2+
3+
describe("test MinAvgTwoSlice", () => {
4+
test("[4, 2, 2, 5, 1, 5, 8] -> 1", () => {
5+
expect(solution([4, 2, 2, 5, 1, 5, 8])).toBe(1);
6+
});
7+
test("[5, 5, 11, 1] -> 0", () => {
8+
expect(solution([5, 5, 11, 1])).toBe(0);
9+
});
10+
});

0 commit comments

Comments
 (0)