Skip to content

Commit adaf03b

Browse files
committed
Add Weekly 216
1 parent 1d5b343 commit adaf03b

File tree

4 files changed

+170
-0
lines changed

4 files changed

+170
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package leetcode
2+
3+
func maximumWealth(accounts [][]int) int {
4+
res := 0
5+
for i := 0; i < len(accounts); i++ {
6+
sum := 0
7+
for j := 0; j < len(accounts[i]); j++ {
8+
sum += accounts[i][j]
9+
}
10+
res = max(res, sum)
11+
}
12+
return res
13+
}
14+
15+
func max(a int, b int) int {
16+
if a > b {
17+
return a
18+
}
19+
return b
20+
}

leetcode/5613. Richest Customer Wealth/5613. Richest Customer Wealth_test.go

Whitespace-only changes.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// 解法一 单调栈
8+
func mostCompetitive(nums []int, k int) []int {
9+
stack := make([]int, 0, len(nums))
10+
for i := 0; i < len(nums); i++ {
11+
for len(stack)+len(nums)-i > k && len(stack) > 0 && nums[i] < stack[len(stack)-1] {
12+
stack = stack[:len(stack)-1]
13+
}
14+
stack = append(stack, nums[i])
15+
}
16+
return stack[:k]
17+
}
18+
19+
// 解法二 DFS 超时
20+
func mostCompetitive1(nums []int, k int) []int {
21+
c, visited, res := []int{}, map[int]bool{}, []int{}
22+
for i := 0; i < len(nums)-1; i++ {
23+
if _, ok := visited[nums[i]]; ok {
24+
continue
25+
} else {
26+
visited[nums[i]] = true
27+
generateIncSubsets(nums, i, k, c, &res)
28+
}
29+
}
30+
return res
31+
}
32+
33+
func generateIncSubsets(nums []int, current, k int, c []int, res *[]int) {
34+
c = append(c, nums[current])
35+
fmt.Printf("c = %v res = %v\n", c, *res)
36+
if len(c) > k {
37+
return
38+
}
39+
if len(c) < k && len(*res) != 0 {
40+
b, flag := make([]int, len(c)), false
41+
copy(b, c)
42+
for i := 0; i < len(b); i++ {
43+
if b[i] < (*res)[i] {
44+
flag = true
45+
break
46+
}
47+
}
48+
if !flag {
49+
return
50+
}
51+
}
52+
// if len(*res) != 0 && len(c) <= len(*res) && c[len(c)-1] > (*res)[len(c)-1] {
53+
// return
54+
// }
55+
if len(c) == k {
56+
//fmt.Printf("c = %v\n", c)
57+
b, flag := make([]int, len(c)), false
58+
copy(b, c)
59+
if len(*res) == 0 {
60+
*res = b
61+
} else {
62+
for i := 0; i < len(b); i++ {
63+
if b[i] < (*res)[i] {
64+
flag = true
65+
break
66+
}
67+
}
68+
if flag {
69+
*res = b
70+
}
71+
}
72+
fmt.Printf("tmp = %v min = %v\n", b, *res)
73+
}
74+
visited := map[int]bool{}
75+
for i := current + 1; i < len(nums); i++ {
76+
// if nums[current] <= nums[i] {
77+
if _, ok := visited[nums[i]]; ok {
78+
continue
79+
} else {
80+
visited[nums[i]] = true
81+
generateIncSubsets(nums, i, k, c, res)
82+
}
83+
//}
84+
}
85+
c = c[:len(c)-1]
86+
return
87+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question491 struct {
9+
para491
10+
ans491
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para491 struct {
16+
nums []int
17+
k int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans491 struct {
23+
one []int
24+
}
25+
26+
func Test_Problem491(t *testing.T) {
27+
28+
qs := []question491{
29+
30+
{
31+
para491{[]int{3, 5, 2, 6}, 2},
32+
ans491{[]int{2, 6}},
33+
},
34+
35+
{
36+
para491{[]int{2, 4, 3, 3, 5, 4, 9, 6}, 4},
37+
ans491{[]int{2, 3, 3, 4}},
38+
},
39+
40+
{
41+
para491{[]int{2, 4, 3, 3, 5, 4, 9, 6}, 4},
42+
ans491{[]int{2, 3, 3, 4}},
43+
},
44+
45+
{
46+
para491{[]int{71, 18, 52, 29, 55, 73, 24, 42, 66, 8, 80, 2}, 3},
47+
ans491{[]int{8, 80, 2}},
48+
},
49+
50+
{
51+
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},
52+
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}},
53+
},
54+
}
55+
56+
fmt.Printf("------------------------Leetcode Problem 491------------------------\n")
57+
58+
for _, q := range qs {
59+
_, p := q.ans491, q.para491
60+
fmt.Printf("【input】:%v 【output】:%v\n", p, mostCompetitive(p.nums, p.k))
61+
}
62+
fmt.Printf("\n\n\n")
63+
}

0 commit comments

Comments
 (0)