Skip to content

Commit f3888ff

Browse files
committed
Add weekly-contest-214
1 parent 6d2472d commit f3888ff

File tree

7 files changed

+282
-0
lines changed

7 files changed

+282
-0
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"go.formatFlags": [
44
"-s"
55
],
6+
"go.autocompleteUnimportedPackages": true,
67
"[go]": {
78
"editor.insertSpaces": false,
89
"editor.formatOnSave": true,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package leetcode
2+
3+
func getMaximumGenerated(n int) int {
4+
if n == 0 {
5+
return 0
6+
}
7+
nums, max := make([]int, n+1), 0
8+
nums[0], nums[1] = 0, 1
9+
for i := 0; i <= n; i++ {
10+
if nums[i] > max {
11+
max = nums[i]
12+
}
13+
if 2*i >= 2 && 2*i <= n {
14+
nums[2*i] = nums[i]
15+
}
16+
if 2*i+1 >= 2 && 2*i+1 <= n {
17+
nums[2*i+1] = nums[i] + nums[i+1]
18+
}
19+
}
20+
return max
21+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question5561 struct {
9+
para5561
10+
ans5561
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para5561 struct {
16+
n int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans5561 struct {
22+
one int
23+
}
24+
25+
func Test_Problem5561(t *testing.T) {
26+
27+
qs := []question5561{
28+
29+
{
30+
para5561{7},
31+
ans5561{3},
32+
},
33+
34+
{
35+
para5561{2},
36+
ans5561{1},
37+
},
38+
39+
{
40+
para5561{3},
41+
ans5561{2},
42+
},
43+
44+
{
45+
para5561{0},
46+
ans5561{0},
47+
},
48+
49+
{
50+
para5561{1},
51+
ans5561{1},
52+
},
53+
}
54+
55+
fmt.Printf("------------------------Leetcode Problem 5561------------------------\n")
56+
57+
for _, q := range qs {
58+
_, p := q.ans5561, q.para5561
59+
fmt.Printf("【input】:%v 【output】:%v \n", p, getMaximumGenerated(p.n))
60+
}
61+
fmt.Printf("\n\n\n")
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
)
7+
8+
func minDeletions(s string) int {
9+
frequency, res := make([]int, 26), 0
10+
for i := 0; i < len(s); i++ {
11+
frequency[s[i]-'a']++
12+
}
13+
sort.Sort(sort.Reverse(sort.IntSlice(frequency)))
14+
fmt.Printf("%v\n", frequency)
15+
for i := 1; i <= 25; i++ {
16+
if frequency[i] == frequency[i-1] && frequency[i] != 0 {
17+
res++
18+
frequency[i]--
19+
sort.Sort(sort.Reverse(sort.IntSlice(frequency)))
20+
i--
21+
}
22+
}
23+
return res
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question5562 struct {
9+
para5562
10+
ans5562
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para5562 struct {
16+
s string
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans5562 struct {
22+
one int
23+
}
24+
25+
func Test_Problem5562(t *testing.T) {
26+
27+
qs := []question5562{
28+
29+
{
30+
para5562{"aab"},
31+
ans5562{0},
32+
},
33+
34+
{
35+
para5562{"aaabbbcc"},
36+
ans5562{2},
37+
},
38+
39+
{
40+
para5562{"ceabaacb"},
41+
ans5562{2},
42+
},
43+
44+
{
45+
para5562{""},
46+
ans5562{0},
47+
},
48+
49+
{
50+
para5562{"abcabc"},
51+
ans5562{3},
52+
},
53+
}
54+
55+
fmt.Printf("------------------------Leetcode Problem 5562------------------------\n")
56+
57+
for _, q := range qs {
58+
_, p := q.ans5562, q.para5562
59+
fmt.Printf("【input】:%v 【output】:%v \n", p, minDeletions(p.s))
60+
}
61+
fmt.Printf("\n\n\n")
62+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package leetcode
2+
3+
import (
4+
"container/heap"
5+
)
6+
7+
func maxProfit(inventory []int, orders int) int {
8+
res, mod := 0, 1000000007
9+
q := PriorityQueue{}
10+
for i := 0; i < len(inventory); i++ {
11+
heap.Push(&q, &Item{count: inventory[i]})
12+
}
13+
for ; orders > 0; orders-- {
14+
item := heap.Pop(&q).(*Item)
15+
res = (res + item.count) % mod
16+
heap.Push(&q, &Item{count: item.count - 1})
17+
}
18+
return res
19+
}
20+
21+
// Item define
22+
type Item struct {
23+
count int
24+
}
25+
26+
// A PriorityQueue implements heap.Interface and holds Items.
27+
type PriorityQueue []*Item
28+
29+
func (pq PriorityQueue) Len() int {
30+
return len(pq)
31+
}
32+
33+
func (pq PriorityQueue) Less(i, j int) bool {
34+
// 注意:因为golang中的heap是按最小堆组织的,所以count越大,Less()越小,越靠近堆顶.
35+
return pq[i].count > pq[j].count
36+
}
37+
38+
func (pq PriorityQueue) Swap(i, j int) {
39+
pq[i], pq[j] = pq[j], pq[i]
40+
}
41+
42+
// Push define
43+
func (pq *PriorityQueue) Push(x interface{}) {
44+
item := x.(*Item)
45+
*pq = append(*pq, item)
46+
}
47+
48+
// Pop define
49+
func (pq *PriorityQueue) Pop() interface{} {
50+
n := len(*pq)
51+
item := (*pq)[n-1]
52+
*pq = (*pq)[:n-1]
53+
return item
54+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question5563 struct {
9+
para5563
10+
ans5563
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para5563 struct {
16+
inventory []int
17+
orders int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans5563 struct {
23+
one int
24+
}
25+
26+
func Test_Problem5563(t *testing.T) {
27+
28+
qs := []question5563{
29+
30+
{
31+
para5563{[]int{2, 5}, 4},
32+
ans5563{14},
33+
},
34+
35+
{
36+
para5563{[]int{3, 5}, 6},
37+
ans5563{19},
38+
},
39+
40+
{
41+
para5563{[]int{2, 8, 4, 10, 6}, 20},
42+
ans5563{110},
43+
},
44+
45+
{
46+
para5563{[]int{1000000000}, 1000000000},
47+
ans5563{21},
48+
},
49+
}
50+
51+
fmt.Printf("------------------------Leetcode Problem 5563------------------------\n")
52+
53+
for _, q := range qs {
54+
_, p := q.ans5563, q.para5563
55+
fmt.Printf("【input】:%v 【output】:%v \n", p, maxProfit(p.inventory, p.orders))
56+
}
57+
fmt.Printf("\n\n\n")
58+
}

0 commit comments

Comments
 (0)