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