-
-
Notifications
You must be signed in to change notification settings - Fork 245
[똘치] Week1 #667
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
[똘치] Week1 #667
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1a00ff9
Feat: Add solution of contains-duplicate
52b09c0
Feat: Add solution of valid-palindrome
15811f5
Feat: Add solution of contains-duplicate
a9dd0c7
Feat: Add solution of valid-palindrome
6592f1d
Merge branch 'main' of github.com:easyone-jwlee/leetcode-study
9bfe355
Feat: Add solution of top-k-frequent-elements
5fcd582
Feat: Add solution of longest-consecutive-sequence.
ed398ff
Docs: Add comments explaining the solution process.
0953f26
Feat: Add solution of house-robber
easyone-jwlee 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,24 @@ | ||
// 풀이 | ||
// map으로 중복된 값이 있는지 체크 | ||
|
||
// TC | ||
// 중복이 하나도 없는 경우에 최대 n번 조회 | ||
// n번 반복시 총 작업의 복잡도는 O(n) | ||
|
||
// SC | ||
// n개의 숫자를 저장하면 map이 사용하는 공간은 최대 O(n) | ||
|
||
// (+) 정렬을 사용한다면? | ||
// 입력된 배열을 정렬해서 서로 인접한 값을 비교하면 O(1)의 SC로 중복 확인 가능. | ||
// 그러나 정렬을 사용하면 TC가 O(nlogn). | ||
|
||
func containsDuplicate(nums []int) bool { | ||
m := make(map[int]int) | ||
for _, num := range nums { | ||
if _, ok := m[num]; ok { | ||
return true | ||
} | ||
m[num] = num | ||
} | ||
return false | ||
} |
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,36 @@ | ||
// 풀이 | ||
// dp를 사용하여 현재 털 수 있는 최대한의 돈을 계산 | ||
// curr이 prev가 되고, prev였던 값이 새로운 값을 더한 것과 curr 이었던 값의 최대값을 비교한 것이 새로운 curr이 된다. | ||
// 마지막엔 prev와 curr의 최대값을 비교 | ||
// 이렇게 하면 털 수 있는 집의 최대값을 계속 가지고 있을 수 있게 됨. | ||
|
||
// TC | ||
// O(n) | ||
|
||
// SC | ||
// 늘어나지 않는 int 값만 사용했으므로 O(1) | ||
|
||
func rob(nums []int) int { | ||
length := len(nums) | ||
|
||
if length == 1 { | ||
return nums[0] | ||
} | ||
|
||
prev := 0 | ||
curr := nums[0] | ||
|
||
for i := 1; i < length; i++ { | ||
prev, curr = curr, max(nums[i]+prev, curr) | ||
} | ||
|
||
return max(prev, curr) | ||
} | ||
|
||
func max(a, b int) int { | ||
if a > b { | ||
return a | ||
} else { | ||
return b | ||
} | ||
} |
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,40 @@ | ||
// 풀이 | ||
// You must write an algorithm that runs in O(n) time. | ||
// TC를 O(n) 이내로 해야한다는 것은 sort를 쓰지 말라는 의미. | ||
// map을 사용하고 순회하며 연속이 시작되는 값을 찾고 찾으면 연속되는지 찾기. | ||
|
||
// TC | ||
// 순회하는 map안에서 for문을 또 호출하긴 하지만, | ||
// 모든 값이 연속되는 값이라고 했을 때 | ||
// 연속이 시작되는 값 외에는 한 번씩 바로 지나가게 되고(n*1), 시작되는 값부터 연속이 끝나는 시점까지 n번이라(1*n) | ||
// O(n+n) 이기 때문에 TC는 O(n) | ||
|
||
// SC | ||
// map이 최대로 차지하는 공간은 O(n) | ||
|
||
func longestConsecutive(nums []int) int { | ||
m := make(map[int]bool) | ||
for _, num := range nums { | ||
m[num] = true | ||
} | ||
length := 1 | ||
maxLength := 0 | ||
for k := range m { | ||
if _, ok := m[k-1]; !ok { | ||
i := 1 | ||
for { | ||
if _, ok := m[k+i]; ok { | ||
length++ | ||
i++ | ||
} else { | ||
break | ||
} | ||
} | ||
if maxLength < length { | ||
maxLength = length | ||
} | ||
length = 1 | ||
} | ||
} | ||
return maxLength | ||
} |
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,31 @@ | ||
// 풀이 | ||
// map으로 입력 숫자들이 각각 몇번 반복되는지 정리 | ||
// [][]int를 선언하고 반복된 횟수를 index로, 입력 숫자값을 배열에 append한다. | ||
// 그리고 배열을 역순으로 순회하며 k개의 element를 가진 결과 배열을 만든다. | ||
|
||
// TC | ||
// O(n) | ||
|
||
// SC | ||
// 모든 숫자가 다르다고 해도 각 숫자는 하나의 하위배열에만 속한다. 따라서 O(n) | ||
|
||
func topKFrequent(nums []int, k int) []int { | ||
m := make(map[int]int) | ||
for _, num := range nums { | ||
m[num]++ | ||
} | ||
a := make([][]int, len(nums)+1) | ||
for key, num := range m { | ||
a[num] = append(a[num], key) | ||
} | ||
result := make([]int, 0) | ||
for i := cap(a) - 1; i >= 0; i-- { | ||
if len(a[i]) > 0 { | ||
result = append(result, a[i]...) | ||
easyone-jwlee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
if len(result) == k { | ||
break | ||
} | ||
} | ||
return result | ||
} |
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,31 @@ | ||
// 풀이 | ||
// 유효한 string 값만 정제하고 palindrome check. | ||
|
||
// TC | ||
// 입력된 string의 길이에 따라 최대 O(n) | ||
|
||
// SC | ||
// validStr으로 유효한 string을 정제하기 때문에 최대 O(n) | ||
|
||
// (+) 입력된 string을 사용하는 방식으로 개선하면 SC가 O(1) | ||
|
||
func isPalindrome(s string) bool { | ||
s = strings.ToLower(s) | ||
validStr := "" | ||
for _, str := range s { | ||
if ('a' > str || 'z' < str) && ('0' > str || '9' < str) { | ||
continue | ||
} | ||
validStr += string(str) | ||
} | ||
if len(validStr) <= 1 { | ||
return true | ||
} | ||
l := len(validStr) | ||
for i := 0; i < l/2; i++ { | ||
if validStr[i] != validStr[l-1-i] { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
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.
이중 for문은 n^2라는 생각만 가지고 있었는데 조건문으로 제한해서 시간복잡도를 개선한 점이 신기했어요!