英文:
Two Crystal Ball Problem O(sqrt(n)) solution not working in all cases
问题
以下是您要翻译的部分:
"这是两个水晶球问题:假设有两个水晶球,如果从足够高的地方掉下来就会破碎,以最优化的方式确定它们会破碎的确切位置。解决方案:为了理解这个问题,让我们假设水晶球如果从高度为8米的地方掉下来就会破碎。所以,如果我们从真实的米数掉下来,球就不会破碎(false)。如果从2米高度掉下来,同样球也不会破碎(false)。我们一直这样做。当从8米高度掉下时,球会破碎(true)。如果我们将所有的false和true列成一个数组,它看起来像下面这样:
[false, false, false, false, false, false, false, false ,false, false, false, true, true, true, true] –
所以你应该找到第一个true。我尝试使用前端主课程中的方法来解决它,但我发现在某些情况下它不起作用,例如,它给我-1,但如果我添加新的false,它会给我true布尔值的第一个位置,即31。有没有办法改进这个算法,谢谢。"
英文:
GHere is the Two Crystal Ball problem: Given two crystal balls that will break if dropped from high enough distance, determine the exact spot in which it will break in the most optimized way. Solution: To understand the problem, let us assume the crystal ball will break if dropped from a height of 8 meters. So, if we drop from true meter, the ball will not break(false). If dropped from height of 2 meter, again the ball will not break(false). We keep on doing it. When dropped from 8 meters, the ball will break(true). If we list all falses and trues in an array, it will look like below:
[false, false, false, false, false, false, false, false ,false, false, false, true, true, true, true] –
so you should find the first true I tried to solve it using the methode from front end master course but I founded it doesn't work in all the cases in that case as an exemple it's give me -1 but if i add new false it give me the first place of true boolean which is 31 is there any way to improve that algorithm and thank you
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function two_crystal_balls(breaks) {
const jmpAmount = Math.floor(Math.sqrt(breaks.length));
let i = jmpAmount
for (i < breaks.length; i += jmpAmount;) {
if (breaks[i]) {
break;
}
}
console.log(i, "i")
i -= jmpAmount
console.log(i, "i")
for (let j = 0; j < jmpAmount && i < breaks.length; j++, i++) {
if (breaks[i]) {
console.log(j, "i")
return i
}
}
return -1
}
console.log(two_crystal_balls([false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true, true, true, true, true, true, true, true, true]))
<!-- end snippet -->
答案1
得分: 0
Here is the translated code without the comments and additional content:
function two_crystal_balls(breaks) {
const jmpAmount = Math.floor(Math.sqrt(breaks.length));
let i = jmpAmount;
for (; i < breaks.length; i += jmpAmount) {
if (breaks[i]) {
break;
}
}
i -= jmpAmount;
for (let j = i; j < breaks.length; j++) {
if (breaks[j]) {
return j;
}
}
return -1;
}
console.log(two_crystal_balls([false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true, true, true, true, true, true, true, true, true]))
Please note that I've translated the code as requested, and it should now be free of comments and additional content.
英文:
You have three problems in your code.
-
The first loop is not correct. A
for
loop has the syntaxfor (initialization; condition; afterthought)
The loop
for (i < breaks.length; i += jmpAmount;) {
should be
for (; i < breaks.length; i += jmpAmount) {
-
The loop
for (let j = 0; j < jmpAmount && i < breaks.length; j++, i++) { if (breaks[i]) { console.log(j, "i") return i } }
should be
for (let j = i; j < breaks.length; j++) { if (breaks[j]) { console.log(j, "i"); return j; } }
-
j
contains the solution, noti
.
The fixed code:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function two_crystal_balls(breaks) {
const jmpAmount = Math.floor(Math.sqrt(breaks.length));
let i = jmpAmount;
for (; i < breaks.length; i += jmpAmount) {
if (breaks[i]) {
break;
}
}
console.log(i, "i");
i -= jmpAmount;
console.log(i, "i");
for (let j = i; j < breaks.length; j++) {
if (breaks[j]) {
console.log(j, "i");
return j;
}
}
return -1;
}
console.log(two_crystal_balls([false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true, true, true, true, true, true, true, true, true]))
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论