From 1a00ff9e4e33d297cda7e7fdc2e9d79f56f70ea8 Mon Sep 17 00:00:00 2001 From: easyone Date: Tue, 10 Dec 2024 07:32:29 +0900 Subject: [PATCH 1/8] Feat: Add solution of contains-duplicate --- contains-duplicate/easyone-jwlee.go | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 contains-duplicate/easyone-jwlee.go diff --git a/contains-duplicate/easyone-jwlee.go b/contains-duplicate/easyone-jwlee.go new file mode 100644 index 000000000..7d552ba43 --- /dev/null +++ b/contains-duplicate/easyone-jwlee.go @@ -0,0 +1,10 @@ +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 +} \ No newline at end of file From 52b09c0541cbc2f1de8a26176ded03cffe92b27a Mon Sep 17 00:00:00 2001 From: easyone Date: Tue, 10 Dec 2024 08:22:19 +0900 Subject: [PATCH 2/8] Feat: Add solution of valid-palindrome --- contains-duplicate/easyone-jwlee.go | 2 +- valid-palindrome/easyone-jwlee.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 valid-palindrome/easyone-jwlee.go diff --git a/contains-duplicate/easyone-jwlee.go b/contains-duplicate/easyone-jwlee.go index 7d552ba43..692163447 100644 --- a/contains-duplicate/easyone-jwlee.go +++ b/contains-duplicate/easyone-jwlee.go @@ -7,4 +7,4 @@ func containsDuplicate(nums []int) bool { m[num] = num } return false -} \ No newline at end of file +} diff --git a/valid-palindrome/easyone-jwlee.go b/valid-palindrome/easyone-jwlee.go new file mode 100644 index 000000000..8b344942a --- /dev/null +++ b/valid-palindrome/easyone-jwlee.go @@ -0,0 +1,20 @@ +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 +} From 15811f5abc1641826b28109a84773225484129f3 Mon Sep 17 00:00:00 2001 From: easyone Date: Tue, 10 Dec 2024 07:32:29 +0900 Subject: [PATCH 3/8] Feat: Add solution of contains-duplicate --- contains-duplicate/easyone-jwlee.go | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 contains-duplicate/easyone-jwlee.go diff --git a/contains-duplicate/easyone-jwlee.go b/contains-duplicate/easyone-jwlee.go new file mode 100644 index 000000000..7d552ba43 --- /dev/null +++ b/contains-duplicate/easyone-jwlee.go @@ -0,0 +1,10 @@ +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 +} \ No newline at end of file From a9dd0c75357b7f0ea2e89bd4527d0dc85dea43d3 Mon Sep 17 00:00:00 2001 From: easyone Date: Tue, 10 Dec 2024 08:22:19 +0900 Subject: [PATCH 4/8] Feat: Add solution of valid-palindrome --- contains-duplicate/easyone-jwlee.go | 2 +- valid-palindrome/easyone-jwlee.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 valid-palindrome/easyone-jwlee.go diff --git a/contains-duplicate/easyone-jwlee.go b/contains-duplicate/easyone-jwlee.go index 7d552ba43..692163447 100644 --- a/contains-duplicate/easyone-jwlee.go +++ b/contains-duplicate/easyone-jwlee.go @@ -7,4 +7,4 @@ func containsDuplicate(nums []int) bool { m[num] = num } return false -} \ No newline at end of file +} diff --git a/valid-palindrome/easyone-jwlee.go b/valid-palindrome/easyone-jwlee.go new file mode 100644 index 000000000..8b344942a --- /dev/null +++ b/valid-palindrome/easyone-jwlee.go @@ -0,0 +1,20 @@ +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 +} From 9bfe355e174c884a03a6cfe07d4a98316b16dc2d Mon Sep 17 00:00:00 2001 From: easyone Date: Wed, 11 Dec 2024 08:06:14 +0900 Subject: [PATCH 5/8] Feat: Add solution of top-k-frequent-elements --- top-k-frequent-elements/easyone-jwlee.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 top-k-frequent-elements/easyone-jwlee.go diff --git a/top-k-frequent-elements/easyone-jwlee.go b/top-k-frequent-elements/easyone-jwlee.go new file mode 100644 index 000000000..5a024515b --- /dev/null +++ b/top-k-frequent-elements/easyone-jwlee.go @@ -0,0 +1,20 @@ +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]...) + } + if len(result) == k { + break + } + } + return result +} From 5fcd5824041a92d4d9d987c7fc28a1d36fc2540c Mon Sep 17 00:00:00 2001 From: easyone Date: Thu, 12 Dec 2024 10:06:24 +0900 Subject: [PATCH 6/8] Feat: Add solution of longest-consecutive-sequence. --- longest-consecutive-sequence/easyone-jwlee.go | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 longest-consecutive-sequence/easyone-jwlee.go diff --git a/longest-consecutive-sequence/easyone-jwlee.go b/longest-consecutive-sequence/easyone-jwlee.go new file mode 100644 index 000000000..87cd429fd --- /dev/null +++ b/longest-consecutive-sequence/easyone-jwlee.go @@ -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 +} From ed398ffb22e91868c5471528c5c6c8d947b6dc3d Mon Sep 17 00:00:00 2001 From: easyone Date: Thu, 12 Dec 2024 10:06:42 +0900 Subject: [PATCH 7/8] Docs: Add comments explaining the solution process. --- contains-duplicate/easyone-jwlee.go | 14 ++++++++++++++ top-k-frequent-elements/easyone-jwlee.go | 11 +++++++++++ valid-palindrome/easyone-jwlee.go | 11 +++++++++++ 3 files changed, 36 insertions(+) diff --git a/contains-duplicate/easyone-jwlee.go b/contains-duplicate/easyone-jwlee.go index 692163447..1dafb7676 100644 --- a/contains-duplicate/easyone-jwlee.go +++ b/contains-duplicate/easyone-jwlee.go @@ -1,3 +1,17 @@ +// 풀이 +// 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 { diff --git a/top-k-frequent-elements/easyone-jwlee.go b/top-k-frequent-elements/easyone-jwlee.go index 5a024515b..f5340e23f 100644 --- a/top-k-frequent-elements/easyone-jwlee.go +++ b/top-k-frequent-elements/easyone-jwlee.go @@ -1,3 +1,14 @@ +// 풀이 +// 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 { diff --git a/valid-palindrome/easyone-jwlee.go b/valid-palindrome/easyone-jwlee.go index 8b344942a..dfa001a88 100644 --- a/valid-palindrome/easyone-jwlee.go +++ b/valid-palindrome/easyone-jwlee.go @@ -1,3 +1,14 @@ +// 풀이 +// 유효한 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 := "" From 0953f2655a99e716287864dae2e5872affd96b4e Mon Sep 17 00:00:00 2001 From: easyone-jwlee Date: Sat, 14 Dec 2024 13:34:45 +0900 Subject: [PATCH 8/8] Feat: Add solution of house-robber --- house-robber/easyone-jwlee.go | 36 +++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 house-robber/easyone-jwlee.go diff --git a/house-robber/easyone-jwlee.go b/house-robber/easyone-jwlee.go new file mode 100644 index 000000000..863974e02 --- /dev/null +++ b/house-robber/easyone-jwlee.go @@ -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 + } +}