diff --git a/contains-duplicate/changchanghwang.go b/contains-duplicate/changchanghwang.go new file mode 100644 index 000000000..fd655fd2d --- /dev/null +++ b/contains-duplicate/changchanghwang.go @@ -0,0 +1,15 @@ +// Time complexity, O(n) +// Space complexity, O(n) +// 풀이 +// nums 배열을 순회하면서 hashMap에 num을 key로, 존재 여부를 value로 저장한다. +// 만약 이미 존재하는 key라면 true를 반환하고, 순회를 전부 했는데도 중복이 없다면 false를 반환한다. +func containsDuplicate(nums []int) bool { + hashMap := map[int]bool{} + for _, num := range nums { + if hashMap[num] { + return true + } + hashMap[num] = true + } + return false +} diff --git a/house-robber/changchanghwang.go b/house-robber/changchanghwang.go new file mode 100644 index 000000000..2b4e1c191 --- /dev/null +++ b/house-robber/changchanghwang.go @@ -0,0 +1,14 @@ +// time complexity: O(n) +// space complexity: O(1) +func rob(nums []int) int { + prev := 0 + curr := 0 + + for _, num := range nums { + temp := curr + curr = max(prev+num, curr) + prev = temp + } + + return curr +} diff --git a/longest-consecutive-sequence/changchanghwang.go b/longest-consecutive-sequence/changchanghwang.go new file mode 100644 index 000000000..432f30b2c --- /dev/null +++ b/longest-consecutive-sequence/changchanghwang.go @@ -0,0 +1,34 @@ +// time complexity: O(n) +// space complexity: O(n) +// 풀이 +// 1. map에 nums의 모든 요소를 저장한다. +// 2. map을 순회하면서 num-1이 존재하는지 확인한다. +// 3. num-1이 존재하면 continue +// 4. num-1이 존재하지 않으면 consecutiveCount를 1로 초기화하고 num+1이 존재하는지 (연속적으로 숫자가 증가하는게 있는지) 확인한다. +// 5. num+1이 존재하면 consecutiveCount를 1 증가시키고 num을 1 증가시켜 다음 수를 찾는다. +// 6. num+1이 존재하지 않으면 현재까지의 consecutiveCount와 maxConsecutiveCount를 비교하여 maxConsecutiveCount를 갱신한다. +func longestConsecutive(nums []int) int { + numMap := make(map[int]bool) + + for _, num := range nums { + numMap[num] = true + } + + maxConsecutiveCount := 0 + + for num := range numMap { + if numMap[num-1] { + continue + } + consecutiveCount := 1 + for numMap[num+1] { + num++ + consecutiveCount++ + } + if consecutiveCount > maxConsecutiveCount { + maxConsecutiveCount = consecutiveCount + } + } + + return maxConsecutiveCount +} diff --git a/top-k-frequent-elements/changchanghwang.go b/top-k-frequent-elements/changchanghwang.go new file mode 100644 index 000000000..ae35d45fd --- /dev/null +++ b/top-k-frequent-elements/changchanghwang.go @@ -0,0 +1,29 @@ +// Time: O(nlogn) +// Space: O(n) +// 풀이 +// hashMap에 num을 key로, count를 value로 저장한다. +// hashMap을 배열로 만들어 count순으로 정렬한다. +// 정렬된 배열에서 앞에서부터 k개만 뽑아내서 반환한다. +func topKFrequent(nums []int, k int) []int { + hashMap := map[int]int{} + for _, num := range nums { + hashMap[num]++ + } + + result := [][]int{} + + for num, count := range hashMap { + result = append(result, []int{num, count}) + } + + sort.Slice(result, func(i, j int) bool { // go의 sort는 quicksort를 기본적으로 사용한다. O(nlogn) + return result[i][1] > result[j][1] + }) + + resultNums := make([]int, 0, k) + for i := 0; i < k; i++ { + resultNums = append(resultNums, result[i][0]) + } + + return resultNums +} diff --git a/valid-palindrome/changchanghwang.go b/valid-palindrome/changchanghwang.go new file mode 100644 index 000000000..ea234ebf8 --- /dev/null +++ b/valid-palindrome/changchanghwang.go @@ -0,0 +1,38 @@ +// 풀이 +// alphabet, number만 걸러내서 소문자로 바꾼 후 reverse한 문자열, 걸러낸 문자열과 비교하여 같은지 확인한다. + +// O(n) time complexity +// O(n) space complexity +func isPalindrome(s string) bool { + regex, _ := regexp.Compile("[a-zA-Z0-9]") + + lowerCaseString := strings.ToLower(s) + reverseAndFilteredString := "" + filteredString := "" + + for _, char := range lowerCaseString { + if regex.MatchString(string(char)) { + reverseAndFilteredString = c + reverseAndFilteredString + filteredString += c + } + } + + return reverseAndFilteredString == filteredString +} + +// O(n) time complexity +// O(n) space complexity +func isPalindrome2(s string) bool { + lowerCaseString := strings.ToLower(s) + reverseAndFilteredString := "" + var filteredString strings.Builder + + for _, char := range lowerCaseString { + if unicode.IsLetter(char) || unicode.IsDigit(char) { + reverseAndFilteredString = string(char) + reverseAndFilteredString + filteredString.WriteRune(char) + } + } + + return reverseAndFilteredString == filteredString.String() +}