File tree Expand file tree Collapse file tree 2 files changed +85
-0
lines changed Expand file tree Collapse file tree 2 files changed +85
-0
lines changed Original file line number Diff line number Diff line change
1
+ ## λ¬Έμ
2
+ You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.
3
+
4
+ Given n, find the total number of full staircase rows that can be formed.
5
+
6
+ n is a non-negative integer and fits within the range of a 32-bit signed integer.
7
+
8
+ ```
9
+ Example 1:
10
+
11
+ n = 5
12
+
13
+ The coins can form the following rows:
14
+ Β€
15
+ Β€ Β€
16
+ Β€ Β€
17
+
18
+ Because the 3rd row is incomplete, we return 2.
19
+ Example 2:
20
+
21
+ n = 8
22
+
23
+ The coins can form the following rows:
24
+ Β€
25
+ Β€ Β€
26
+ Β€ Β€ Β€
27
+ Β€ Β€
28
+
29
+ Because the 4th row is incomplete, we return 3.
30
+ ```
31
+
32
+ ### μ루μ
33
+ n μ΄λΌλ μκ° λ€μ΄μμ λ 1μ© μ¦κ°νλ μμ΄ ` a + (a+1) + (a+2) + ... + (a+k) <= n ` μμ λ§μ‘±νλ ` k ` λ₯Ό μ°Ύλ λ¬Έμ μλ€.
34
+ μ΄λ₯Ό μμμΌλ‘ νλ©΄ λλ€.
35
+
36
+ #### ν΄κ²° 1
37
+ 1μ© μ¦κ°νλ μμ΄μ ν©μ (κ³ λ±νμ λ λ€λ€ λ°°μ κ² μ§λ§...) ` k * (k+1) / 2 ` μ΄λκΉ μ΄ κ°μ΄ μ£Όμ΄μ§ κ° n μ μλκ² νλ k λ₯Ό μ°ΎμΌλ©΄ λλ€.
38
+ μ½λμμ μ΄ k κ°μ a μ΄κ³ k * (k+1) μ b λ‘ λκ³ while λ¬Έμ λλ Έλ€.
39
+
40
+ ``` python3
41
+ class Solution :
42
+ def arrangeCoins (self , n : int ) -> int :
43
+
44
+ a = 1
45
+ b = 2
46
+ while b <= (2 * n):
47
+ a += 1
48
+ b = (pow (a, 2 ) + a)
49
+
50
+ return a - 1
51
+ ```
52
+
53
+ #### ν΄κ²° 2
54
+ μ΄ μ½λλ λμκ°μ§λ§ κ²°κ΅ κ³΅μμ ` k * (k+1) / 2 <= 2 * n ` μ΄λ―λ‘ n μ μκ³ μμΌλκΉ kλ₯Ό λ³μλ‘ λκ³ μμ νμ΄μΈ μλ μλ€.
55
+ kλ₯Ό λμμΌλ‘ μμ μ κ³±μμ λ§λ€λ©΄ λλ€. μ΄ λ λ§μ‘±νλ k λ μ μμΈλ°, sqrt ν΄μ λμ¨ κ²°κ³Ό κ°μ float μ΄λ―λ‘ int λ‘ μΊμ€ν
ν΄μ€λ€.
56
+ (μΊμ€ν
ν΄μ£Όλ μ΄μ λ μμ μ½λμμ -1 νλ κ²κ³Ό κ°μ)
57
+
58
+ ``` python3
59
+ class Solution :
60
+ def arrangeCoins (self , n : int ) -> int :
61
+ return int (sqrt((2 * n) + 0.25 ) - 0.5 )
62
+ ```
63
+
64
+ μ΄ λ°©λ²μ κ²½μ° μν 곡μμ΄κΈ° λλ¬Έμ ν΄κ²° 1 λ³΄λ€ ν¨μ¬ λΉ λ₯΄λ€.
Original file line number Diff line number Diff line change
1
+ from math import sqrt
2
+
3
+
4
+ class Solution :
5
+ def arrangeCoinsWithWhile (self , n : int ) -> int :
6
+ a = 1
7
+ b = 2
8
+ while b <= (2 * n ):
9
+ a += 1
10
+ b = (pow (a , 2 ) + a )
11
+
12
+ return a - 1
13
+
14
+ def arrangeCoinsWithMath (self , n : int ) -> int :
15
+ return int (sqrt ((2 * n ) + 0.25 ) - 0.5 )
16
+
17
+
18
+ if __name__ == '__main__' :
19
+ sol = Solution ()
20
+ print (sol .arrangeCoinsWithMath (8 ))
21
+ print (sol .arrangeCoinsWithMath (5 ))
You canβt perform that action at this time.
0 commit comments