You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I think with only one counter (i) it will be more easy to get the logic, and less memory used.
Looks like works in the same way...
export default function two_crystal_balls(breaks: boolean[]): number {
const jump = Math.floor(Math.sqrt(breaks.length));
let i = 0;
for (; i < breaks.length; i += jump) {
if (breaks[i]) break;
}
for (i -= jump + 1; i < breaks.length; i++ ){
if (breaks[i]) return i;
}
return -1;
}
The text was updated successfully, but these errors were encountered:
Hi @demakoff,
I really like your simple solution, it looks nice and clear.
I think there are two points to be fixed, though.
unexpected result of i -= jump + 1;
leti=4,constjump=2i-=jump+1;console.log(i)// might expect 3, but actually got 1.
In case breaks = [true], the program will return -1;
If above issue is fixed, the program will return -1 in case the breaks array has only one value, which is true.
So I think.
export default function two_crystal_balls(breaks: boolean[]): number {
const jump = Math.floor(Math.sqrt(breaks.length));
- let i = 0;+ let i = jump;
for (; i < breaks.length; i += jump) {
if (breaks[i]) break;
}
+ i -= jump;+ for (; i < breaks.length; i++ ){- for (i -= jump + 1; i < breaks.length; i++ ){
if (breaks[i]) return i;
}
return -1;
}
Or stick to initializing with zero,
export default function two_crystal_balls(breaks: boolean[]): number {
const jump = Math.floor(Math.sqrt(breaks.length));
let i = 0;
for (; i < breaks.length; i += jump) {
if (breaks[i]) break;
}
+ for (i &&= i - jump + 1; i < breaks.length; i++ ){- for (i -= jump + 1; i < breaks.length; i++ ){
if (breaks[i]) return i;
}
return -1;
}
I think with only one counter (i) it will be more easy to get the logic, and less memory used.
Looks like works in the same way...
The text was updated successfully, but these errors were encountered: