diff --git "a/leetcode3/\354\235\264\354\247\204\355\235\254/357. Count Numbers with Unique Digits.java" "b/leetcode3/\354\235\264\354\247\204\355\235\254/357. Count Numbers with Unique Digits.java" new file mode 100644 index 00000000..8cfbd3e0 --- /dev/null +++ "b/leetcode3/\354\235\264\354\247\204\355\235\254/357. Count Numbers with Unique Digits.java" @@ -0,0 +1,36 @@ +/* + +1. 아이디어 : 각 자리수를 순열로 구함. 이때 첫자리는 9이고 그 외 n-1자리는 !(n-1)으로 계산 + +2. 시간복잡도 : O(8 + N - 2) -> O(N) + +3. 자료구조/알고리즘 : 누적합 + + */ + +class Solution { + public int countNumbersWithUniqueDigits(int n) { + // 최대 1억 + // 모든 개수, 겹치지 않아야함 + + // n = 1 -> 0 ~ 9 (9) + // n = 2 -> 10 ~ 99 (9*9) + // n = 3 -> 100 ~ 999 (9*9*8) + // n = 4 -> 1000 ~ 9999 + + if(n == 0) return 1; + + int ans = 10; + + int[] sum = new int[9]; + sum[0] = 9; + for(int i=1; i<9; i++) sum[i] = sum[i-1]*(9-i); + + for(int i=2; i<=n; i++) { + ans+= 9*sum[i-2]; + } + + return ans; + + } +} \ No newline at end of file diff --git "a/leetcode3/\354\235\264\354\247\204\355\235\254/70. Climbing Stairs.java" "b/leetcode3/\354\235\264\354\247\204\355\235\254/70. Climbing Stairs.java" new file mode 100644 index 00000000..0ed54e34 --- /dev/null +++ "b/leetcode3/\354\235\264\354\247\204\355\235\254/70. Climbing Stairs.java" @@ -0,0 +1,32 @@ +/* + +1. 아이디어 : + n을 1부터 4까지 직접 경우의 수를 구한 후, 수학적 규칙을 찾아 계산 + +2. 시간복잡도 : O(N) + +3. 자료구조/알고리즘 : DP + + */ + +class Solution { + public int climbStairs(int n) { + + // 1 2 3 5 + // 22 1111 211 -> 5 + + if(n == 1) return 1; + if(n == 2) return 2; + + int[] dp = new int[n+1]; + dp[1] = 1; + dp[2] = 2; + + for(int i=3; i<=n; i++) { + dp[i] = dp[i-1] + dp[i-2]; + } + + return dp[n]; + + } +} \ No newline at end of file