-
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 #711 from easyone-jwlee/main
[๋์น] Week2
- Loading branch information
Showing
5 changed files
with
169 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,46 @@ | ||
// ํ์ด | ||
// ๋ฐฐ์ด์ ์ ๋ ฌํ๊ณ | ||
// two pointer ์ฌ์ฉ | ||
|
||
// TC | ||
// ์ ๋ ฌ O(nlogn) + Two pointer ์ด์ค ๋ฃจํ O(n^2) = O(n^2) | ||
|
||
// SC | ||
// Go์ sort.Ints()๋ TimSort๋ฅผ ์ฌ์ฉ. | ||
// Merge Sort์ Insertion Sort์ ์กฐํฉ์ผ๋ก ๋์. | ||
// ์ ๋ ฌ O(n) + Two pointer O(1) + ๊ฒฐ๊ณผ ๋ฐฐ์ด O(n) = O(n) | ||
|
||
func threeSum(nums []int) [][]int { | ||
result := [][]int{} | ||
sort.Ints(nums) // nums๋ฅผ ์ ๋ ฌ | ||
|
||
for i := 0; i < len(nums)-2; i++ { | ||
if i > 0 && nums[i] == nums[i-1] { | ||
continue // ์ค๋ณต๋ ๊ฐ ๊ฑด๋๋ | ||
} | ||
|
||
left, right := i+1, len(nums)-1 | ||
for left < right { | ||
sum := nums[i] + nums[left] + nums[right] | ||
if sum == 0 { | ||
result = append(result, []int{nums[i], nums[left], nums[right]}) | ||
left++ | ||
right-- | ||
|
||
// ์ค๋ณต ์ ๊ฑฐ | ||
for left < right && nums[left] == nums[left-1] { | ||
left++ | ||
} | ||
for left < right && nums[right] == nums[right+1] { | ||
right-- | ||
} | ||
} else if sum < 0 { | ||
left++ | ||
} else { | ||
right-- | ||
} | ||
} | ||
} | ||
|
||
return result | ||
} |
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,23 @@ | ||
// ํ์ด | ||
// 1 ์ผ ๋, ๊ฐ๋ฅํ step์ 1 -> 1๊ฐ์ง | ||
// 2 ์ผ ๋, ๊ฐ๋ฅํ step์ 1 1, 2 -> 2๊ฐ์ง | ||
// 3 ์ผ ๋, ๊ฐ๋ฅํ step์ 1 1 1, 1 2, 2 1 -> 3๊ฐ์ง | ||
// n ์ผ ๋, ๊ฐ๋ฅํ stop์ n-1์ ๊ฐ์ง์์ 1์ด ๋ถ๊ณ , n-2์ ๊ฐ์ง์์ 2๊ฐ ๋ถ๋๋ค. | ||
|
||
// TC | ||
// O(n) | ||
|
||
// SC | ||
// intํ์ ๋ณ์๋ง ์ฌ์ฉํด์ O(1) | ||
|
||
func climbStairs(n int) int { | ||
if n <= 2 { | ||
return n | ||
} | ||
prev := 1 // climb1 | ||
curr := 2 // climb2 | ||
for i := 3; i <= n; i++ { | ||
prev, curr = curr, (curr + prev) | ||
} | ||
return curr | ||
} |
40 changes: 40 additions & 0 deletions
40
construct-binary-tree-from-preorder-and-inorder-traversal/easyone-jwlee.go
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,40 @@ | ||
// ํ์ด | ||
// preorder[0]์ด ์ ์ผ ๊ผญ๋๊ธฐ | ||
// preorder ๋ฐฐ์ด์ root index, inorder ๋ฐฐ์ด์ ์์, inorder ๋ฐฐ์ด์ ๋ | ||
// ์์ ์ธ๊ฐ์ง๋ฅผ parameter๋ก ๋ฐ๋ ์ฌ๊ทํจ์๋ฅผ ๋ง๋ค์ด์ | ||
// ์ผ์ชฝ ์๋ธํธ๋ฆฌ, ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ๋ฅผ ์ํ | ||
|
||
// TC | ||
// map ๋ง๋ค๊ธฐ O(n) + ์ฌ๊ทํจ์๋ ๊ฐ ๋ ธ๋๋ฅผ ๋ฑ ํ๋ฒ์ฉ๋ง ๋ฐฉ๋ฌธํ๋ฏ๋ก O(n) = O(n) | ||
|
||
// SC | ||
// map O(n) + ์ฌ๊ทํจ์ ์ต์ ์ ๊ฒฝ์ฐ ํ์ชฝ์ผ๋ก๋ง ๋ ธ๋๊ฐ ์ด์ด์ง๋ ๊ฒฝ์ฐ O(n) = O(n) | ||
|
||
func buildTree(preorder []int, inorder []int) *TreeNode { | ||
inorderMap := make(map[int]int) | ||
for i, n := range inorder { | ||
inorderMap[n] = i | ||
} | ||
|
||
var recursive func(rootIndex, inStart, inEnd int) *TreeNode | ||
recursive = func(rootIndex, inStart, inEnd int) *TreeNode { | ||
if rootIndex >= len(preorder) || inStart > inEnd { | ||
return nil | ||
} | ||
rootVal := preorder[rootIndex] | ||
rootInorderIndex := inorderMap[rootVal] | ||
|
||
result := &TreeNode{ | ||
Val: rootVal, | ||
} | ||
|
||
leftSize := rootInorderIndex - inStart | ||
|
||
result.Left = recursive(rootIndex+1, inStart, rootInorderIndex-1) | ||
result.Right = recursive(rootIndex+1+leftSize, rootInorderIndex+1, inEnd) | ||
|
||
return result | ||
} | ||
|
||
return recursive(0, 0, len(inorder)-1) | ||
} |
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,33 @@ | ||
// ํ์ด | ||
// dp๋ก ํ์ด | ||
// ๋์๋ฆฌ ์์ ์ ํฉํ๋ฉด prev2(i-2)์ ํด๋นํ๋ ๊ฐ์ ๋ํ๊ธฐ | ||
|
||
// TC | ||
// O(n) | ||
|
||
// SC | ||
// data type์ด int์ธ ๋ณ์๋ง ์ฌ์ฉํ์ผ๋ฏ๋ก O(1) | ||
|
||
func numDecodings(s string) int { | ||
if len(s) == 0 || s[0] == '0' { | ||
return 0 | ||
} | ||
prev2, prev1 := 1, 1 | ||
for i := 1; i < len(s); i++ { | ||
curr := 0 | ||
|
||
// ํ์๋ฆฌ์ ํ์ธ | ||
if s[i] != '0' { | ||
curr += prev1 | ||
} | ||
|
||
// ๋์๋ฆฌ์ ํ์ธ | ||
digit, _ := strconv.Atoi(s[i-1 : i+1]) | ||
if digit >= 10 && digit <= 26 { | ||
curr += prev2 | ||
} | ||
|
||
prev2, prev1 = prev1, curr | ||
} | ||
return prev1 | ||
} |
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,27 @@ | ||
// ํ์ด | ||
// s์ rune์ key๋ก ๊ฐ์ ์ฒ ์์ ๊ฐฏ์๋ฅผ ๋ด๊ฒํ๊ณ | ||
// t์ ๋น๊ตํ๋ฉด์ ๊ฐฏ์๋ฅผ ํ๋์ฉ ๊ฐ์ํ๊ฒ ํด์ | ||
// ๋จ์ ์ฒ ์๊ฐ ์๊ฑฐ๋ (s์๋ ์๊ณ , r์๋ ์๋ ์ฒ ์๊ฐ ์๋ ๊ฒฝ์ฐ) | ||
// ๋ ๊ฐ์๋ ์ฒ ์๊ฐ ์์ผ๋ฉด (s์๋ ์๊ณ , r์๋ง ์๋ ์ฒ ์๊ฐ ์๋ ๊ฒฝ์ฐ) false๊ฐ return ๋๊ฒ ํจ. | ||
|
||
// TC | ||
// O(n+n+n) = O(n) | ||
|
||
// SC | ||
// s์ ๊ธธ์ด๋งํผ ๋์ด๋๋ map์ผ๋ก ์ธํด O(n) | ||
|
||
func isAnagram(s string, t string) bool { | ||
m := make(map[rune]int) | ||
for _, r := range s { | ||
m[r]++ | ||
} | ||
for _, r := range t { | ||
m[r]-- | ||
} | ||
for _, count := range m { | ||
if count < 0 || count > 0 { | ||
return false | ||
} | ||
} | ||
return true | ||
} |