mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-10 13:37:33 +08:00
Add Weekly 216
This commit is contained in:
@ -0,0 +1,20 @@
|
|||||||
|
package leetcode
|
||||||
|
|
||||||
|
func maximumWealth(accounts [][]int) int {
|
||||||
|
res := 0
|
||||||
|
for i := 0; i < len(accounts); i++ {
|
||||||
|
sum := 0
|
||||||
|
for j := 0; j < len(accounts[i]); j++ {
|
||||||
|
sum += accounts[i][j]
|
||||||
|
}
|
||||||
|
res = max(res, sum)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func max(a int, b int) int {
|
||||||
|
if a > b {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
@ -0,0 +1,87 @@
|
|||||||
|
package leetcode
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 解法一 单调栈
|
||||||
|
func mostCompetitive(nums []int, k int) []int {
|
||||||
|
stack := make([]int, 0, len(nums))
|
||||||
|
for i := 0; i < len(nums); i++ {
|
||||||
|
for len(stack)+len(nums)-i > k && len(stack) > 0 && nums[i] < stack[len(stack)-1] {
|
||||||
|
stack = stack[:len(stack)-1]
|
||||||
|
}
|
||||||
|
stack = append(stack, nums[i])
|
||||||
|
}
|
||||||
|
return stack[:k]
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解法二 DFS 超时
|
||||||
|
func mostCompetitive1(nums []int, k int) []int {
|
||||||
|
c, visited, res := []int{}, map[int]bool{}, []int{}
|
||||||
|
for i := 0; i < len(nums)-1; i++ {
|
||||||
|
if _, ok := visited[nums[i]]; ok {
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
visited[nums[i]] = true
|
||||||
|
generateIncSubsets(nums, i, k, c, &res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateIncSubsets(nums []int, current, k int, c []int, res *[]int) {
|
||||||
|
c = append(c, nums[current])
|
||||||
|
fmt.Printf("c = %v res = %v\n", c, *res)
|
||||||
|
if len(c) > k {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(c) < k && len(*res) != 0 {
|
||||||
|
b, flag := make([]int, len(c)), false
|
||||||
|
copy(b, c)
|
||||||
|
for i := 0; i < len(b); i++ {
|
||||||
|
if b[i] < (*res)[i] {
|
||||||
|
flag = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !flag {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// if len(*res) != 0 && len(c) <= len(*res) && c[len(c)-1] > (*res)[len(c)-1] {
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
if len(c) == k {
|
||||||
|
//fmt.Printf("c = %v\n", c)
|
||||||
|
b, flag := make([]int, len(c)), false
|
||||||
|
copy(b, c)
|
||||||
|
if len(*res) == 0 {
|
||||||
|
*res = b
|
||||||
|
} else {
|
||||||
|
for i := 0; i < len(b); i++ {
|
||||||
|
if b[i] < (*res)[i] {
|
||||||
|
flag = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if flag {
|
||||||
|
*res = b
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Printf("tmp = %v min = %v\n", b, *res)
|
||||||
|
}
|
||||||
|
visited := map[int]bool{}
|
||||||
|
for i := current + 1; i < len(nums); i++ {
|
||||||
|
// if nums[current] <= nums[i] {
|
||||||
|
if _, ok := visited[nums[i]]; ok {
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
visited[nums[i]] = true
|
||||||
|
generateIncSubsets(nums, i, k, c, res)
|
||||||
|
}
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
c = c[:len(c)-1]
|
||||||
|
return
|
||||||
|
}
|
@ -0,0 +1,63 @@
|
|||||||
|
package leetcode
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
type question491 struct {
|
||||||
|
para491
|
||||||
|
ans491
|
||||||
|
}
|
||||||
|
|
||||||
|
// para 是参数
|
||||||
|
// one 代表第一个参数
|
||||||
|
type para491 struct {
|
||||||
|
nums []int
|
||||||
|
k int
|
||||||
|
}
|
||||||
|
|
||||||
|
// ans 是答案
|
||||||
|
// one 代表第一个答案
|
||||||
|
type ans491 struct {
|
||||||
|
one []int
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Problem491(t *testing.T) {
|
||||||
|
|
||||||
|
qs := []question491{
|
||||||
|
|
||||||
|
{
|
||||||
|
para491{[]int{3, 5, 2, 6}, 2},
|
||||||
|
ans491{[]int{2, 6}},
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
para491{[]int{2, 4, 3, 3, 5, 4, 9, 6}, 4},
|
||||||
|
ans491{[]int{2, 3, 3, 4}},
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
para491{[]int{2, 4, 3, 3, 5, 4, 9, 6}, 4},
|
||||||
|
ans491{[]int{2, 3, 3, 4}},
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
para491{[]int{71, 18, 52, 29, 55, 73, 24, 42, 66, 8, 80, 2}, 3},
|
||||||
|
ans491{[]int{8, 80, 2}},
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
para491{[]int{84, 10, 71, 23, 66, 61, 62, 64, 34, 41, 80, 25, 91, 43, 4, 75, 65, 13, 37, 41, 46, 90, 55, 8, 85, 61, 95, 71}, 24},
|
||||||
|
ans491{[]int{10, 23, 61, 62, 34, 41, 80, 25, 91, 43, 4, 75, 65, 13, 37, 41, 46, 90, 55, 8, 85, 61, 95, 71}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("------------------------Leetcode Problem 491------------------------\n")
|
||||||
|
|
||||||
|
for _, q := range qs {
|
||||||
|
_, p := q.ans491, q.para491
|
||||||
|
fmt.Printf("【input】:%v 【output】:%v\n", p, mostCompetitive(p.nums, p.k))
|
||||||
|
}
|
||||||
|
fmt.Printf("\n\n\n")
|
||||||
|
}
|
Reference in New Issue
Block a user