吹拉弹唱


  • Home
  • Archive
  • Categories
  • Tags
  • Books
  •  

© 2022 Kleon

Theme Typography by Makito

Proudly published with Hexo

Code - Greedy

Posted at 2022-05-05Updated at 2022-05-05 interview  interview algorithm 

  • LeetCode
    • 860. Lemonade Change
    • 441. Arranging Coins
    • 134. Gas Station
    • 135. Candy
    • 56. Merge Intervals

# LeetCode

# 860. Lemonade Change

leetcode

For $20, use $10+$5 is always better than 3*$5, because $5 is more handy than $10.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
func lemonadeChange(bills []int) bool {
five, ten := 0, 0
for _, bill := range bills {
if bill == 5 {
five++
} else if bill == 10 {
ten++
five--
} else if ten > 0 {
ten--
five--
} else {
five -= 3
}
if five < 0 {
return false
}
}
return true
}

# 441. Arranging Coins

leetcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
func arrangeCoins(n int) int {
if n == 0 {
return 0
}
c := 1
for {
if n < c {
return c - 1
} else if n == c {
return c
}
n-=c
c++
}
}

# 134. Gas Station

leetcode

Brute-Force

TC O(N^2)
SC O(1)

Greedy

TC O(N)
SC O(1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
func canCompleteCircuit(gas []int, cost []int) int {
curSum := 0
totalSum := 0
start := 0
for i := 0; i < len(gas); i++ {
curSum += gas[i] - cost[i]
totalSum += gas[i] - cost[i]
if curSum < 0 {
start = i + 1
curSum = 0
}
}
if totalSum < 0 {
return -1
}
return start
}

# 135. Candy

leetcode

Greedy

Consider from left to right and right to left respectively.

Don’t try to consider left and right simultaneously.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
func candy(ratings []int) int {
candy := make([]int, len(ratings))
for i := range candy {
candy[i]= 1
}

for i := 1; i < len(ratings); i++ {
if ratings[i] > ratings[i - 1] {
candy[i] = candy[i - 1] + 1
}
}
for i := len(ratings) - 2; i >= 0; i-- {
if ratings[i] > ratings[i + 1] {
candy[i] = max(candy[i], candy[i+1]+1)
}
}
result := 0
for i := range candy {
result += candy[i]
}
return result
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

# 56. Merge Intervals

leetcode

Sort first and find the max right interval.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
func merge(intervals [][]int) [][]int {
result := [][]int{}
if len(intervals) == 0 {
return result
}

sort.Slice(intervals, func(i, j int) bool {
return intervals[i][0] < intervals[j][0]
})

flag := false // whether to merge last interval
for i := 1; i < len(intervals); i++ {
start := intervals[i - 1][0]
end := intervals[i - 1][1]
for i < len(intervals) && intervals[i][0] <= end {
end = max(end, intervals[i][1])
if i == len(intervals) - 1 {
flag = true
}
i++
}
result = append(result, []int{start, end})
}
if !flag {
result = append(result, intervals[len(intervals)-1])
}

return result
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

Share 

 Previous post: Code - DFS Next post: Code - Hash Table 

© 2022 Kleon

Theme Typography by Makito

Proudly published with Hexo