-
-
Notifications
You must be signed in to change notification settings - Fork 245
[ysle0] Week2 #742
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
Merged
Merged
[ysle0] Week2 #742
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4666f02
PR review 수정
ysle0 3f1552f
add: #242 valid anagram
ysle0 3223589
tidy: unused codes
ysle0 3057d82
add: #70 climbing stairs
ysle0 ed5382d
Merge branch 'DaleStudy:main' into main
ysle0 700c896
add: 1,2 번쨰 문제 주석 추가
ysle0 0b703de
add: 3sum 풀이 추가
ysle0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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 | ||
} |
This file contains hidden or 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,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 | ||
} |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,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 | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
javascript의 slice 메소드가 이렇게 다르게 생겼군요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
네.. 저도 처음에 이렇게 slicing 하는 것 보고 되게 신기했었어요 ㅎㅎ