Skip to content

Commit 273c397

Browse files
committed
Runtime: 135 ms (Top 34.29%) | Memory: 45.5 MB (Top 45.71%)
1 parent 8db0105 commit 273c397

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1+
// Runtime: 135 ms (Top 34.29%) | Memory: 45.5 MB (Top 45.71%)
12
var isPossible = function(target) {
23
let max=0
34
let index=-1
4-
5+
56
for(let i=target.length-1;i>=0;i--){
67
if(target[i]>max){
78
max=target[i]
89
index=i
910
}
1011
}
1112
if(max===1)return true // if max itself is 1 return true
12-
13+
1314
let total=0
1415
for(let i=0;i<target.length;i++){
1516
if(i!==index){
1617
total+=target[i]
1718
}
1819
}
19-
// If total=1,it means only one element was remaining apart from max and its value is 1 return true
20-
// eg target=[10,1] we started with [1,1] so next steps would be [2,1]->[3,1]->...[10,1] we can make sure it leads to target
21-
if(total===1)return true;
22-
// max should be greater than remaining nums sum OR if total is 0 it would lead to infinite loop( num%0 === NaN) so return false
20+
// If total=1,it means only one element was remaining apart from max and its value is 1 return true
21+
// eg target=[10,1] we started with [1,1] so next steps would be [2,1]->[3,1]->...[10,1] we can make sure it leads to target
22+
if(total===1)return true;
23+
// max should be greater than remaining nums sum OR if total is 0 it would lead to infinite loop( num%0 === NaN) so return false
2324
if(max<=total||total===0)return false;
2425
max=max%total;
2526
if(max<1)return false; // it should not be less than 1
2627
target[index]=max;
27-
28+
2829
return isPossible(target)
29-
};
30+
};

0 commit comments

Comments
 (0)