Skip to content

Commit e65b1b3

Browse files
committed
feat: climbing-stairs solutions
BigInteger Reculsive ํ’€์ด, pibonacci ํ’€์ด
1 parent d65f72f commit e65b1b3

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

โ€Žclimbing-stairs/john9803.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import java.math.BigInteger;
2+
3+
// 1, 2 ๊ฑธ์Œ์œผ๋กœ n๊ฐœ์˜ ๊ณ„๋‹จ์„ ์˜ฌ๋ผ๊ฐ€๋Š” ๋ฐฉ๋ฒ•์˜ ๊ฐ€์ง“์ˆ˜๋ฅผ ๊ณ„์‚ฐ
4+
5+
// ================================================================================
6+
// * ํ’€์ด 1 *
7+
// ================================================================================
8+
// n <= 45 ์ด๋ฏ€๋กœ ์ตœ๋Œ€ํ•œ 2๋กœ ๋‚˜๋ˆ ์„œ ๊ฐ€์ง“์ˆ˜ ๊ตฌํ•˜๊ธฐ
9+
// n์ด ์ปค์ง์— ๋”ฐ๋ผ์„œ ๊ณ„์‚ฐ์˜ ๋ฒ”์œ„๊ฐ€ ๊ต‰์žฅํžˆ ๋Š˜์–ด๋‚จ.
10+
// ๊ณ„์‚ฐ๋ฒ”์œ„ ์ดˆ๊ณผ๋กœ ํ’€์ด ์‹คํŒจ.
11+
12+
// ================================================================================
13+
// * ํ’€์ด 2 *
14+
// ================================================================================
15+
// ํ’€์ด 1์„ ๋ณด์™„ํ•œ ํ’€์ด
16+
// ๋‹จ์ˆœํžˆ ์กฐํ•ฉ ๊ตฌํ˜„๋ง๊ณ  ๋‹ค๋ฅธ ํ•ด๊ฒฐ๋ฐฉ์•ˆ์ด ํ•„์š”ํ•จ -> BigInteger๋กœ ๊ณ„์‚ฐ๋ฒ”์œ„ ๋Š˜๋ฆผ.
17+
// ํ’€์ด์„ฑ๊ณต -> ๋‹ค๋งŒ ๋Ÿฐํƒ€์ž„ ์‹œ๊ฐ„์ด ํ‰๊ท ํ’€์ด๋ณด๋‹ค ๋„ˆ๋ฌด ๊ธธ๊ณ , ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ์ด ๋†’์•„์„œ ์ƒˆ๋กœ์šด ํ’€์ด ์ƒ๊ฐํ•ด๋ด„.
18+
19+
// ================================================================================
20+
// * ํ’€์ด 3 *
21+
// ================================================================================
22+
// ๋” ๋น ๋ฅธ ํ’€์ด๋ฅผ ์œ„ํ•ด์„œ ์ฐพ์•„๋ณด๋˜ ์ค‘. n์ด ์ปค์ง์— ๋”ฐ๋ผ์„œ ๋‚˜์˜ค๋Š” ๊ฐ’๋“ค์˜ ๊ทœ์น™์ด ํ”ผ๋ณด๋‚˜์น˜ ๋ฐฐ์—ด์ž„์„ ๋ฐœ๊ฒฌ.
23+
// f(n) = f(n-1) + f(n-2) ์ž„์„ ์ด์šฉํ•ด์„œ ๋น ๋ฅด๊ฒŒ ํ’€์ด.
24+
// ์‹œ๊ฐ„๋ณต์žก๋„ = O(n)
25+
26+
class john9803 {
27+
public int climbStairs(int n) {
28+
// return firstApproch(n);
29+
// return bigIntSolve(n);
30+
return piboSolve(n);
31+
}
32+
33+
public int firstApproch(int n){
34+
int result = 0;
35+
// 2๊ฑธ์Œ ์ตœ๋Œ€ํ•œ ๋“ค์–ด๊ฐ€๊ณ  ๋‚จ์€๋ฐ 1๋กœ ์ฑ„์›Œ๋„ฃ๋Š” ๊ฑธ๋กœ ๊ฐ€์ง“์ˆ˜ ์„ธ๊ธฐ
36+
for(int step=0; 2*step<=n; step++){
37+
// 2๊ฑธ์Œ๊ณผ 1์€ ์ˆœ์„œ๋ฅผ ๊ฐ€์ง
38+
int totalNumCnt = step + (n-(2*step));
39+
// (totalNumCnt)C(step) -> Combination ์„ ๊ณ„์‚ฐํ•ด์„œ result์— ๋”ํ•จ
40+
int top = 1;
41+
int bottom = 1;
42+
// ์ฒ˜์Œ์—๋Š” ๋ถ„์ž๋ถ„๋ชจ๋ฅผ int๋กœ ๋†“์•˜๋‹ค๊ฐ€ ๋ถ„์ž๊ณ„์‚ฐ์—์„œ int๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚จ.
43+
for(int c =0; c< step; c++){
44+
top *= (totalNumCnt-c);
45+
bottom *= (step-c);
46+
}
47+
result = result + (int)(top/bottom);
48+
// System.out.println("top: "+ top + " bottom: " + bottom + " step is: " + step +" result is: "+ result);
49+
}
50+
return result;
51+
}
52+
53+
54+
// ๋‹จ์ˆœํ•œ ์žฌ๊ท€๋ฅผ ํ†ตํ•œ ๋ฌธ์ œํ’€์ด
55+
// ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒผ๋˜ ๋ถ€๋ถ„ n์ด ์ปค์ง์— ๋”ฐ๋ผ์„œ ๊ณ„์‚ฐ๋ฒ”์œ„๊ฐ€ ์ปค์กŒ์Œ.
56+
// ๋”ฐ๋ผ์„œ ๊ฒฐ๊ณผ์ ์œผ๋กœ ๊ณ„์‚ฐ์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ๋” ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํฌ๊ฒŒ ๋ถ€์—ฌํ•˜๋Š” BigInteger๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์ด
57+
public int bigIntSolve(int n){
58+
int result = 0;
59+
60+
for(int step=0; 2*step<=n; step++){
61+
int totalNumCnt = step + (n-(2*step));
62+
// (totalNumCnt)C(step) -> Combination ์„ ๊ณ„์‚ฐํ•ด์„œ result์— ๋”ํ•จ
63+
BigInteger top = new BigInteger("1");
64+
BigInteger bottom = new BigInteger("1");
65+
66+
for(int c =0; c< step; c++){
67+
top = top.multiply(new BigInteger(String.valueOf(totalNumCnt-c)));
68+
bottom = bottom.multiply(new BigInteger(String.valueOf(step-c)));
69+
}
70+
result += (top.divide(bottom)).intValue();
71+
// System.out.println("top: "+ top + " bottom: " + bottom + " step is: " + step +" result is: "+ result);
72+
}
73+
return result;
74+
}
75+
76+
// n์ด ์ปค์ง์— ๋”ฐ๋ผ์„œ ๊ฐ’์ด ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด์˜ ๊ทœ์น™์„ฑ์„ ์ง€๋‹ˆ๋Š” ๊ฒƒ์„ ํŒŒ์•…ํ•จ.
77+
// ํ’€์ด๋ฅผ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด์„ ์ด์šฉํ•ด ํ’€์ด ํ•˜๋„๋ก ๊ตฌํ˜„
78+
// f(n) = f(n-1) + f(n-2) ์ž„์„ ์ด์šฉ.
79+
public int piboSolve(int n){
80+
int prev = 1;
81+
int curr = 1;
82+
83+
for(int i=2; i<=n; i++){
84+
int temp = prev+curr;
85+
prev = curr;
86+
curr = temp;
87+
}
88+
return curr;
89+
}
90+
91+
}

0 commit comments

Comments
ย (0)