-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbricks.js
More file actions
31 lines (16 loc) · 742 Bytes
/
Copy pathbricks.js
File metadata and controls
31 lines (16 loc) · 742 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// We need to redistribute bricks across N boxes so that each box contains exactly 10 bricks. We can only move bricks one step at a time to adjacent boxes. The goal is to determine the minimum number of moves required or return -1 if it's impossible.
function solution(A){
let N = A.length;
let bricksTotal = A.reduce((acc,cur)=>acc + cur, 0);
if(bricksTotal !== (N * 10)) return - 1;
let balance = 0;
let moves = 0;
for(let i = 0; i < N; i++){
balance += A[i] - 10;
moves += Math.abs(balance);
}
return moves;
}
console.log(solution([7, 15, 10, 8])); // Output: 7
console.log(solution([11, 10, 8, 12, 8, 10, 11])); // Output: 6
console.log(solution([7, 14, 10])); // Output: -1