mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
Add weekly-contest-214
This commit is contained in:
@ -0,0 +1,21 @@
|
||||
package leetcode
|
||||
|
||||
func getMaximumGenerated(n int) int {
|
||||
if n == 0 {
|
||||
return 0
|
||||
}
|
||||
nums, max := make([]int, n+1), 0
|
||||
nums[0], nums[1] = 0, 1
|
||||
for i := 0; i <= n; i++ {
|
||||
if nums[i] > max {
|
||||
max = nums[i]
|
||||
}
|
||||
if 2*i >= 2 && 2*i <= n {
|
||||
nums[2*i] = nums[i]
|
||||
}
|
||||
if 2*i+1 >= 2 && 2*i+1 <= n {
|
||||
nums[2*i+1] = nums[i] + nums[i+1]
|
||||
}
|
||||
}
|
||||
return max
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question5561 struct {
|
||||
para5561
|
||||
ans5561
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para5561 struct {
|
||||
n int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans5561 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem5561(t *testing.T) {
|
||||
|
||||
qs := []question5561{
|
||||
|
||||
{
|
||||
para5561{7},
|
||||
ans5561{3},
|
||||
},
|
||||
|
||||
{
|
||||
para5561{2},
|
||||
ans5561{1},
|
||||
},
|
||||
|
||||
{
|
||||
para5561{3},
|
||||
ans5561{2},
|
||||
},
|
||||
|
||||
{
|
||||
para5561{0},
|
||||
ans5561{0},
|
||||
},
|
||||
|
||||
{
|
||||
para5561{1},
|
||||
ans5561{1},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 5561------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans5561, q.para5561
|
||||
fmt.Printf("【input】:%v 【output】:%v \n", p, getMaximumGenerated(p.n))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
func minDeletions(s string) int {
|
||||
frequency, res := make([]int, 26), 0
|
||||
for i := 0; i < len(s); i++ {
|
||||
frequency[s[i]-'a']++
|
||||
}
|
||||
sort.Sort(sort.Reverse(sort.IntSlice(frequency)))
|
||||
fmt.Printf("%v\n", frequency)
|
||||
for i := 1; i <= 25; i++ {
|
||||
if frequency[i] == frequency[i-1] && frequency[i] != 0 {
|
||||
res++
|
||||
frequency[i]--
|
||||
sort.Sort(sort.Reverse(sort.IntSlice(frequency)))
|
||||
i--
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question5562 struct {
|
||||
para5562
|
||||
ans5562
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para5562 struct {
|
||||
s string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans5562 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem5562(t *testing.T) {
|
||||
|
||||
qs := []question5562{
|
||||
|
||||
{
|
||||
para5562{"aab"},
|
||||
ans5562{0},
|
||||
},
|
||||
|
||||
{
|
||||
para5562{"aaabbbcc"},
|
||||
ans5562{2},
|
||||
},
|
||||
|
||||
{
|
||||
para5562{"ceabaacb"},
|
||||
ans5562{2},
|
||||
},
|
||||
|
||||
{
|
||||
para5562{""},
|
||||
ans5562{0},
|
||||
},
|
||||
|
||||
{
|
||||
para5562{"abcabc"},
|
||||
ans5562{3},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 5562------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans5562, q.para5562
|
||||
fmt.Printf("【input】:%v 【output】:%v \n", p, minDeletions(p.s))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"container/heap"
|
||||
)
|
||||
|
||||
func maxProfit(inventory []int, orders int) int {
|
||||
res, mod := 0, 1000000007
|
||||
q := PriorityQueue{}
|
||||
for i := 0; i < len(inventory); i++ {
|
||||
heap.Push(&q, &Item{count: inventory[i]})
|
||||
}
|
||||
for ; orders > 0; orders-- {
|
||||
item := heap.Pop(&q).(*Item)
|
||||
res = (res + item.count) % mod
|
||||
heap.Push(&q, &Item{count: item.count - 1})
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// Item define
|
||||
type Item struct {
|
||||
count int
|
||||
}
|
||||
|
||||
// A PriorityQueue implements heap.Interface and holds Items.
|
||||
type PriorityQueue []*Item
|
||||
|
||||
func (pq PriorityQueue) Len() int {
|
||||
return len(pq)
|
||||
}
|
||||
|
||||
func (pq PriorityQueue) Less(i, j int) bool {
|
||||
// 注意:因为golang中的heap是按最小堆组织的,所以count越大,Less()越小,越靠近堆顶.
|
||||
return pq[i].count > pq[j].count
|
||||
}
|
||||
|
||||
func (pq PriorityQueue) Swap(i, j int) {
|
||||
pq[i], pq[j] = pq[j], pq[i]
|
||||
}
|
||||
|
||||
// Push define
|
||||
func (pq *PriorityQueue) Push(x interface{}) {
|
||||
item := x.(*Item)
|
||||
*pq = append(*pq, item)
|
||||
}
|
||||
|
||||
// Pop define
|
||||
func (pq *PriorityQueue) Pop() interface{} {
|
||||
n := len(*pq)
|
||||
item := (*pq)[n-1]
|
||||
*pq = (*pq)[:n-1]
|
||||
return item
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question5563 struct {
|
||||
para5563
|
||||
ans5563
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para5563 struct {
|
||||
inventory []int
|
||||
orders int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans5563 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem5563(t *testing.T) {
|
||||
|
||||
qs := []question5563{
|
||||
|
||||
{
|
||||
para5563{[]int{2, 5}, 4},
|
||||
ans5563{14},
|
||||
},
|
||||
|
||||
{
|
||||
para5563{[]int{3, 5}, 6},
|
||||
ans5563{19},
|
||||
},
|
||||
|
||||
{
|
||||
para5563{[]int{2, 8, 4, 10, 6}, 20},
|
||||
ans5563{110},
|
||||
},
|
||||
|
||||
{
|
||||
para5563{[]int{1000000000}, 1000000000},
|
||||
ans5563{21},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 5563------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans5563, q.para5563
|
||||
fmt.Printf("【input】:%v 【output】:%v \n", p, maxProfit(p.inventory, p.orders))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
Reference in New Issue
Block a user