Skip to content

Commit f9dc342

Browse files
Merge pull request #626 from changchanghwang/main
[�Arthur] Week 1
2 parents 9a79056 + 4f5f15e commit f9dc342

File tree

5 files changed

+130
-0
lines changed

5 files changed

+130
-0
lines changed

contains-duplicate/changchanghwang.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Time complexity, O(n)
2+
// Space complexity, O(n)
3+
// 풀이
4+
// nums 배열을 순회하면서 hashMap에 num을 key로, 존재 여부를 value로 저장한다.
5+
// 만약 이미 존재하는 key라면 true를 반환하고, 순회를 전부 했는데도 중복이 없다면 false를 반환한다.
6+
func containsDuplicate(nums []int) bool {
7+
hashMap := map[int]bool{}
8+
for _, num := range nums {
9+
if hashMap[num] {
10+
return true
11+
}
12+
hashMap[num] = true
13+
}
14+
return false
15+
}

house-robber/changchanghwang.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// time complexity: O(n)
2+
// space complexity: O(1)
3+
func rob(nums []int) int {
4+
prev := 0
5+
curr := 0
6+
7+
for _, num := range nums {
8+
temp := curr
9+
curr = max(prev+num, curr)
10+
prev = temp
11+
}
12+
13+
return curr
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// time complexity: O(n)
2+
// space complexity: O(n)
3+
// 풀이
4+
// 1. map에 nums의 모든 요소를 저장한다.
5+
// 2. map을 순회하면서 num-1이 존재하는지 확인한다.
6+
// 3. num-1이 존재하면 continue
7+
// 4. num-1이 존재하지 않으면 consecutiveCount를 1로 초기화하고 num+1이 존재하는지 (연속적으로 숫자가 증가하는게 있는지) 확인한다.
8+
// 5. num+1이 존재하면 consecutiveCount를 1 증가시키고 num을 1 증가시켜 다음 수를 찾는다.
9+
// 6. num+1이 존재하지 않으면 현재까지의 consecutiveCount와 maxConsecutiveCount를 비교하여 maxConsecutiveCount를 갱신한다.
10+
func longestConsecutive(nums []int) int {
11+
numMap := make(map[int]bool)
12+
13+
for _, num := range nums {
14+
numMap[num] = true
15+
}
16+
17+
maxConsecutiveCount := 0
18+
19+
for num := range numMap {
20+
if numMap[num-1] {
21+
continue
22+
}
23+
consecutiveCount := 1
24+
for numMap[num+1] {
25+
num++
26+
consecutiveCount++
27+
}
28+
if consecutiveCount > maxConsecutiveCount {
29+
maxConsecutiveCount = consecutiveCount
30+
}
31+
}
32+
33+
return maxConsecutiveCount
34+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Time: O(nlogn)
2+
// Space: O(n)
3+
// 풀이
4+
// hashMap에 num을 key로, count를 value로 저장한다.
5+
// hashMap을 배열로 만들어 count순으로 정렬한다.
6+
// 정렬된 배열에서 앞에서부터 k개만 뽑아내서 반환한다.
7+
func topKFrequent(nums []int, k int) []int {
8+
hashMap := map[int]int{}
9+
for _, num := range nums {
10+
hashMap[num]++
11+
}
12+
13+
result := [][]int{}
14+
15+
for num, count := range hashMap {
16+
result = append(result, []int{num, count})
17+
}
18+
19+
sort.Slice(result, func(i, j int) bool { // go의 sort는 quicksort를 기본적으로 사용한다. O(nlogn)
20+
return result[i][1] > result[j][1]
21+
})
22+
23+
resultNums := make([]int, 0, k)
24+
for i := 0; i < k; i++ {
25+
resultNums = append(resultNums, result[i][0])
26+
}
27+
28+
return resultNums
29+
}

valid-palindrome/changchanghwang.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// 풀이
2+
// alphabet, number만 걸러내서 소문자로 바꾼 후 reverse한 문자열, 걸러낸 문자열과 비교하여 같은지 확인한다.
3+
4+
// O(n) time complexity
5+
// O(n) space complexity
6+
func isPalindrome(s string) bool {
7+
regex, _ := regexp.Compile("[a-zA-Z0-9]")
8+
9+
lowerCaseString := strings.ToLower(s)
10+
reverseAndFilteredString := ""
11+
filteredString := ""
12+
13+
for _, char := range lowerCaseString {
14+
if regex.MatchString(string(char)) {
15+
reverseAndFilteredString = c + reverseAndFilteredString
16+
filteredString += c
17+
}
18+
}
19+
20+
return reverseAndFilteredString == filteredString
21+
}
22+
23+
// O(n) time complexity
24+
// O(n) space complexity
25+
func isPalindrome2(s string) bool {
26+
lowerCaseString := strings.ToLower(s)
27+
reverseAndFilteredString := ""
28+
var filteredString strings.Builder
29+
30+
for _, char := range lowerCaseString {
31+
if unicode.IsLetter(char) || unicode.IsDigit(char) {
32+
reverseAndFilteredString = string(char) + reverseAndFilteredString
33+
filteredString.WriteRune(char)
34+
}
35+
}
36+
37+
return reverseAndFilteredString == filteredString.String()
38+
}

0 commit comments

Comments
 (0)