Problem Description:
You have a total of n coins, you need to put them in a ladder shape, the K -line must have exactly the k coin.
Given a number n, find the total number of rows that can form a complete ladder row.
N is a non-negative integer and is within the range of a 32-bit signed integer.
Example 1:
n = 5 coins can be arranged in the following lines: ¤¤¤¤¤ returns 2 because the third line is incomplete.
Example 2:
n = 8 coins can be arranged in the following lines: ¤¤¤¤¤¤¤¤ returns 3 because the row is incomplete.
Method 1:
1 classsolution (object):2 defarrangecoins (self, n):3 """4 : Type N:int5 : Rtype:int6 """ 7Low =08High =N9 Ten whilelow<=High : Onemid = Int ((low+high)//2) A - ifmid* (mid+1)/2 <= N < (mid+1) * (mid+2)/2: - returnMid the elifmid* (mid+1)/2 >N: -High = Mid-1 - elif(mid+1) * (mid+2)/2 <=N: -Low = mid + 1
Amazing:
1 class solution (object): 2 def arrangecoins (self, n): 3 return Int (((8*n + 1) **0.5-1)/2)
Ditto:
class solution (Object): def arrangecoins (self, N): """ : Type n:int : Rtype:int " " "= Int ((2*n+0.25) **0.5-0.5) return K
2018-10-03 21:33:05
leetcode--441--Arranging coins