Your browser does not seem to support JavaScript. As a result, your viewing experience will be diminished, and you have been placed in read-only mode.

Please download a browser that supports JavaScript, or enable it if it's disabled (i.e. NoScript).

Arranging Coins

N

G

I got the same idea at the first look. Glad to see someone has posted that.

Z

@sujiths52 Use -b+sqrt(b^2 - 4ac)/2*a is correct too.

I just found my solution intuition.

S

python 1 line accepted solution:

No one has replied

I think we can skip the floor function because the int cast will do

Why do we need the + 1 here, without +1, we will get Time Exceed Limit. Can anyone help me understand the corner case about that? I assume there is something related to the stopping condition while (low < high)

@jasonshieh said in C++_9ms_Accepted:

class Solution { public: int arrangeCoins(int n) { if(n <= 1) return n; long left = 0, right = n; while(left <= right){ long mid = left + (right - left)/2; long sum = ((1 + mid) * mid)/2; if(sum <= n){left = mid + 1;} else {right = mid - 1;} } return left - 1; } };

C

Best code world！

T

@zebra1831 to prevent integer overflow. left + right could overflow, whereas right - left couldn't.

@Johnvon you are right, it's a mistake. Thanks for your replication.

Disabled Categories are greyed out

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.