Skip to content

Commit 6cbe69c

Browse files
authored
Merge pull request #1005 from neverlish/w9
[jinho] week 9
2 parents 3bcb24f + fd7f47f commit 6cbe69c

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(1)
3+
4+
func findMin(nums []int) int {
5+
result := nums[0]
6+
7+
for i := 1; i < len(nums)-1; i++ {
8+
if nums[i] < nums[i-1] {
9+
result = nums[i]
10+
}
11+
}
12+
return result
13+
14+
}

linked-list-cycle/neverlish.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(n)
3+
4+
func hasCycle(head *ListNode) bool {
5+
visited := make(map[*ListNode]bool)
6+
7+
for head != nil {
8+
if visited[head] {
9+
return true
10+
}
11+
visited[head] = true
12+
head = head.Next
13+
}
14+
return false
15+
}

maximum-product-subarray/neverlish.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(1)
3+
4+
func maxProduct(nums []int) int {
5+
result, max, min := nums[0], 1, 1
6+
7+
for _, num := range nums {
8+
candidates := []int{max * num, min * num, num}
9+
max = maxIntIn3(candidates[0], candidates[1], candidates[2])
10+
min = minIntIn3(candidates[0], candidates[1], candidates[2])
11+
if max > result {
12+
result = max
13+
}
14+
}
15+
16+
return result
17+
}
18+
19+
func maxIntIn3(a, b, c int) int {
20+
if a > b && a > c {
21+
return a
22+
}
23+
if b > c {
24+
return b
25+
}
26+
return c
27+
}
28+
29+
func minIntIn3(a, b, c int) int {
30+
if a < b && a < c {
31+
return a
32+
}
33+
if b < c {
34+
return b
35+
}
36+
return c
37+
}

0 commit comments

Comments
 (0)