Skip to content

Commit 554a5ed

Browse files
committed
Solved "Missing number" and "Word search"
1 parent 7d53495 commit 554a5ed

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

missing-number/KwonNayeon.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Constraints:
3+
1. n equals the length of array nums
4+
2. n is between 1 and 10^4 inclusive
5+
3. Each element nums[i] is between 0 and n inclusive
6+
4. All numbers in nums are unique (no duplicates)
7+
8+
Time Complexity: O(nlogn)
9+
- 정렬에 nlogn, 순회에 n이 필요하므로 전체적으로 O(nlogn)
10+
Space Complexity: O(1)
11+
- 추가 공간을 사용하지 않고 입력 배열만 사용
12+
13+
풀이 방법:
14+
1. 배열을 정렬하여 0부터 n까지 순서대로 있어야 할 위치에 없는 숫자를 찾음
15+
2. 인덱스와 해당 위치의 값을 비교하여 다르다면 그 인덱스가 missing number
16+
3. 모든 인덱스를 확인했는데도 없다면 n이 missing number
17+
"""
18+
19+
class Solution:
20+
def missingNumber(self, nums: List[int]) -> int:
21+
nums.sort()
22+
23+
for i in range(len(nums)):
24+
if nums[i] != i:
25+
return i
26+
27+
return len(nums)
28+

word-search/KwonNayeon.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
Constraints:
3+
1. m equals board length (number of rows)
4+
2. n equals board[i] length (number of columns)
5+
3. m and n are between 1 and 6 inclusive
6+
4. word length is between 1 and 15 inclusive
7+
5. board and word contain only lowercase and uppercase English letters
8+
9+
Time Complexity: O(N * 3^L)
10+
- N은 board의 모든 cell (m * n)
11+
- L은 word의 길이
12+
- 각 cell에서 시작하여 word의 각 글자마다 세방향으로 탐색 (이미 방문한 방향 제외)
13+
14+
Space Complexity: O(L)
15+
- L은 word의 길이로, 재귀 호출 스택의 깊이
16+
17+
To Do:
18+
- DFS와 백트래킹 개념 복습하기
19+
- 다른 스터디원분들의 답안 참조하여 다른 풀이방법 복습하기
20+
"""
21+
22+
class Solution:
23+
def exist(self, board: List[List[str]], word: str) -> bool:
24+
rows, cols = len(board), len(board[0])
25+
26+
def dfs(i, j, k):
27+
if k == len(word):
28+
return True
29+
30+
if (i < 0 or i >= rows or
31+
j < 0 or j >= cols or
32+
board[i][j] != word[k]):
33+
return False
34+
35+
temp = board[i][j]
36+
board[i][j] = '#'
37+
38+
result = (dfs(i+1, j, k+1) or
39+
dfs(i-1, j, k+1) or
40+
dfs(i, j+1, k+1) or
41+
dfs(i, j-1, k+1))
42+
43+
board[i][j] = temp
44+
return result
45+
46+
for i in range(rows):
47+
for j in range(cols):
48+
if board[i][j] == word[0]:
49+
if dfs(i, j, 0):
50+
return True
51+
return False

0 commit comments

Comments
 (0)