Skip to content

Commit

Permalink
Merge pull request #742 from ysle0/main
Browse files Browse the repository at this point in the history
[ysle0] Week2
  • Loading branch information
ysle0 authored Dec 22, 2024
2 parents 109a279 + 0b703de commit 626038c
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 20 deletions.
72 changes: 72 additions & 0 deletions 3sum/ysle0.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// package _sum
package main

import (
"slices"
"strconv"
)

/*
* ์ฒซ ๋ฒˆ์งธ ์ˆ˜ n1
* ๋‘ ๋ฒˆ์งธ ์ˆ˜ n2
* ์„ธ ๋ฒˆ์งธ ์ˆ˜ n3 ์ผ๋•Œ,
* 2sum ๋ฌธ์ œ์™€ ๋™์ผํ•˜๊ฒŒ map์— ๋ชจ๋“  ์ˆ˜๋ฅผ ๋งตํ•‘ํ•˜์—ฌ
* ๋‘๊ฐ€์ง€ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์„ธ๊ฐ€์ง€ ์ˆ˜๊ฐ€ map์— ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜์—ฌ
* ์ค‘๋ณต๋˜์ง€ ์•Š๋„๋ก ๊ฒฐ๊ณผ๋ฅผ map์— ์ €์žฅ -> [][]int๋กœ ๋ฐ˜ํ™˜
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(N^2)
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(N)
*/
func threeSum(nums []int) [][]int {
triplets := make(map[string][]int)
for i, n1 := range nums[:len(nums)-2] {
seen := make(map[int]int)
for _, n2 := range nums[i+1:] {
target := -n1 - n2
if _, ok := seen[target]; ok {
item := []int{n1, n2, target}
slices.Sort(item)
key := strconv.Itoa(item[0]) + strconv.Itoa(item[1]) + strconv.Itoa(item[2])
triplets[key] = item
}
seen[n2] = n2
}
}

ret := make([][]int, 0)
for _, t := range triplets {
ret = append(ret, t)
}
return ret
}

/*
* ๋”ฐ๋กœ ๋งตํ•‘ํ•˜์—ฌ ์ฒซ ๋ฒˆ์งธ ์ˆ˜๋ฅผ ๊ณ ์ • ํ›„, 2,3 ๋ฒˆ์งธ ์ˆ˜๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ๋„๋Š” ์ฒซ ๋ฒˆ์งธ ๋ฐฉ์‹๊ณผ ๋™์ผํ•˜๊ฒŒ O(N^2) ๋น„์šฉ์€ ์œ ์ง€
* ๋˜์ง€๋งŒ, ๊ณต๊ฐ„ ๋ณต์žก๋„ ๋ฉด์—์„œ O(N) -> O(1)๋กœ ๊ฐœ์„ ์ด ๋จ.
*/
func threeSum2(nums []int) [][]int {
slices.Sort(nums)
triplets := make(map[string][]int, 0)

for i := 0; i < len(nums); i++ {
l, h := i+1, len(nums)-1
for l < h {
sum := nums[l] + nums[h] + nums[i]
if sum < 0 {
l++
} else if sum > 0 {
h--
} else {
k := strconv.Itoa(nums[l]) + strconv.Itoa(nums[h]) + strconv.Itoa(nums[i])
nums := []int{nums[i], nums[l], nums[h]}
triplets[k] = nums
l++
h--
}
}
}
ret := make([][]int, 0)
for _, t := range triplets {
ret = append(ret, t)
}
return ret
}
25 changes: 25 additions & 0 deletions climbing-stairs/ysle0.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package climbing_stairs

/**
* ํ’€์ด: ์ด์ „ ์Šคํ…์„ ๊ณ„์‚ฐํ•˜์—ฌ ๊ณ ์ • ํ•œํ›„ ๋‹ค์Œ ์กฐ๊ฑด์„ ํ™•์ธํ•˜๋Š” ๊ฒƒ์—์„œ dp์œ ํ˜•์˜ ๋ฌธ์ œ๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ์ง€๋งŒ
* dp๊ฐ€ ๋ญ”์ง€๋ชฐ๋ผ ๋ฌด์„œ์›Œ ํŒจํ„ด์€ ๋ชป์ฐพ์•˜์Šต๋‹ˆ๋‹ค! (๊ฐ์ •์ )
* ๊ทธ๋ž˜์„œ ํžŒํŠธ ๋ณด๊ณ  ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด์ธ ๊ฒƒ์„ ํŒŒ์•… ํ›„ ์žฌ๊ท€ํ•˜์ง€์•Š๋Š” ๋ฐฉ์‹์œผ๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.
* ํ’€๋ฉด์„œ ํ”ผ๋ณด๋‚˜์น˜๋„ dp์˜ ๋ฐฉ์‹์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฑธ ๋ช…ํ™•ํ•˜๊ฒŒ ์•Œ์•˜๋„ค์š”. (memoization)
*/
func climbStairs(n int) int {
return fibo(n)
}

func fibo(n int) int {
ret := []int{0, 1}
for i := 2; i <= n; i++ {
ret = append(ret, ret[i-1]+ret[i-2])
}
ret = ret[len(ret)-2:]

sum := 0
for _, n := range ret {
sum += n
}
return sum
}
20 changes: 12 additions & 8 deletions contains-duplicate/ysle0.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package contains_duplicate
import "sort"

/*
1. ๋ฌธ์ œ
1. ๋ฌธ์ œ
์ฃผ์–ด์ง„ int ๋ฐฐ์—ด nums์— ์ˆซ์ž๊ฐ€ ์ค‘๋ณต๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ํ•œ ๋ฒˆ์ด๋ผ๋„ ์žˆ์œผ๋ฉด true, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false ๋ฅผ ๋ฆฌํ„ด
์ฃผ์–ด์ง„ int ๋ฐฐ์—ด nums์— ์ˆซ์ž๊ฐ€ ์ค‘๋ณต๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ํ•œ ๋ฒˆ์ด๋ผ๋„ ์žˆ์œผ๋ฉด true, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false ๋ฅผ ๋ฆฌํ„ด
2. ํ’€์ด
๊ณ ์œ ๊ฐ’๋งŒ ์ €์žฅํ•˜๋Š” set(go ์—์„œ๋Š” map)์˜ ์„ฑ์งˆ์„ ํ™œ์šฉํ•˜์—ฌ
nums๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ set์— ๊ฐ’์ด ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ์ฒดํฌํ•˜์—ฌ
์ˆซ์ž๊ฐ€ ์ค‘๋ณต๋˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ฒดํฌ
2. ํ’€์ด
๊ณ ์œ ๊ฐ’๋งŒ ์ €์žฅํ•˜๋Š” set(go ์—์„œ๋Š” map)์˜ ์„ฑ์งˆ์„ ํ™œ์šฉํ•˜์—ฌ
nums๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ set์— ๊ฐ’์ด ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ์ฒดํฌํ•˜์—ฌ
์ˆซ์ž๊ฐ€ ์ค‘๋ณต๋˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ฒดํฌ
3. ๋ถ„์„
- ์‹œ๊ฐ„ ๋ณต์žก๋„: O(N)
Expand All @@ -37,6 +35,12 @@ func containsDuplicate(nums []int) bool {
return false
}

/*
* ์ฒ˜์Œ๋ถ€ํ„ฐ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ๋žœ๋ค ์ ‘๊ทผ์˜ ํ•„์š”์„ฑ์„ ์—†์•ฐ.
* ๋ณต์žก๋„ ์ธก๋ฉด์—์„œ์˜ ๊ฐœ์„ ์ ์€ ์—†์–ด๋ณด์ž„.
- ์‹œ๊ฐ„ ๋ณต์žก๋„: O(N)
- ๊ณต๊ฐ„ ๋ณต์žก๋„: O(N)
*/
func containsDuplicate_SortedApproach(nums []int) bool {
// early exit for small slices
if len(nums) < 2 {
Expand Down
3 changes: 2 additions & 1 deletion longest-consecutive-sequence/ysle0.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func longestConsecutive(nums []int) int {
cursor++
}

//tmp := make([]int, 0, len(cons))
// map->array
tmp := make([]int, 0, len(cons))
for _, v := range cons {
tmp = append(tmp, v)
Expand All @@ -68,5 +68,6 @@ func longestConsecutive(nums []int) int {
func(a, b int) int {
return b - a
})

return tmp[0]
}
11 changes: 1 addition & 10 deletions top-k-frequent-elements/ysle0.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,5 @@ func topKFrequentElements_BucketSort(nums []int, k int) []int {
res = append(res, buc[i]...)
}

return res[:k]
return res
}

//
//func main() {
// r1 := topKFrequent([]int{1, 1, 1, 2, 2, 3}, 2)
// fmt.Println(r1)
//
// r2 := topKFrequent([]int{1}, 1)
// fmt.Println(r2)
//}
57 changes: 57 additions & 0 deletions valid-anagram/ysle0.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package valid_anagram

/**
* ํ’€์ด:
* t๊ฐ€ s์˜ anagram์ธ์ง€ ํ™•์ธํ•˜๋Š” ๋ฌธ์ œ!
* s๋ฅผ hashmap์— ๋งตํ•‘ํ•˜๊ณ , t์˜ rune r์„ ํ•˜๋‚˜์”ฉ s[r] ๋กœ ๊ฐฏ์ˆ˜๋ฅผ ์ฒดํฌํ•˜์—ฌ ํ’€์Œ.
* anagram์ด๋ฉด return true, otherwise false
* true ๊นŒ์ง€ ์กฐ๊ฑด 3๊ฐœ๊ฐ€ ์žˆ์Œ.
*
* TC: O(N) -> s๋‚˜ t์˜ ๊ธธ์ด ๋งŒํผ๋งŒ ๋Œ์Œ
* SC: O(N) -> M * N์—์„œ ์ตœ๊ณ ์ฐจํ•ญ ์ •๋ฆฌ
*/
func isAnagram(s string, t string) bool {
ht := make(map[rune]int, len(s))
for _, r := range s {
ht[r]++
}

for _, r := range t {
cnt, ok := ht[r]
if !ok { // 1. t์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” rune -> false
return false
}

ht[r] -= 1 // t์˜ rune์ด s์— ์กด์žฌํ•˜๋ฏ€๋กœ ๊ฐฏ์ˆ˜ 1 ์ฐจ๊ฐ
if cnt-1 < 0 { // 2. s์— ์žˆ๋Š” rune๋ณด๋‹ค ๋” ๋งŽ์ด ๊ฐ€์ง€๊ณ  ์žˆ์Œ
return false
}
}

for _, v := range ht {
if v > 0 { // 3. t๋ฅผ ์ˆœํšŒํ•˜๊ณ  s์— ๋‚จ์•„์žˆ๋Š”๊ฒŒ ์žˆ์–ด๋„ false
return false
}
}

return true
}

func isAnagram_faster(s string, t string) bool {
if len(s) != len(t) { // ๋‘˜์ด ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅด๋ฉด ๋‹น์—ฐํžˆ ์‹คํŒจํ•จ
return false
}

ht := make(map[byte]int, len(s))
for i, _ := range s {
ht[s[i]]++
ht[t[i]]-- // ๊ธฐ์กด์— ํ‘ผ ๋ฐฉ์‹์—์„œ ok ์ฒดํฌ ์•ˆํ•ด๋ฒ„๋ฆผ? ..
}

for _, v := range ht {
if v != 0 {
return false
}
}
return true
}
3 changes: 2 additions & 1 deletion valid-palindrome/ysle0.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
๋ชจ๋“  ๋ฌธ์ž์—ด์„ ๋Œ๋ฉฐ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜
palindrome ๋ฌธ์ž์—ด ์ฒดํฌ loop
์•ž ์ปค์„œ < ๋’ค ์ปค์„œ ์˜ ์กฐ๊ฑด์œผ๋กœ O(n/2) ---> O(n)
- ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
- ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
์ƒˆ๋กœ์šด ์ €์žฅ๊ณต๊ฐ„์€ ์—†์œผ๋ฉฐ ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด s ํ•˜๋‚˜๋ฟ
*/
var nonAlphanumericRegex = regexp.MustCompile(`[^a-zA-Z0-9]+`)
Expand Down Expand Up @@ -54,6 +54,7 @@ func isPalindrome(s string) bool {
/*
1. ๊ฐœ์„ ์ 
- regex ์˜ค๋ฒ„ํ—ค๋“œ ์ œ๊ฑฐ
๊ณต๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐœ์„ : O(N) -> O(1)
*/
func isPalindrome_Optimized(s string) bool {
front, rear := 0, len(s)-1
Expand Down

0 comments on commit 626038c

Please sign in to comment.