|
| 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