Problem of The Day: Arranging Coins
Problem Statement
Brute Force Approach [Accepted]
class Solution:
def arrangeCoins(self, n: int) -> int:
i = 1
res = 0
while n >= 0:
n -= i
i += 1
res += 1
return res - 1
Approach 1: Binary Search
class Solution:
def arrangeCoins(self, n: int) -> int:
left, right = 0, n
while left <= right:
k = (right + left) // 2
curr = k * (k + 1) // 2
if curr == n:
return k
if n < curr:
right = k - 1
else:
left = k + 1
return right