-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
casino-chips.js
58 lines (47 loc) · 1.13 KB
/
casino-chips.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const compare = (a, b) => b - a;
function solve(arr){
// a place to store the number of days
// initialized to zero
let numDays = 0;
arr.sort(compare);
// while there are at least 2 stacks with at least 1 chip
while(arr[0] > 0 && arr[1] > 0) {
// increment number of days
numDays++;
// decrement the 2 highest stacks
arr[0]--;
arr[1]--;
arr.sort(compare);
}
return numDays;
}
function solve(arr){
let numDays = 0;
arr.sort(compare);
while(arr[0] > 0 && arr[1] > 0) {
numDays++;
arr[0]--;
arr[1]--;
arr.sort(compare);
}
return numDays;
}
// DOES NOT WORK!
function solve(arr) {
arr.sort(compare);
console.log(arr);
console.log(arr[1] - arr[0]);
return (arr[0] - arr[1]) + (arr[1] - arr[2]);
}
// Solution from code wars
function solve([a,b,c]) {
return Math.min(a+b,a+c,b+c,Math.floor((a+b+c)/2));
}
// console.log(solve([1,1,1]), 1);
// console.log(solve([2,1,1]), 2);
console.log(solve([4,1,1]), 2);
console.log(solve([8,2,8]), 9);
console.log(solve([8,1,4]), 5);
console.log(solve([7,4,10]), 10);
console.log(solve([12,12,12]), 18);
console.log(solve([1,23,2]), 3);