首页 > 其他分享 >[Algorithm] Two crystal balls problem

[Algorithm] Two crystal balls problem

时间:2023-07-08 20:34:56浏览次数:49  
标签:balls point breaks crystal let problem find

You're given two identical crystal balls and a 100-story building. The balls are incredibly tough, but there exists some floor in the building, above which the balls will break when dropped, and below which they will not. You don't know what this floor is, but you want to find out with the fewest possible drops. How do you do it?

 

/**
 * Noramlly we would consider find middle point of the array
 * But if assume the first ball break, then we need to walk through
 * half of the array to find the breaking point.
 *
 * There fore the T: O(N/2) = O(N).
 *
 * We want better than that.
 *
 * So what we can do is we take step of sqrt(N) each time for the first ball
 * If it breaks, then we can walk through the sqrt(N) array to find the breaking point.
 *
 * O(sqrt(N)) < O(N)
 */
export default function two_crystal_balls(breaks: boolean[]): number {

    const jumpAmount = Math.floor(Math.sqrt(breaks.length))

    // using first ball to find the breaking point range
    let i = 0
    while (jumpAmount * i < breaks.length) {
        if (breaks[jumpAmount * i]) {
            break
        }
        i++
    }

    // using second ball to find actual breaking point
    let j = jumpAmount * (i - 1)
    for (let k =0; k < jumpAmount; k++) {
        if (breaks[j + k]) {
            return j + k
        }
    }

    return -1
}

 

Test:

test("two crystal balls", function () {
    let idx = Math.floor(Math.random() * 10000);
    const data = new Array(10000).fill(false);

    for (let i = idx; i < 10000; ++i) {
        data[i] = true;
    }

    expect(two_crystal_balls(data)).toEqual(idx);
    expect(two_crystal_balls(new Array(821).fill(false))).toEqual(-1);
});

 

标签:balls,point,breaks,crystal,let,problem,find
From: https://www.cnblogs.com/Answer1215/p/17537791.html

相关文章

  • 【cs50】lab6&problemset6
    (1)lab6worldcup#Simulateasportstournamentimportcsvimportsysimportrandom#NumberofsimluationstorunN=1000000#1000defmain():#Ensurecorrectusageiflen(sys.argv)!=2:sys.exit("Usage:pythontournament.......
  • HDOJ 5296 Annoying problem
    根据每次的加点删点求对答案的贡献。。。#include<iostream>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<cstdio>#include<algorithm>#include<cstring>#include<climits&g......
  • Could not fetch URL https://pypi.org/simple/keras-bert/: There was a problem co
    pip下载包的时候报错CouldnotfetchURLhttps://pypi.org/simple/keras-bert/:Therewasaproblemconfirmingthesslcertificate:HTTPSConnectionPool(host='pypi.org',port=443):Maxretriesexceededwithurl:/simple/keras-bert/(CausedbySSLError(SSLEO......
  • 【cs 50】lab4 & problemset4 -ing
    (1)lab4-Smileyhelpers.c#include"helpers.h"voidcolorize(intheight,intwidth,RGBTRIPLEimage[height][width]){//Changeallblackpixelstoacolorofyourchoosingfor(inti=0;i<height;++i){for(intj=0;j<width;++......
  • 【每日一题】Problem 414B. Mashmokh and ACM
    原题解决思路先计算\([1,n]\)中的约数集合\(dp[i][j](i\in[1,n],j\in[1,k])\)表示第\(j\)个数放置\(i\)所拥有的可能性以此类推,到达\(k\)时,计算\(\sum_{i=1}^{n}dp[i][k]\)即可#include<bits/stdc++.h>intmain(){intn,k;std::cin>>n>>k;......
  • 【哈佛cs50 2022】lab3 & problemSet3【ing】
    (1)lab3如何测试每个代码运行所需要的时间?time./sort1sorted5000.txt sort1sort2sort3sorted5000.txt0.037s0.020s0.045ssorted10000.txt0.058s0.050s0.151ssorted50000.txt1.244s2.238s5.637sreversed5000.txt0.088s0.026s0.045srever......
  • Constructive Problem
    ConstructiveProblemtimelimitpertest1secondmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputAsyouknow,anyproblemthatdoesnotrequiretheuseofcomplexdatastructuresisconsideredconstructive.Youare......
  • 【每日一题】Problem 489B. BerSU Ball
    原题解决思路排序+双指针#include<bits/stdc++.h>intmain(){intn;std::cin>>n;std::vector<int>a(n+1,0);for(inti=1;i<=n;++i)std::cin>>a[i];intm;std::cin>>m;std::vector<int>b(m+1,0);......
  • 【每日一题】Problem 443B. Kuriyama Mirai's Stones
    原题解决思路两个数组,一个未排序,一个排序;使用前缀和的方式减少时间复杂度#include<bits/stdc++.h>intmain(){std::ios::sync_with_stdio(false);std::cin.tie(nullptr);std::cout.tie(nullptr);intn;std::cin>>n;std::vector<int>v(n+1,0);f......
  • 【cs50 2022】lab1 && problem set1
    |lab1|#include<cs50.h>#include<stdio.h>intmain(void){//TODO:Promptforstartsizeintstart_size;do{start_size=get_int("Startsize:");}while(start_size<9);//TODO:Promptforend......