Skip to content

Commit 0c2b773

Browse files
committed
Add LeetCode Biweekly Contest 67
1 parent 60395f6 commit 0c2b773

File tree

8 files changed

+266
-0
lines changed

8 files changed

+266
-0
lines changed

leetcode/biweekly/67/a/a.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import "sort"
4+
5+
// 两次排序
6+
7+
// github.com/EndlessCheng/codeforces-go
8+
func maxSubsequence(nums []int, k int) []int {
9+
id := make([]int, len(nums))
10+
for i := range id {
11+
id[i] = i
12+
}
13+
sort.Slice(id, func(i, j int) bool { return nums[id[i]] > nums[id[j]] }) // 按元素值从大到小排序
14+
sort.Ints(id[:k]) // 对下标从小到大排序
15+
ans := make([]int, k)
16+
for i, j := range id[:k] {
17+
ans[i] = nums[j]
18+
}
19+
return ans
20+
}

leetcode/biweekly/67/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/biweekly/67/b/b.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
// 前后缀经典技巧
4+
5+
// github.com/EndlessCheng/codeforces-go
6+
func goodDaysToRobBank(security []int, time int) (ans []int) {
7+
n := len(security)
8+
if time*2 >= n { // i 最小为 time,而且需要满足 i+time<n,即 time*2<n,不满足则直接返回空数组
9+
return
10+
}
11+
12+
suf := make([]int, n) // suf[i] 表示从 security[i+1] 向后的最长连续非递减长度
13+
for i := n - 2; i >= 0; i-- {
14+
if security[i] <= security[i+1] {
15+
suf[i] = suf[i+1] + 1
16+
}
17+
}
18+
19+
pre := 0 // pre 表示从 security[i-1] 向前的最长连续非递增长度
20+
for i, v := range security[:n-time] {
21+
if i > 0 && v <= security[i-1] {
22+
pre++
23+
} else {
24+
pre = 0
25+
}
26+
if i >= time && pre >= time && suf[i] >= time {
27+
ans = append(ans, i)
28+
}
29+
}
30+
return
31+
}

leetcode/biweekly/67/b/b_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.

leetcode/biweekly/67/c/c.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
// 建图 + 暴力枚举所有起点
4+
5+
// github.com/EndlessCheng/codeforces-go
6+
func maximumDetonation(bombs [][]int) (ans int) {
7+
n := len(bombs)
8+
g := make([][]int, n)
9+
for i, p := range bombs {
10+
for j, q := range bombs {
11+
if j != i && (q[0]-p[0])*(q[0]-p[0])+(q[1]-p[1])*(q[1]-p[1]) <= p[2]*p[2] {
12+
g[i] = append(g[i], j) // 有向图
13+
}
14+
}
15+
}
16+
for i := range g {
17+
vis := make([]bool, n)
18+
cnt := 0
19+
var dfs func(int)
20+
dfs = func(v int) {
21+
vis[v] = true
22+
cnt++
23+
for _, w := range g[v] {
24+
if !vis[w] {
25+
dfs(w)
26+
}
27+
}
28+
}
29+
dfs(i)
30+
if cnt > ans {
31+
ans = cnt
32+
}
33+
}
34+
return
35+
}

leetcode/biweekly/67/c/c_test.go

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

leetcode/biweekly/67/d/d.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import "github.com/emirpasic/gods/trees/redblacktree"
4+
5+
/* 巧妙利用查询的特殊性
6+
7+
为了高效插入数据,我们可以用平衡树来维护所有景点。
8+
9+
注意到每次 `get` 后仅会将查询次数加一,我们可以直接用一个迭代器(指针)$\textit{cur}$ 指向下次查询需要返回的元素。
10+
11+
- 对于添加操作,如果添加的景点排在当前 $\textit{cur}$ 前面,那么移动 $\textit{cur}$ 至其前一个元素,否则不移动 $\textit{cur}$;
12+
- 对于查询操作,每次查询结束后将 $\textit{cur}$ 移至其下一个元素。
13+
14+
代码实现时,可以在初始时插入一个哨兵元素,从而简化判断逻辑。
15+
16+
*/
17+
18+
// github.com/EndlessCheng/codeforces-go
19+
type pair struct {
20+
score int
21+
name string
22+
}
23+
24+
func compare(x, y interface{}) int {
25+
a, b := x.(pair), y.(pair)
26+
if a.score > b.score || a.score == b.score && a.name < b.name {
27+
return -1
28+
}
29+
return 1
30+
}
31+
32+
type SORTracker struct {
33+
*redblacktree.Tree
34+
cur redblacktree.Iterator
35+
}
36+
37+
func Constructor() SORTracker {
38+
root := redblacktree.NewWith(compare)
39+
root.Put(pair{}, nil) // 哨兵
40+
return SORTracker{root, root.IteratorAt(root.Left())}
41+
}
42+
43+
func (t *SORTracker) Add(name string, score int) {
44+
p := pair{score, name}
45+
t.Put(p, nil)
46+
if compare(p, t.cur.Key()) < 0 {
47+
t.cur.Prev() // 移动至前一个元素
48+
}
49+
}
50+
51+
func (t *SORTracker) Get() string {
52+
name := t.cur.Key().(pair).name
53+
t.cur.Next() // 移动至下一个元素
54+
return name
55+
}

leetcode/biweekly/67/d/d_test.go

Lines changed: 25 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)