Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ysle0] Week2 #742

Merged
merged 7 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

import (
"fmt"
)

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:]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

javascript의 slice 메소드가 이렇게 다르게 생겼군요!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네.. 저도 처음에 이렇게 slicing 하는 것 보고 되게 신기했었어요 ㅎㅎ

fmt.Printf("ret=%v\n", ret)
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)
//}
28 changes: 28 additions & 0 deletions valid-anagram/ysle0.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package valid_anagram

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 {
return false
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

밑에서 포문으로 한번 걸러주는 로직 덕분에 이 조건문은 없어도 되는 것 같습니다~

Copy link
Contributor

@obzva obzva Dec 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@taewanseoul
극단적인 경우엔 해당 조건문이 상당한 양의 연산을 줄일 수 있습니다

// 예
s = "a...a" // (길이 5 * 10^4)
t = "b...b" // (길이 5 * 10^4)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ysle0
isAnagram 함수의 body 가장 앞단에 아래와 같은 로직도 있으면 좋을 것 같네요 :)

func isAnagram(s string, t string) bool {
    if len(s) != len(t) {
        return false
    }
    ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 그렇겠네요 경계검사 조건 피드백 감사합니다!


ht[r] -= 1
if cnt-1 < 0 {
return false
}
}

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
Loading