add new solutions
This commit is contained in:
parent
886b5e0a8e
commit
67cad91898
47 changed files with 1549 additions and 1 deletions
33
solutions/7/q714/solution.go
Normal file
33
solutions/7/q714/solution.go
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package q714
|
||||
|
||||
// Note: could be done faster with dynamic programming
|
||||
|
||||
func maxProfit(prices []int, fee int) int {
|
||||
var low, high, profit int
|
||||
bought := false
|
||||
for i := range prices {
|
||||
if bought {
|
||||
high = max(high, prices[i])
|
||||
if high-prices[i] > fee { // should have sold earlier or not bought
|
||||
if high-low > fee {
|
||||
profit += high - low - fee
|
||||
}
|
||||
bought = false
|
||||
} else if prices[i] < low { // regret buying too early
|
||||
low, high = prices[i], prices[i]
|
||||
}
|
||||
}
|
||||
|
||||
if !bought && i < len(prices)-1 && prices[i+1] > prices[i] {
|
||||
bought = true // buy the dip
|
||||
low, high = prices[i], prices[i]
|
||||
}
|
||||
}
|
||||
|
||||
if bought && high-low > fee {
|
||||
profit += high - low - fee
|
||||
}
|
||||
return profit
|
||||
}
|
||||
|
||||
var _ = maxProfit
|
||||
23
solutions/7/q735/solution.go
Normal file
23
solutions/7/q735/solution.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package q735
|
||||
|
||||
func asteroidCollision(asteroids []int) []int {
|
||||
p := 0
|
||||
for _, a := range asteroids {
|
||||
if a < 0 {
|
||||
for p > 0 && asteroids[p-1] > 0 && asteroids[p-1] < -a {
|
||||
p--
|
||||
}
|
||||
if p > 0 && asteroids[p-1] > 0 {
|
||||
if asteroids[p-1] == -a {
|
||||
p--
|
||||
}
|
||||
continue // explode
|
||||
}
|
||||
}
|
||||
asteroids[p] = a
|
||||
p++
|
||||
}
|
||||
return asteroids[:p]
|
||||
}
|
||||
|
||||
var _ = asteroidCollision
|
||||
19
solutions/7/q739/solution.go
Normal file
19
solutions/7/q739/solution.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package q739
|
||||
|
||||
func dailyTemperatures(temperatures []int) []int {
|
||||
waitDays := make([]int, len(temperatures))
|
||||
|
||||
idxStack := make([]int, 0, len(temperatures))
|
||||
for i, temp := range temperatures {
|
||||
for len(idxStack) > 0 && temperatures[idxStack[len(idxStack)-1]] < temp {
|
||||
popped := idxStack[len(idxStack)-1]
|
||||
idxStack = idxStack[:len(idxStack)-1] // pop
|
||||
|
||||
waitDays[popped] = i - popped
|
||||
}
|
||||
idxStack = append(idxStack, i)
|
||||
}
|
||||
return waitDays
|
||||
}
|
||||
|
||||
var _ = dailyTemperatures
|
||||
24
solutions/7/q790/solution.go
Normal file
24
solutions/7/q790/solution.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package q790
|
||||
|
||||
const (
|
||||
TopHalf int8 = iota
|
||||
BottomHalf
|
||||
Full
|
||||
|
||||
Modulo int = 1e9 + 7
|
||||
)
|
||||
|
||||
func numTilings(n int) int {
|
||||
cache := make([][3]int, n+1)
|
||||
cache[0][Full] = 1
|
||||
cache[1][Full] = 1
|
||||
|
||||
for w := 2; w <= n; w++ {
|
||||
cache[w][Full] = (cache[w-1][Full] + cache[w-2][Full] + cache[w-1][TopHalf] + cache[w-1][BottomHalf]) % Modulo
|
||||
cache[w][TopHalf] = (cache[w-1][BottomHalf] + cache[w-2][Full]) % Modulo
|
||||
cache[w][BottomHalf] = (cache[w-1][TopHalf] + cache[w-2][Full]) % Modulo
|
||||
}
|
||||
return cache[n][Full]
|
||||
}
|
||||
|
||||
var _ = numTilings
|
||||
Loading…
Add table
Add a link
Reference in a new issue