Created
March 15, 2022 07:33
-
-
Save riyafa/e2d60885d54708099e8b4ba3d95bb017 to your computer and use it in GitHub Desktop.
Binary search solution for 441. Arranging Coins: https://youtu.be/H-Tdu8qJ_uk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ArrangingCoinsBS { | |
public int arrangeCoins(int n) { | |
int low = 1; | |
int high = n; | |
while(low < high) { | |
int mid = low +(high -low+1)/2; | |
long num = (((long)mid * ((long)mid +1l))/2l); | |
if(num == n) { | |
return mid; | |
} else if(n < num){ | |
high = mid-1; | |
} else { | |
low = mid; | |
} | |
} | |
return low; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment