Skip to content

Commit 7da6ca7

Browse files
committed
Add LeetCode Weekly Contest 259
1 parent 6ac3b50 commit 7da6ca7

File tree

8 files changed

+310
-0
lines changed

8 files changed

+310
-0
lines changed

leetcode/weekly/259/a/a.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
// github.com/EndlessCheng/codeforces-go
4+
func finalValueAfterOperations(operations []string) (ans int) {
5+
for _, op := range operations {
6+
if op[1] == '+' {
7+
ans++
8+
} else {
9+
ans--
10+
}
11+
}
12+
return
13+
}

leetcode/weekly/259/a/a_test.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

leetcode/weekly/259/b/b.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
// 前缀最大值+后缀最小值
4+
5+
// github.com/EndlessCheng/codeforces-go
6+
func sumOfBeauties(a []int) (ans int) {
7+
n := len(a)
8+
sufMin := make([]int, n) // 后缀最小值
9+
sufMin[n-1] = a[n-1]
10+
for i := n - 2; i > 1; i-- {
11+
sufMin[i] = min(sufMin[i+1], a[i])
12+
}
13+
preMax := a[0] // 前缀最大值
14+
for i := 1; i < n-1; i++ {
15+
v := a[i]
16+
if preMax < v && v < sufMin[i+1] {
17+
ans += 2
18+
} else if a[i-1] < v && v < a[i+1] {
19+
ans++
20+
}
21+
if v > preMax {
22+
preMax = v
23+
}
24+
}
25+
return
26+
}
27+
28+
func min(a, b int) int {
29+
if a > b {
30+
return b
31+
}
32+
return a
33+
}

leetcode/weekly/259/b/b_test.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

leetcode/weekly/259/c/c.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
// github.com/EndlessCheng/codeforces-go
4+
type DetectSquares struct{}
5+
6+
var row [1001][]int
7+
var col = [1001]map[int]int{}
8+
9+
func Constructor() (_ DetectSquares) {
10+
row = [1001][]int{}
11+
for i := range col {
12+
col[i] = map[int]int{}
13+
}
14+
return
15+
}
16+
17+
func (DetectSquares) Add(point []int) {
18+
x, y := point[0], point[1]
19+
row[y] = append(row[y], x)
20+
col[x][y]++
21+
}
22+
23+
func (DetectSquares) Count(point []int) (ans int) {
24+
x, y := point[0], point[1]
25+
for _, x2 := range row[y] {
26+
if x2 != x {
27+
d := abs(x2 - x) // 横向距离
28+
ans += col[x][y-d] * col[x2][y-d] // 将 x-x2 当作正方形顶边两个点,计算正方形底边两点的组合方案
29+
ans += col[x][y+d] * col[x2][y+d] // 将 x-x2 当作正方形底边两个点,计算正方形顶边两点的组合方案
30+
}
31+
}
32+
return
33+
}
34+
35+
func abs(x int) int {
36+
if x < 0 {
37+
return -x
38+
}
39+
return x
40+
}

leetcode/weekly/259/c/c_test.go

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

leetcode/weekly/259/d/d.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package main
2+
3+
/*
4+
5+
根据题意,答案长度不会超过 $\lceil\dfrac{n}{k}\rceil$,而 $n<8k$,故答案长度不超过 $7$。
6+
7+
我们可以统计出 $s$ 中个数不低于 $k$ 的字符,答案只能由这些字符注册,而这些字符的个数不会超过 $\lceil\dfrac{n}{k}\rceil$,由于 $n<8k$,故字符个数不超过 $7$。
8+
9+
这些然后暴力枚举这些字符的某个排列,然后贪心地匹配子序列,从中找到符合题目要求的子序列。
10+
11+
*/
12+
13+
// github.com/EndlessCheng/codeforces-go
14+
func longestSubsequenceRepeatedK(s string, k int) (ans string) {
15+
n := len(s)
16+
right := [26]int{}
17+
for i := range right {
18+
right[i] = n
19+
}
20+
nxt := make([][26]int, n)
21+
cnt := [26]int{}
22+
for i := n - 1; i >= 0; i-- {
23+
nxt[i] = right
24+
right[s[i]-'a'] = i
25+
cnt[s[i]-'a']++
26+
}
27+
28+
// 计算所有可能出现在 ans 中的字符,包括重复的
29+
// 倒着统计,这样下面计算排列时的第一个合法方案就是答案,从而提前退出
30+
a := []byte{}
31+
for i := 25; i >= 0; i-- {
32+
for c := cnt[i]; c >= k; c -= k {
33+
a = append(a, 'a'+byte(i))
34+
}
35+
}
36+
37+
for m := len(a); m > 0 && ans == ""; m-- {
38+
permutations(len(a), m, func(ids []int) bool {
39+
t := make([]byte, m)
40+
for i, id := range ids {
41+
t[i] = a[id]
42+
}
43+
i, j := 0, 0
44+
if t[0] == s[0] {
45+
j = 1
46+
}
47+
for {
48+
i = nxt[i][t[j%m]-'a']
49+
if i == n {
50+
break
51+
}
52+
j++
53+
}
54+
if j >= k*len(t) {
55+
ans = string(t)
56+
return true
57+
}
58+
return false
59+
})
60+
}
61+
return
62+
}
63+
64+
// 生成 n 选 r 的排列
65+
func permutations(n, r int, do func(ids []int) bool) {
66+
ids := make([]int, n)
67+
for i := range ids {
68+
ids[i] = i
69+
}
70+
if do(ids[:r]) {
71+
return
72+
}
73+
cycles := make([]int, r)
74+
for i := range cycles {
75+
cycles[i] = n - i
76+
}
77+
for {
78+
i := r - 1
79+
for ; i >= 0; i-- {
80+
cycles[i]--
81+
if cycles[i] == 0 {
82+
tmp := ids[i]
83+
copy(ids[i:], ids[i+1:])
84+
ids[n-1] = tmp
85+
cycles[i] = n - i
86+
} else {
87+
j := cycles[i]
88+
ids[i], ids[n-j] = ids[n-j], ids[i]
89+
if do(ids[:r]) {
90+
return
91+
}
92+
break
93+
}
94+
}
95+
if i == -1 {
96+
return
97+
}
98+
}
99+
}

leetcode/weekly/259/d/d_test.go

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)