Skip to content

Commit

Permalink
Merge pull request #711 from easyone-jwlee/main
Browse files Browse the repository at this point in the history
[๋˜˜์น˜] Week2
  • Loading branch information
easyone-jwlee authored Dec 19, 2024
2 parents e0d7d23 + df74dd4 commit 36cfe67
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 0 deletions.
46 changes: 46 additions & 0 deletions 3sum/easyone-jwlee.go
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
}
23 changes: 23 additions & 0 deletions climbing-stairs/easyone-jwlee.go
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
}
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)
}
33 changes: 33 additions & 0 deletions decode-ways/easyone-jwlee.go
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
}
27 changes: 27 additions & 0 deletions valid-anagram/easyone-jwlee.go
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
}

0 comments on commit 36cfe67

Please sign in to comment.