Skip to content

Commit 6cf55f0

Browse files
Merge pull request #2028 from CodingTestStudy2/최원준
[최원준] Day15
2 parents 6444269 + bff2a75 commit 6cf55f0

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#
2+
3+
'''
4+
1. 아이디어 :
5+
백트래킹으로 모든 경우를 구한다.
6+
현재 숫자에 방문하지 않은 숫자를 더한다.
7+
8+
2. 시간복잡도 :
9+
약 O(10 ** 8)
10+
11+
3. 자료구조/알고리즘 :
12+
backtracking
13+
14+
'''
15+
class Solution:
16+
def countNumbersWithUniqueDigits(self, n: int) -> int:
17+
visited = set()
18+
19+
def backtrack(length, total_length):
20+
if length == total_length:
21+
return 1
22+
23+
total = 0
24+
25+
for i in range(10):
26+
if length == 0 and i ==0:
27+
continue # 0
28+
29+
if i in visited:
30+
continue
31+
32+
visited.add(i)
33+
total += backtrack(length+1, total_length)
34+
visited.remove(i)
35+
36+
return total
37+
38+
ans = 0
39+
for length in range(n+1):
40+
ans += backtrack(0, length)
41+
return ans
42+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#
2+
3+
'''
4+
1. 아이디어 :
5+
x에 도달하기 위해서는 x-1에서 1칸 점프 + x-2에서 1칸 점프이므로, f(x) = f(x-1) + f(x-2)
6+
길이 2의 배열을 활용하여 공간 절약
7+
8+
2. 시간복잡도 :
9+
O(n)
10+
11+
3. 자료구조/알고리즘 :
12+
dp
13+
14+
'''
15+
class Solution:
16+
def climbStairs(self, n: int) -> int:
17+
if n == 1:
18+
return n
19+
20+
ans = [1,2]
21+
for i in range(3, n+1):
22+
ans = [ans[1],ans[0] + ans[1]]
23+
return ans[1]
24+

0 commit comments

Comments
 (0)