Skip to content

Commit ea38a91

Browse files
committed
Add weekly-contest-214 solution
1 parent f3888ff commit ea38a91

20 files changed

+1407
-184
lines changed
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 question1646 struct {
9+
para1646
10+
ans1646
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1646 struct {
16+
n int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans1646 struct {
22+
one int
23+
}
24+
25+
func Test_Problem1646(t *testing.T) {
26+
27+
qs := []question1646{
28+
29+
{
30+
para1646{7},
31+
ans1646{3},
32+
},
33+
34+
{
35+
para1646{2},
36+
ans1646{1},
37+
},
38+
39+
{
40+
para1646{3},
41+
ans1646{2},
42+
},
43+
44+
{
45+
para1646{0},
46+
ans1646{0},
47+
},
48+
49+
{
50+
para1646{1},
51+
ans1646{1},
52+
},
53+
}
54+
55+
fmt.Printf("------------------------Leetcode Problem 1646------------------------\n")
56+
57+
for _, q := range qs {
58+
_, p := q.ans1646, q.para1646
59+
fmt.Printf("【input】:%v 【output】:%v \n", p, getMaximumGenerated(p.n))
60+
}
61+
fmt.Printf("\n\n\n")
62+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# [1646. Get Maximum in Generated Array](https://leetcode.com/problems/get-maximum-in-generated-array/)
2+
3+
4+
## 题目
5+
6+
You are given an integer `n`. An array `nums` of length `n + 1` is generated in the following way:
7+
8+
- `nums[0] = 0`
9+
- `nums[1] = 1`
10+
- `nums[2 * i] = nums[i]` when `2 <= 2 * i <= n`
11+
- `nums[2 * i + 1] = nums[i] + nums[i + 1]` when `2 <= 2 * i + 1 <= n`
12+
13+
Return *****the **maximum** integer in the array* `nums`.
14+
15+
**Example 1:**
16+
17+
```
18+
Input: n = 7
19+
Output: 3
20+
Explanation: According to the given rules:
21+
nums[0] = 0
22+
nums[1] = 1
23+
nums[(1 * 2) = 2] = nums[1] = 1
24+
nums[(1 * 2) + 1 = 3] = nums[1] + nums[2] = 1 + 1 = 2
25+
nums[(2 * 2) = 4] = nums[2] = 1
26+
nums[(2 * 2) + 1 = 5] = nums[2] + nums[3] = 1 + 2 = 3
27+
nums[(3 * 2) = 6] = nums[3] = 2
28+
nums[(3 * 2) + 1 = 7] = nums[3] + nums[4] = 2 + 1 = 3
29+
Hence, nums = [0,1,1,2,1,3,2,3], and the maximum is 3.
30+
31+
```
32+
33+
**Example 2:**
34+
35+
```
36+
Input: n = 2
37+
Output: 1
38+
Explanation: According to the given rules, the maximum between nums[0], nums[1], and nums[2] is 1.
39+
40+
```
41+
42+
**Example 3:**
43+
44+
```
45+
Input: n = 3
46+
Output: 2
47+
Explanation: According to the given rules, the maximum between nums[0], nums[1], nums[2], and nums[3] is 2.
48+
49+
```
50+
51+
**Constraints:**
52+
53+
- `0 <= n <= 100`
54+
55+
## 题目大意
56+
57+
给你一个整数 n 。按下述规则生成一个长度为 n + 1 的数组 nums :
58+
59+
- nums[0] = 0
60+
- nums[1] = 1
61+
- 当 2 <= 2 * i <= n 时,nums[2 * i] = nums[i]
62+
- 当 2 <= 2 * i + 1 <= n 时,nums[2 * i + 1] = nums[i] + nums[i + 1]
63+
64+
返回生成数组 nums 中的 最大值。
65+
66+
## 解题思路
67+
68+
- 给出一个 n + 1 的数组,并按照生成规则生成这个数组,求出这个数组中的最大值。
69+
- 简单题,按照题意生成数组,边生成边记录和更新最大值即可。
70+
- 注意边界条件,当 n 为 0 的时候,数组里面只有一个元素 0 。
71+
72+
## 代码
73+
74+
```go
75+
package leetcode
76+
77+
func getMaximumGenerated(n int) int {
78+
if n == 0 {
79+
return 0
80+
}
81+
nums, max := make([]int, n+1), 0
82+
nums[0], nums[1] = 0, 1
83+
for i := 0; i <= n; i++ {
84+
if nums[i] > max {
85+
max = nums[i]
86+
}
87+
if 2*i >= 2 && 2*i <= n {
88+
nums[2*i] = nums[i]
89+
}
90+
if 2*i+1 >= 2 && 2*i+1 <= n {
91+
nums[2*i+1] = nums[i] + nums[i+1]
92+
}
93+
}
94+
return max
95+
}
96+
```
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package leetcode
22

33
import (
4-
"fmt"
54
"sort"
65
)
76

@@ -11,7 +10,6 @@ func minDeletions(s string) int {
1110
frequency[s[i]-'a']++
1211
}
1312
sort.Sort(sort.Reverse(sort.IntSlice(frequency)))
14-
fmt.Printf("%v\n", frequency)
1513
for i := 1; i <= 25; i++ {
1614
if frequency[i] == frequency[i-1] && frequency[i] != 0 {
1715
res++
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 question1647 struct {
9+
para1647
10+
ans1647
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1647 struct {
16+
s string
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans1647 struct {
22+
one int
23+
}
24+
25+
func Test_Problem1647(t *testing.T) {
26+
27+
qs := []question1647{
28+
29+
{
30+
para1647{"aab"},
31+
ans1647{0},
32+
},
33+
34+
{
35+
para1647{"aaabbbcc"},
36+
ans1647{2},
37+
},
38+
39+
{
40+
para1647{"ceabaacb"},
41+
ans1647{2},
42+
},
43+
44+
{
45+
para1647{""},
46+
ans1647{0},
47+
},
48+
49+
{
50+
para1647{"abcabc"},
51+
ans1647{3},
52+
},
53+
}
54+
55+
fmt.Printf("------------------------Leetcode Problem 1647------------------------\n")
56+
57+
for _, q := range qs {
58+
_, p := q.ans1647, q.para1647
59+
fmt.Printf("【input】:%v 【output】:%v \n", p, minDeletions(p.s))
60+
}
61+
fmt.Printf("\n\n\n")
62+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# [1647. Minimum Deletions to Make Character Frequencies Unique](https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/)
2+
3+
4+
## 题目
5+
6+
A string `s` is called **good** if there are no two different characters in `s` that have the same **frequency**.
7+
8+
Given a string `s`, return *the **minimum** number of characters you need to delete to make* `s` ***good**.*
9+
10+
The **frequency** of a character in a string is the number of times it appears in the string. For example, in the string `"aab"`, the **frequency** of `'a'` is `2`, while the **frequency** of `'b'` is `1`.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: s = "aab"
16+
Output: 0
17+
Explanation: s is already good.
18+
19+
```
20+
21+
**Example 2:**
22+
23+
```
24+
Input: s = "aaabbbcc"
25+
Output: 2
26+
Explanation: You can delete two 'b's resulting in the good string "aaabcc".
27+
Another way it to delete one 'b' and one 'c' resulting in the good string "aaabbc".
28+
```
29+
30+
**Example 3:**
31+
32+
```
33+
Input: s = "ceabaacb"
34+
Output: 2
35+
Explanation: You can delete both 'c's resulting in the good string "eabaab".
36+
Note that we only care about characters that are still in the string at the end (i.e. frequency of 0 is ignored).
37+
38+
```
39+
40+
**Constraints:**
41+
42+
- `1 <= s.length <= 105`
43+
- `s` contains only lowercase English letters.
44+
45+
## 题目大意
46+
47+
如果字符串 s 中 不存在 两个不同字符 频次 相同的情况,就称 s 是 优质字符串 。
48+
49+
给你一个字符串 s,返回使 s 成为优质字符串需要删除的最小字符数。
50+
51+
字符串中字符的 频次 是该字符在字符串中的出现次数。例如,在字符串 "aab" 中,'a' 的频次是 2,而 'b' 的频次是 1 。
52+
53+
**提示:**
54+
55+
- `1 <= s.length <= 105`
56+
- `s` 仅含小写英文字母
57+
58+
## 解题思路
59+
60+
- 给出一个字符串 s,要求输出使 s 变成“优质字符串”需要删除的最小字符数。“优质字符串”的定义是:字符串 s 中不存在频次相同的两个不同字符。
61+
- 首先将 26 个字母在字符串中的频次分别统计出来,然后把频次从大到小排列,从频次大的开始,依次调整:例如,假设前一个和后一个频次相等,就把前一个字符删除一个,频次减一,再次排序,如果频次还相等,继续调整,如果频次不同了,游标往后移,继续调整后面的频次。直到所有的频次都不同了,就可以输出最终结果了。
62+
- 这里需要注意频次为 0 的情况,即字母都被删光了。频次为 0 以后,就不需要再比较了。
63+
64+
## 代码
65+
66+
```go
67+
package leetcode
68+
69+
import (
70+
"sort"
71+
)
72+
73+
func minDeletions(s string) int {
74+
frequency, res := make([]int, 26), 0
75+
for i := 0; i < len(s); i++ {
76+
frequency[s[i]-'a']++
77+
}
78+
sort.Sort(sort.Reverse(sort.IntSlice(frequency)))
79+
for i := 1; i <= 25; i++ {
80+
if frequency[i] == frequency[i-1] && frequency[i] != 0 {
81+
res++
82+
frequency[i]--
83+
sort.Sort(sort.Reverse(sort.IntSlice(frequency)))
84+
i--
85+
}
86+
}
87+
return res
88+
}
89+
```

leetcode/5563.Sell-Diminishing-Valued-Colored-Balls/5563. Sell Diminishing-Valued Colored Balls.go renamed to leetcode/1648.Sell-Diminishing-Valued-Colored-Balls/1648. Sell Diminishing-Valued Colored Balls.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,55 @@ import (
44
"container/heap"
55
)
66

7+
// 解法一 贪心 + 二分搜索
78
func maxProfit(inventory []int, orders int) int {
9+
maxItem, thresholdValue, count, res, mod := 0, -1, 0, 0, 1000000007
10+
for i := 0; i < len(inventory); i++ {
11+
if inventory[i] > maxItem {
12+
maxItem = inventory[i]
13+
}
14+
}
15+
low, high := 0, maxItem
16+
for low <= high {
17+
mid := low + ((high - low) >> 1)
18+
for i := 0; i < len(inventory); i++ {
19+
count += max(inventory[i]-mid, 0)
20+
}
21+
if count <= orders {
22+
thresholdValue = mid
23+
high = mid - 1
24+
} else {
25+
low = mid + 1
26+
}
27+
count = 0
28+
}
29+
count = 0
30+
for i := 0; i < len(inventory); i++ {
31+
count += max(inventory[i]-thresholdValue, 0)
32+
}
33+
count = orders - count
34+
for i := 0; i < len(inventory); i++ {
35+
if inventory[i] >= thresholdValue {
36+
if count > 0 {
37+
res += (thresholdValue + inventory[i]) * (inventory[i] - thresholdValue + 1) / 2
38+
count--
39+
} else {
40+
res += (thresholdValue + 1 + inventory[i]) * (inventory[i] - thresholdValue) / 2
41+
}
42+
}
43+
}
44+
return res % mod
45+
}
46+
47+
func max(a int, b int) int {
48+
if a > b {
49+
return a
50+
}
51+
return b
52+
}
53+
54+
// 解法二 优先队列,超时!
55+
func maxProfit_(inventory []int, orders int) int {
856
res, mod := 0, 1000000007
957
q := PriorityQueue{}
1058
for i := 0; i < len(inventory); i++ {

0 commit comments

Comments
 (0)