Skip to content

Commit 15c1278

Browse files
authored
[jinho] Week 1 (#632)
* neverlish: week01 * refactor: optimize result slice initialization in topKFrequent function * docs: add time and space complexity comments to algorithms
1 parent f04849b commit 15c1278

File tree

5 files changed

+213
-0
lines changed

5 files changed

+213
-0
lines changed

contains-duplicate/neverlish.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(n)
3+
4+
package main
5+
6+
import "testing"
7+
8+
func Test(t *testing.T) {
9+
result1 := containsDuplicate([]int{1, 2, 3, 1})
10+
11+
if result1 != true {
12+
t.Fatal("failed test1")
13+
}
14+
15+
result2 := containsDuplicate([]int{1, 2, 3, 4})
16+
17+
if result2 != false {
18+
t.Fatal("failed test2")
19+
}
20+
}
21+
22+
func containsDuplicate(nums []int) bool {
23+
data := make(map[int]bool)
24+
25+
for _, num := range nums {
26+
if data[num] {
27+
return true
28+
} else {
29+
data[num] = true
30+
}
31+
}
32+
return false
33+
}

house-robber/neverlish.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(n)
3+
4+
package main
5+
6+
import (
7+
"testing"
8+
)
9+
10+
func Test(t *testing.T) {
11+
result1 := rob([]int{1, 2, 3, 1})
12+
13+
if result1 != 4 {
14+
t.Fatal("failed test1")
15+
}
16+
17+
result2 := rob([]int{2, 7, 9, 3, 1})
18+
19+
if result2 != 12 {
20+
t.Fatal("failed test2")
21+
}
22+
}
23+
24+
func rob(nums []int) int {
25+
length := len(nums)
26+
27+
if length == 0 {
28+
return 0
29+
}
30+
if length == 1 {
31+
return nums[0]
32+
}
33+
34+
moneys := make([]int, length)
35+
36+
moneys[0] = nums[0]
37+
moneys[1] = max(nums[0], nums[1])
38+
39+
for position := 2; position < length; position++ {
40+
moneys[position] = max(moneys[position-1], moneys[position-2]+nums[position])
41+
}
42+
43+
return moneys[length-1]
44+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(n)
3+
4+
package main
5+
6+
import "testing"
7+
8+
func Test(t *testing.T) {
9+
result := longestConsecutive([]int{100, 4, 200, 1, 3, 2})
10+
11+
if result != 4 {
12+
t.Fatal("failed test")
13+
}
14+
15+
result2 := longestConsecutive([]int{0, 3, 7, 2, 5, 8, 4, 6, 0, 1})
16+
17+
if result2 != 9 {
18+
t.Fatal("failed test2")
19+
}
20+
}
21+
22+
func longestConsecutive(nums []int) int {
23+
numsSet := make(map[int]bool)
24+
25+
for _, num := range nums {
26+
numsSet[num] = true
27+
}
28+
29+
longest := 0
30+
31+
for num := range numsSet {
32+
if !numsSet[num-1] {
33+
currentNum := num
34+
currentLength := 1
35+
36+
for numsSet[currentNum+1] {
37+
currentNum++
38+
currentLength++
39+
}
40+
41+
if currentLength > longest {
42+
longest = currentLength
43+
}
44+
}
45+
}
46+
47+
return longest
48+
}

top-k-frequent-elements/neverlish.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(n)
3+
4+
package main
5+
6+
import "testing"
7+
8+
func Test(t *testing.T) {
9+
result := topKFrequent([]int{1, 1, 1, 2, 2, 3}, 2)
10+
11+
if result[0] != 1 || result[1] != 2 {
12+
t.Fatal("failed test")
13+
}
14+
}
15+
16+
func topKFrequent(nums []int, k int) []int {
17+
freq := make(map[int]int)
18+
19+
for _, num := range nums {
20+
freq[num]++
21+
}
22+
23+
freq_by_counts := make(map[int][]int)
24+
25+
for num, count := range freq {
26+
if _, ok := freq_by_counts[count]; !ok {
27+
freq_by_counts[count] = []int{}
28+
}
29+
freq_by_counts[count] = append(freq_by_counts[count], num)
30+
}
31+
32+
result := make([]int, 0, k)
33+
34+
for count := len(nums); count > 0; count-- {
35+
if nums, ok := freq_by_counts[count]; ok {
36+
if len(result) >= k {
37+
break
38+
}
39+
result = append(result, nums...)
40+
}
41+
}
42+
return result
43+
}

valid-palindrome/neverlish.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(n)
3+
4+
package main
5+
6+
import (
7+
"strings"
8+
"testing"
9+
)
10+
11+
func Test(t *testing.T) {
12+
result1 := isPalindrome("A man, a plan, a canal: Panama")
13+
if result1 != true {
14+
t.Fatal("failed test1")
15+
}
16+
17+
result2 := isPalindrome("race a car")
18+
if result2 != false {
19+
t.Fatal("failed test2")
20+
}
21+
22+
result3 := isPalindrome("")
23+
24+
if result3 != true {
25+
t.Fatal("failed test3")
26+
}
27+
}
28+
29+
func isPalindrome(s string) bool {
30+
s = strings.ToLower(s)
31+
32+
var filtered []rune
33+
for _, r := range s {
34+
if ('a' <= r && r <= 'z') || ('0' <= r && r <= '9') {
35+
filtered = append(filtered, r)
36+
}
37+
}
38+
39+
for index, r := range filtered[:len(filtered)/2] {
40+
if r != filtered[len(filtered)-index-1] {
41+
return false
42+
}
43+
}
44+
return true
45+
}

0 commit comments

Comments
 (0)