Skip to content

Commit 7c6a8bd

Browse files
committed
Add Weekly Contest 218
1 parent 3881a5a commit 7c6a8bd

21 files changed

+1076
-222
lines changed

leetcode/5617.Goal-Parser-Interpretation/5617. Goal Parser Interpretation.go renamed to leetcode/1678.Goal-Parser-Interpretation/1678. Goal Parser Interpretation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func interpret(command string) string {
1414
i += 3
1515
} else {
1616
res += "o"
17-
i += 1
17+
i++
1818
}
1919
}
2020
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1678 struct {
9+
para1678
10+
ans1678
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1678 struct {
16+
command string
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans1678 struct {
22+
one string
23+
}
24+
25+
func Test_Problem1678(t *testing.T) {
26+
27+
qs := []question1678{
28+
29+
{
30+
para1678{"G()(al)"},
31+
ans1678{"Goal"},
32+
},
33+
34+
{
35+
para1678{"G()()()()(al)"},
36+
ans1678{"Gooooal"},
37+
},
38+
39+
{
40+
para1678{"(al)G(al)()()G"},
41+
ans1678{"alGalooG"},
42+
},
43+
}
44+
45+
fmt.Printf("------------------------Leetcode Problem 1678------------------------\n")
46+
47+
for _, q := range qs {
48+
_, p := q.ans1678, q.para1678
49+
fmt.Printf("【input】:%v 【output】:%v\n", p, interpret(p.command))
50+
}
51+
fmt.Printf("\n\n\n")
52+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# [1678. Goal Parser Interpretation](https://leetcode.com/problems/goal-parser-interpretation/)
2+
3+
## 题目
4+
5+
You own a **Goal Parser** that can interpret a string `command`. The `command` consists of an alphabet of `"G"``"()"` and/or `"(al)"` in some order. The Goal Parser will interpret `"G"` as the string `"G"``"()"` as the string `"o"`, and `"(al)"` as the string `"al"`. The interpreted strings are then concatenated in the original order.
6+
7+
Given the string `command`, return *the **Goal Parser**'s interpretation of* `command`.
8+
9+
**Example 1:**
10+
11+
```
12+
Input: command = "G()(al)"
13+
Output: "Goal"
14+
Explanation: The Goal Parser interprets the command as follows:
15+
G -> G
16+
() -> o
17+
(al) -> al
18+
The final concatenated result is "Goal".
19+
```
20+
21+
**Example 2:**
22+
23+
```
24+
Input: command = "G()()()()(al)"
25+
Output: "Gooooal"
26+
```
27+
28+
**Example 3:**
29+
30+
```
31+
Input: command = "(al)G(al)()()G"
32+
Output: "alGalooG"
33+
```
34+
35+
**Constraints:**
36+
37+
- `1 <= command.length <= 100`
38+
- `command` consists of `"G"``"()"`, and/or `"(al)"` in some order.
39+
40+
## 题目大意
41+
42+
请你设计一个可以解释字符串 command 的 Goal 解析器 。command 由 "G"、"()" 和/或 "(al)" 按某种顺序组成。Goal 解析器会将 "G" 解释为字符串 "G"、"()" 解释为字符串 "o" ,"(al)" 解释为字符串 "al" 。然后,按原顺序将经解释得到的字符串连接成一个字符串。给你字符串 command ,返回 Goal 解析器 对 command 的解释结果。
43+
44+
## 解题思路
45+
46+
- 简单题,按照题意修改字符串即可。由于是简单题,这一题也不用考虑嵌套的情况。
47+
48+
## 代码
49+
50+
```go
51+
package leetcode
52+
53+
func interpret(command string) string {
54+
if command == "" {
55+
return ""
56+
}
57+
res := ""
58+
for i := 0; i < len(command); i++ {
59+
if command[i] == 'G' {
60+
res += "G"
61+
} else {
62+
if command[i] == '(' && command[i+1] == 'a' {
63+
res += "al"
64+
i += 3
65+
} else {
66+
res += "o"
67+
i ++
68+
}
69+
}
70+
}
71+
return res
72+
}
73+
```
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package leetcode
2+
3+
// 解法一 优化版
4+
func maxOperations(nums []int, k int) int {
5+
counter, res := make(map[int]int), 0
6+
for _, n := range nums {
7+
counter[n]++
8+
}
9+
if (k & 1) == 0 {
10+
res += counter[k>>1] >> 1
11+
// 能够由 2 个相同的数构成 k 的组合已经都排除出去了,剩下的一个单独的也不能组成 k 了
12+
// 所以这里要把它的频次置为 0 。如果这里不置为 0,下面代码判断逻辑还需要考虑重复使用数字的情况
13+
counter[k>>1] = 0
14+
}
15+
for num, freq := range counter {
16+
if num <= k/2 {
17+
remain := k - num
18+
if counter[remain] < freq {
19+
res += counter[remain]
20+
} else {
21+
res += freq
22+
}
23+
}
24+
}
25+
return res
26+
}
27+
28+
// 解法二
29+
func maxOperations_(nums []int, k int) int {
30+
counter, res := make(map[int]int), 0
31+
for _, num := range nums {
32+
counter[num]++
33+
remain := k - num
34+
if num == remain {
35+
if counter[num] >= 2 {
36+
res++
37+
counter[num] -= 2
38+
}
39+
} else {
40+
if counter[remain] > 0 {
41+
res++
42+
counter[remain]--
43+
counter[num]--
44+
}
45+
}
46+
}
47+
return res
48+
}
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 question1679 struct {
9+
para1679
10+
ans1679
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1679 struct {
16+
nums []int
17+
k int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans1679 struct {
23+
one int
24+
}
25+
26+
func Test_Problem1679(t *testing.T) {
27+
28+
qs := []question1679{
29+
30+
{
31+
para1679{[]int{1, 2, 3, 4}, 5},
32+
ans1679{2},
33+
},
34+
35+
{
36+
para1679{[]int{3, 1, 3, 4, 3}, 6},
37+
ans1679{1},
38+
},
39+
40+
{
41+
para1679{[]int{2, 5, 4, 4, 1, 3, 4, 4, 1, 4, 4, 1, 2, 1, 2, 2, 3, 2, 4, 2}, 3},
42+
ans1679{4},
43+
},
44+
45+
{
46+
para1679{[]int{2, 5, 5, 5, 1, 3, 4, 4, 1, 4, 4, 1, 3, 1, 3, 1, 3, 2, 4, 2}, 6},
47+
ans1679{8},
48+
},
49+
}
50+
51+
fmt.Printf("------------------------Leetcode Problem 1679------------------------\n")
52+
53+
for _, q := range qs {
54+
_, p := q.ans1679, q.para1679
55+
fmt.Printf("【input】:%v 【output】:%v\n", p, maxOperations(p.nums, p.k))
56+
}
57+
fmt.Printf("\n\n\n")
58+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# [1679. Max Number of K-Sum Pairs](https://leetcode.com/problems/max-number-of-k-sum-pairs/)
2+
3+
4+
## 题目
5+
6+
You are given an integer array `nums` and an integer `k`.
7+
8+
In one operation, you can pick two numbers from the array whose sum equals `k` and remove them from the array.
9+
10+
Return *the maximum number of operations you can perform on the array*.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: nums = [1,2,3,4], k = 5
16+
Output: 2
17+
Explanation: Starting with nums = [1,2,3,4]:
18+
- Remove numbers 1 and 4, then nums = [2,3]
19+
- Remove numbers 2 and 3, then nums = []
20+
There are no more pairs that sum up to 5, hence a total of 2 operations.
21+
```
22+
23+
**Example 2:**
24+
25+
```
26+
Input: nums = [3,1,3,4,3], k = 6
27+
Output: 1
28+
Explanation: Starting with nums = [3,1,3,4,3]:
29+
- Remove the first two 3's, then nums = [1,4,3]
30+
There are no more pairs that sum up to 6, hence a total of 1 operation.
31+
```
32+
33+
**Constraints:**
34+
35+
- `1 <= nums.length <= 105`
36+
- `1 <= nums[i] <= 109`
37+
- `1 <= k <= 109`
38+
39+
## 题目大意
40+
41+
给你一个整数数组 nums 和一个整数 k 。每一步操作中,你需要从数组中选出和为 k 的两个整数,并将它们移出数组。返回你可以对数组执行的最大操作数。
42+
43+
## 解题思路
44+
45+
- 读完题第一感觉这道题是 TWO SUM 题目的加强版。需要找到所有满足和是 k 的数对。先考虑能不能找到两个数都是 k/2 ,如果能找到多个这样的数,可以先移除他们。其次在利用 TWO SUM 的思路,找出和为 k 的数对。利用 TWO SUM 里面 map 的做法,时间复杂度 O(n)。
46+
47+
## 代码
48+
49+
```go
50+
package leetcode
51+
52+
// 解法一 优化版
53+
func maxOperations(nums []int, k int) int {
54+
counter, res := make(map[int]int), 0
55+
for _, n := range nums {
56+
counter[n]++
57+
}
58+
if (k & 1) == 0 {
59+
res += counter[k>>1] >> 1
60+
// 能够由 2 个相同的数构成 k 的组合已经都排除出去了,剩下的一个单独的也不能组成 k 了
61+
// 所以这里要把它的频次置为 0 。如果这里不置为 0,下面代码判断逻辑还需要考虑重复使用数字的情况
62+
counter[k>>1] = 0
63+
}
64+
for num, freq := range counter {
65+
if num <= k/2 {
66+
remain := k - num
67+
if counter[remain] < freq {
68+
res += counter[remain]
69+
} else {
70+
res += freq
71+
}
72+
}
73+
}
74+
return res
75+
}
76+
77+
// 解法二
78+
func maxOperations_(nums []int, k int) int {
79+
counter, res := make(map[int]int), 0
80+
for _, num := range nums {
81+
counter[num]++
82+
remain := k - num
83+
if num == remain {
84+
if counter[num] >= 2 {
85+
res++
86+
counter[num] -= 2
87+
}
88+
} else {
89+
if counter[remain] > 0 {
90+
res++
91+
counter[remain]--
92+
counter[num]--
93+
}
94+
}
95+
}
96+
return res
97+
}
98+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package leetcode
2+
3+
import (
4+
"math/bits"
5+
)
6+
7+
// 解法一 模拟
8+
func concatenatedBinary(n int) int {
9+
res, mod, shift := 0, 1000000007, 0
10+
for i := 1; i <= n; i++ {
11+
if (i & (i - 1)) == 0 {
12+
shift++
13+
}
14+
res = ((res << shift) + i) % mod
15+
}
16+
return res
17+
}
18+
19+
// 解法二 位运算
20+
func concatenatedBinary1(n int) int {
21+
res := 0
22+
for i := 1; i <= n; i++ {
23+
res = (res<<bits.Len(uint(i)) | i) % (1e9 + 7)
24+
}
25+
return res
26+
}

0 commit comments

Comments
 (0)