-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #520 from obzva/main
[Flynn] Week 09
- Loading branch information
Showing
5 changed files
with
331 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
ํ์ด 1 | ||
- ์ฃผ์ด์ง ๋ฐฐ์ด์ ๋ ๋ถ๋ถ์ผ๋ก ๋๋ ์ ์๊ธฐ ๋๋ฌธ์ ์ด์งํ์์ ์ด์ฉํ์ฌ ํ์ดํ ์ ์์ต๋๋ค | ||
์ฃผ์ด์ง ๋ฐฐ์ด์ด A = [4,5,6,7,0,1,2] ๋ผ๊ณ ํ ๋, ์ด ๋ฐฐ์ด์ ๋ ๋ถ๋ถ์ผ๋ก ๋๋ ์ ์์ต๋๋ค | ||
A[0:3] : rotate๋์ด ์์ผ๋ก ๋ฐฐ์ด์ ์์ผ๋ก ์ฎ๊ฒจ์ง ๋ถ๋ถ | ||
A[4:6] : rotate๋์ด ๋ฐฐ์ด์ ๋ค๋ก ๋ฐ๋ ค๋ ๋ถ๋ถ | ||
์ด๊ฑธ ์กฐ๊ธ ๋ค๋ฅด๊ฒ ํํํ๋ฉด ์ด๋ ๊ฒ๋ ๋ฐ๋ผ๋ณผ ์ ์์ต๋๋ค | ||
f(A) = [T, T, T, T, F, F, F] (T/F: rotate๋์ด ๋ฐฐ์ด์ ์์ผ๋ก ์ฎ๊ฒจ์ง ๋ถ๋ถ์ธ๊ฐ?) | ||
์ด ๋, ์ฐ๋ฆฌ๊ฐ ์ฐพ๋ ๊ฐ (the Minimum in the rotated sorted array)๋ ๋ ๊ตฌ๊ฐ์ ๊ฒฝ๊ณ์ ์์นํ๊ณ ์์ต๋๋ค | ||
์ฆ, ์ฒซ๋ฒ์งธ F์ ์์น๋ฅผ ์ฐพ๋ ๋ฌธ์ ๋ก ๋ฐ๊ฟ ์๊ฐํ ์ ์๋จ ๋ป์ ๋๋ค | ||
Big O | ||
- N: ์ฃผ์ด์ง ๋ฐฐ์ด nums์ ๊ธธ์ด | ||
- Time complexity: O(logN) | ||
- Space complexity: O(1) | ||
*/ | ||
|
||
func findMin(nums []int) int { | ||
lo, hi := 0, len(nums)-1 | ||
// rotate๊ฐ 0๋ฒ ์คํ๋ ๊ฒฝ์ฐ, ์ด์งํ์์ ์คํํ ํ์๊ฐ ์๊ณ ์ด์งํ์์ ์ด๊ธฐ๊ฐ ์ค์ ์ด ๊น๋ค๋ก์์ง๊ธฐ ๋๋ฌธ์ ์ฒ์์ ๋ฐ๋ก ์ฒ๋ฆฌํด์ค๋๋ค | ||
// ์์ hi์ ์ด๊ธฐ๊ฐ์ ์ค์ ํ ๋, len(nums)๊ฐ ์๋ len(nums) - 1๋ก ์ค์ ํ ์ ์์๋ ์ด์ ์ด๊ธฐ๋ ํฉ๋๋ค | ||
if nums[lo] < nums[hi] { | ||
return nums[lo] | ||
} | ||
|
||
// Go๋ while๋ฌธ์ ๋์ํ๋ ํํ์ for๋ก ์ด๋ ๊ฒ ํํํฉ๋๋ค | ||
for lo < hi { | ||
mid := lo + (hi-lo)/2 | ||
|
||
// ๋ง์ผ mid๊ฐ ์์ผ๋ก ๋ฐ๋ ค๋ ๋ถ๋ถ์ ์ํ๋ค๋ฉด... | ||
if nums[lo] <= nums[mid] && nums[mid] > nums[hi] { | ||
lo = mid + 1 | ||
} else { | ||
hi = mid | ||
} | ||
} | ||
|
||
return nums[lo] | ||
} | ||
|
||
/* | ||
ํ์ด 2 | ||
- ํ์ด 1์์ ๋์ด๋ผ ์ ์๋ ๋ก์ง์ ๋์ด๋ธ ์ข ๋ ๊น๋ํ ์ด์งํ์ ํ์ด์ ๋๋ค | ||
Big O | ||
- ํ์ด 1๊ณผ ๋์ผ | ||
*/ | ||
|
||
func findMin(nums []int) int { | ||
lo, hi := 0, len(nums) | ||
|
||
for lo < hi { | ||
mid := lo+(hi-lo)/2 | ||
|
||
if nums[mid] > nums[len(nums)-1] { | ||
lo = mid + 1 | ||
} else { | ||
hi = mid | ||
} | ||
} | ||
|
||
return nums[lo] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* ํ์ด | ||
* - Floyd's Tortoise and Hare Algorithm์ ์ด์ฉํ ํ์ด์ ๋๋ค. | ||
* ์ฐธ๊ณ : https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoise_and_hare | ||
* | ||
* Big O | ||
* - N: ๋ ธ๋์ ๊ฐ์ | ||
* - L: ๋ฃจํ ๊ตฌ๊ฐ์ ์ํ๋ ๋ ธ๋์ ๊ฐ์ | ||
* | ||
* Time Complexity: O(N) | ||
* - ๋ฃจํ๊ฐ ์๋ ๊ฒฝ์ฐ: | ||
* - fast ํฌ์ธํฐ๊ฐ ๋งํฌ๋๋ฆฌ์คํธ์ ๋๊น์ง ์ด๋ํ๋ฉด ์ข ๋ฃํฉ๋๋ค. | ||
* - ์ด ๋ fast ํฌ์ธํฐ์ ํ์ ์๊ฐ ๋ณต์ก๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค: | ||
* O(N / 2) = O(N) | ||
* - ๋ฃจํ๊ฐ ์๋ ๊ฒฝ์ฐ: | ||
* - slow ํฌ์ธํฐ์ fast ํฌ์ธํฐ๊ฐ ๋ฃจํ ์์์ ๋ง๋๋ฉด ์ข ๋ฃํฉ๋๋ค. | ||
* - ์ด ๋ slow ํฌ์ธํฐ์ ํ์ ์๊ฐ ๋ณต์ก๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค: | ||
* O((N - L) + L * c) (c๋ slow๊ฐ fast๋ฅผ ๋ง๋ ๋๊น์ง ๋ฃจํ๋ฅผ ๋ฐ๋ณตํ ํ์) | ||
* = O(r + (N - r) * c) (L์ 0 <= r <= N์ธ r์ ๋ํด N - r๋ก ํํํ ์ ์์ต๋๋ค) | ||
* = O(N) | ||
* | ||
* Space Complexity: O(1) | ||
* - ๋ ธ๋์ ๊ฐ์์ ์๊ด์์ด ์ผ์ ํ ๊ณต๊ฐ์ ์ฌ์ฉํฉ๋๋ค. | ||
*/ | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* type ListNode struct { | ||
* Val int | ||
* Next *ListNode | ||
* } | ||
*/ | ||
func hasCycle(head *ListNode) bool { | ||
if head == nil { | ||
return false | ||
} | ||
|
||
slow := head | ||
fast := head | ||
|
||
for fast != nil && fast.Next != nil { | ||
slow = slow.Next | ||
fast = fast.Next.Next | ||
|
||
if slow == fast { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
ํ์ด 1 | ||
- ์๋์ ๊ฐ์ memo ๋ฐฐ์ด์ ๋ง๋ค์ด์ ํ์ดํ ์ ์์ต๋๋ค (์ฐธ๊ณ : Kadane's Algorithm) | ||
memo[i] = nums[:i] ์ค์์ nums[i]๋ฅผ ๋ฐ๋์ ํฌํจํ๋ ๋ถ๋ถ ๋ฐฐ์ด์ ์ต๋ ํฉ | ||
Big O | ||
- N: ์ฃผ์ด์ง ๋ฐฐ์ด nums์ ๊ธธ์ด | ||
- Time complexity: O(N) | ||
- Space complexity: O(N) | ||
*/ | ||
|
||
func maxSubArray(nums []int) int { | ||
n := len(nums) | ||
|
||
memo := make([]int, n) | ||
copy(memo, nums) | ||
|
||
maxSum := nums[0] | ||
|
||
for i := 1; i < n; i++ { | ||
if memo[i-1] > 0 { | ||
memo[i] += memo[i-1] | ||
} | ||
|
||
if maxSum < memo[i] { | ||
maxSum = memo[i] | ||
} | ||
} | ||
|
||
return maxSum | ||
} | ||
|
||
/* | ||
ํ์ด 2 | ||
- ์๊ณ ๋ฆฌ์ฆ์ ์ ๊ฐ ๊ณผ์ ์ ๋ณด๋ฉด O(N)์ ๊ณต๊ฐ๋ณต์ก๋๋ฅผ ๊ฐ๋ memo๊ฐ ํ์ํ์ง ์๋ค๋ ๊ฑธ ์ ์ ์์ต๋๋ค | ||
- memo ๋ฐฐ์ด ๋์ ํ์ฌ ๊ณ์ฐ ์ค์ธ ๋ถ๋ถ ๋ฐฐ์ด์ ํฉ๋ง ๊ณ์ ๊ฐฑ์ ํฉ๋๋ค | ||
Big O | ||
- N: ์ฃผ์ด์ง ๋ฐฐ์ด nums์ ๊ธธ์ด | ||
- Time complexity: O(N) | ||
- Space complexity: O(1) | ||
*/ | ||
|
||
func maxSubArray(nums []int) int { | ||
maxSum, currSum := nums[0], nums[0] | ||
|
||
for i := 1; i < len(nums); i++ { | ||
if currSum > 0 { | ||
currSum += nums[i] | ||
} else { | ||
currSum = nums[i] | ||
} | ||
|
||
if maxSum < currSum { | ||
maxSum = currSum | ||
} | ||
} | ||
|
||
return maxSum | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
ํ์ด | ||
- ์ฌ๋ผ์ด๋ฉ ์๋์ฐ๋ฅผ ์ด์ฉํ์ฌ ํ์ดํฉ๋๋ค | ||
- ์ฃผ์ด์ง ๋ฌธ์์ด t์ ๊ฐ ๋ฌธ์์ ๊ทธ ๊ฐ์๋ฅผ tMap์ด๋ผ๋ ํด์๋งต์ ์ ์ฅํฉ๋๋ค | ||
- ํ์ฌ ์ฌ๋ผ์ด๋ฉ ์๋์ฐ์ ํฌํจ๋ ๋ฌธ์์ ๊ทธ ๊ฐ์๋ฅผ sMap์ด๋ผ๋ ํด์๋งต์ ์ ์ฅํฉ๋๋ค | ||
- ์ฌ๋ผ์ด๋ฉ ์๋์ฐ์ ๋๋จ(end)์ ๋ํ๋๊ฐ๋ฉด์ sMap๊ณผ tMap์ ๋น๊ตํฉ๋๋ค | ||
์ด ๋ ์ฌ๋ผ์ด๋ฉ ์๋์ฐ๊ฐ t์ ๋ชจ๋ ๋ฌธ์๋ฅผ ํฌํจํ๋ ๊ฒฝ์ฐ๋ฅผ ์ฐพ์ผ๋ฉด ์์๋จ(start)์ ์ขํ์ฃผ๋ฉด์ ์ฌ๋ผ์ด๋ฉ ์๋์ฐ์ ์ต์ํญ์ ์ฐพ์ต๋๋ค | ||
์ฌ๋ผ์ด๋ฉ ์๋์ฐ๊ฐ t์ ๋ชจ๋ ๋ฌธ์๋ฅผ ํฌํจํ๋์ง๋ฅผ ํ๋ณํ๊ธฐ ์ํด์ match๋ผ๋ ๋ณ์๋ฅผ ์ฌ์ฉํฉ๋๋ค | ||
match๋ sMap[c] == tMap[c]์ธ ๋ฌธ์ c์ ๊ฐ์์ด๋ฉฐ, match == len(tMap) ์ฆ match๊ฐ t์ ๊ณ ์ ๋ฌธ์ ๊ฐ์์ ๊ฐ๋ค๋ฉด ์ฌ๋ผ์ด๋ฉ ์๋์ฐ๊ฐ t์ ๋ชจ๋ ๋ฌธ์๋ฅผ ํฌํจํ๋ ๊ฒ์ด๋ผ๊ณ ๋ณผ ์ ์์ต๋๋ค | ||
์ฌ๋ผ์ด๋ฉ ์๋์ฐ๋ฅผ ์ขํ์ค ๋์๋ match์ ๊ฐ์ ์ฌ๋ถ๋ฅผ ์ ๊ด์ฐฐํ๋ฉฐ ์ต์ํญ์ ๊ฐฑ์ ํฉ๋๋ค | ||
์ฌ๋ผ์ด๋ฉ ์๋์ฐ๋ฅผ ์ค์ผ๋งํผ ์ค์๋ค๋ฉด ๋ค์ ์ฌ๋ผ์ด๋ฉ ์๋์ฐ์ ๋๋จ์ ๋ํ๋๊ฐ๋ฉด์ ๋ค๋ฅธ ๊ฒฝ์ฐ์ ์๋ฅผ ์ฐพ์ต๋๋ค | ||
Big O | ||
- M: ์ฃผ์ด์ง ๋ฌธ์์ด s์ ๊ธธ์ด | ||
- N: ์ฃผ์ด์ง ๋ฌธ์์ด t์ ๊ธธ์ด | ||
- Time complexity: O(M + N) | ||
- ๋ฌธ์์ด s๋ฅผ ์ํํ๋ ๋ฐ๋ณต๋ฌธ ๋ด์์ ๊ฐ ๋ฌธ์๋ ์ต๋ ๋ ๋ฒ ์กฐํ๋ ์ ์์ต๋๋ค (start, end) -> O(2M) | ||
- ๋ฌธ์์ด t๋ก ํด์๋งต์ ๋ง๋๋ ๋ฐ์๋ O(N)์ ์๊ฐ๋ณต์ก๋๊ฐ ์์๋ฉ๋๋ค | ||
- O(2M + N) -> O(M + N) | ||
- Space complexity: O(M + N) | ||
- ๋ ๊ฐ์ ํด์๋งต์ ์ฌ์ฉํ๋ฏ๋ก O(M + N)์ ๊ณต๊ฐ๋ณต์ก๋๋ฅผ ๊ฐ์ต๋๋ค | ||
*/ | ||
|
||
func minWindow(s string, t string) string { | ||
sMap, tMap := map[string]int{}, map[string]int{} | ||
for _, r := range t { | ||
tMap[string(r)]++ | ||
} | ||
|
||
start, end, matched, res := 0, 0, 0, "" | ||
for end < len(s) { | ||
sMap[string(s[end])]++ | ||
|
||
if sMap[string(s[end])] == tMap[string(s[end])] { | ||
matched++ | ||
} | ||
|
||
for matched == len(tMap) { | ||
if res == "" || len(res) > end-start+1 { | ||
res = s[start : end+1] | ||
} | ||
|
||
if sMap[string(s[start])] == tMap[string(s[start])] { | ||
matched-- | ||
} | ||
sMap[string(s[start])]-- | ||
start++ | ||
} | ||
|
||
end++ | ||
} | ||
|
||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
ํ์ด | ||
- BFS๋ฅผ ์ด์ฉํ์ฌ ํ์ดํ ์ ์์ต๋๋ค | ||
- ์ฃผ์ด์ง ๋ฐฐ์ด์ ๊ฐ์ฅ์๋ฆฌ์์๋ถํฐ ์์ํ๋ BFS๋ฅผ pacific๊ณผ atlantic์ ๋ํด ๊ฐ๊ฐ ์คํํฉ๋๋ค | ||
Big O | ||
- M: ์ฃผ์ด์ง ๋ฐฐ์ด์ ํ์ ๊ฐ์ | ||
- N: ์ฃผ์ด์ง ๋ฐฐ์ด์ ์ด์ ๊ฐ์ | ||
- Time complexity: O(MN) | ||
- ํ์์ ์งํํ๋ ํ์๋ ๋ฐฐ์ด์ ์์ ๊ฐ์์ ๋น๋กํ์ฌ ์ฆ๊ฐํฉ๋๋ค | ||
- Space complexity: O(MN) | ||
- queue์ ์ต๋ ํฌ๊ธฐ๋ ๋ฐฐ์ด์ ์์ ๊ฐ์์ ๋น๋กํ์ฌ ์ฆ๊ฐํฉ๋๋ค | ||
- memo ๋ฐฐ์ด์ ํฌ๊ธฐ๋ ๋ฐฐ์ด์ ์์ ๊ฐ์์ ๋น๋กํ์ฌ ์ฆ๊ฐํฉ๋๋ค | ||
*/ | ||
|
||
type pair struct { | ||
pacific bool | ||
atlantic bool | ||
} | ||
|
||
func pacificAtlantic(heights [][]int) [][]int { | ||
var res [][]int | ||
|
||
m, n := len(heights), len(heights[0]) | ||
|
||
dr := []int{0, 0, 1, -1} | ||
dc := []int{1, -1, 0, 0} | ||
|
||
// ๋ชจ๋ r, c์ ๋ํด memo[r][c] = {pacific: false, atlantic: false}๋ก ์ด๊ธฐํํฉ๋๋ค | ||
memo := make([][]pair, m) | ||
for r := range memo { | ||
memo[r] = make([]pair, n) | ||
} | ||
|
||
queue := make([][]int, 0) | ||
|
||
// pacific์์ ์์ํ๋ BFS | ||
for c := 0; c < n; c++ { | ||
queue = append(queue, []int{0, c}) | ||
} | ||
for r := 1; r < m; r++ { | ||
queue = append(queue, []int{r, 0}) | ||
} | ||
|
||
for len(queue) > 0 { | ||
r, c := queue[0][0], queue[0][1] | ||
queue = queue[1:] | ||
|
||
memo[r][c].pacific = true | ||
|
||
for i := 0; i < 4; i++ { | ||
nr, nc := r+dr[i], c+dc[i] | ||
|
||
if !(0 <= nr && nr < m && 0 <= nc && nc < n) { | ||
continue | ||
} | ||
|
||
if heights[r][c] <= heights[nr][nc] && !memo[nr][nc].pacific { | ||
queue = append(queue, []int{nr, nc}) | ||
} | ||
} | ||
} | ||
|
||
// atlantic์์ ์์ํ๋ BFS | ||
for c := 0; c < n; c++ { | ||
queue = append(queue, []int{m - 1, c}) | ||
} | ||
for r := 0; r < m-1; r++ { | ||
queue = append(queue, []int{r, n - 1}) | ||
} | ||
|
||
for len(queue) > 0 { | ||
r, c := queue[0][0], queue[0][1] | ||
queue = queue[1:] | ||
|
||
memo[r][c].atlantic = true | ||
|
||
for i := 0; i < 4; i++ { | ||
nr, nc := r+dr[i], c+dc[i] | ||
|
||
if !(0 <= nr && nr < m && 0 <= nc && nc < n) { | ||
continue | ||
} | ||
|
||
if heights[r][c] <= heights[nr][nc] && !memo[nr][nc].atlantic { | ||
memo[nr][nc].atlantic = true | ||
queue = append(queue, []int{nr, nc}) | ||
} | ||
} | ||
} | ||
|
||
for r, row := range memo { | ||
for c, el := range row { | ||
if el.pacific && el.atlantic { | ||
res = append(res, []int{r, c}) | ||
} | ||
} | ||
} | ||
|
||
return res | ||
} |