Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions leetcode3/이진희/357. Count Numbers with Unique Digits.java
Original file line number Diff line number Diff line change
@@ -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;

}
}
32 changes: 32 additions & 0 deletions leetcode3/이진희/70. Climbing Stairs.java
Original file line number Diff line number Diff line change
@@ -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];

}
}