Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 84637d8

Browse files
feat: change treap to gods library in lc/lcof2 problems (doocs#589)
1 parent b9d8aab commit 84637d8

File tree

5 files changed

+72
-457
lines changed

5 files changed

+72
-457
lines changed

‎lcof2/剑指 Offer II 057. 值和下标之差都在给定的范围内/README.md‎

Lines changed: 14 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -117,100 +117,23 @@ public:
117117
### **Go**
118118
119119
```go
120-
import "math/rand"
121-
122-
type node struct {
123-
ch [2]*node
124-
priority int
125-
val int
126-
}
127-
128-
func (o *node) cmp(b int) int {
129-
switch {
130-
case b < o.val:
131-
return 0
132-
case b > o.val:
133-
return 1
134-
default:
135-
return -1
136-
}
137-
}
138-
139-
func (o *node) rotate(d int) *node {
140-
x := o.ch[d^1]
141-
o.ch[d^1] = x.ch[d]
142-
x.ch[d] = o
143-
return x
144-
}
145-
146-
type treap struct {
147-
root *node
148-
}
149-
150-
func (t *treap) _put(o *node, val int) *node {
151-
if o == nil {
152-
return &node{priority: rand.Int(), val: val}
153-
}
154-
d := o.cmp(val)
155-
o.ch[d] = t._put(o.ch[d], val)
156-
if o.ch[d].priority > o.priority {
157-
o = o.rotate(d ^ 1)
158-
}
159-
return o
160-
}
161-
162-
func (t *treap) put(val int) {
163-
t.root = t._put(t.root, val)
164-
}
165-
166-
func (t *treap) _delete(o *node, val int) *node {
167-
if d := o.cmp(val); d >= 0 {
168-
o.ch[d] = t._delete(o.ch[d], val)
169-
return o
170-
}
171-
if o.ch[1] == nil {
172-
return o.ch[0]
173-
}
174-
if o.ch[0] == nil {
175-
return o.ch[1]
176-
}
177-
d := 0
178-
if o.ch[0].priority > o.ch[1].priority {
179-
d = 1
180-
}
181-
o = o.rotate(d)
182-
o.ch[d] = t._delete(o.ch[d], val)
183-
return o
184-
}
185-
186-
func (t *treap) delete(val int) {
187-
t.root = t._delete(t.root, val)
188-
}
189-
190-
func (t *treap) lowerBound(val int) (lb *node) {
191-
for o := t.root; o != nil; {
192-
switch c := o.cmp(val); {
193-
case c == 0:
194-
lb = o
195-
o = o.ch[0]
196-
case c > 0:
197-
o = o.ch[1]
198-
default:
199-
return o
120+
func containsNearbyAlmostDuplicate(nums []int, k int, t int) bool {
121+
n := len(nums)
122+
left, right := 0, 0
123+
rbt := redblacktree.NewWithIntComparator()
124+
for right < n {
125+
cur := nums[right]
126+
right++
127+
if p, ok := rbt.Floor(cur); ok && cur-p.Key.(int) <= t {
128+
return true
200129
}
201-
}
202-
return
203-
}
204-
205-
func containsNearbyAlmostDuplicate(nums []int, k, t int) bool {
206-
s := &treap{}
207-
for i, num := range nums {
208-
if lb := s.lowerBound(num - t); lb != nil && lb.val <= num+t {
130+
if p, ok := rbt.Ceiling(cur); ok && p.Key.(int)-cur <= t {
209131
return true
210132
}
211-
s.put(num)
212-
if i >= k {
213-
s.delete(nums[i-k])
133+
rbt.Put(cur, struct{}{})
134+
if right-left > k {
135+
rbt.Remove(nums[left])
136+
left++
214137
}
215138
}
216139
return false
Lines changed: 15 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,21 @@
1-
import "math/rand"
2-
3-
type node struct {
4-
ch [2]*node
5-
priority int
6-
val int
7-
}
8-
9-
func (o *node) cmp(b int) int {
10-
switch {
11-
case b < o.val:
12-
return 0
13-
case b > o.val:
14-
return 1
15-
default:
16-
return -1
17-
}
18-
}
19-
20-
func (o *node) rotate(d int) *node {
21-
x := o.ch[d^1]
22-
o.ch[d^1] = x.ch[d]
23-
x.ch[d] = o
24-
return x
25-
}
26-
27-
type treap struct {
28-
root *node
29-
}
30-
31-
func (t *treap) _put(o *node, val int) *node {
32-
if o == nil {
33-
return &node{priority: rand.Int(), val: val}
34-
}
35-
d := o.cmp(val)
36-
o.ch[d] = t._put(o.ch[d], val)
37-
if o.ch[d].priority > o.priority {
38-
o = o.rotate(d ^ 1)
39-
}
40-
return o
41-
}
42-
43-
func (t *treap) put(val int) {
44-
t.root = t._put(t.root, val)
45-
}
46-
47-
func (t *treap) _delete(o *node, val int) *node {
48-
if d := o.cmp(val); d >= 0 {
49-
o.ch[d] = t._delete(o.ch[d], val)
50-
return o
51-
}
52-
if o.ch[1] == nil {
53-
return o.ch[0]
54-
}
55-
if o.ch[0] == nil {
56-
return o.ch[1]
57-
}
58-
d := 0
59-
if o.ch[0].priority > o.ch[1].priority {
60-
d = 1
61-
}
62-
o = o.rotate(d)
63-
o.ch[d] = t._delete(o.ch[d], val)
64-
return o
65-
}
66-
67-
func (t *treap) delete(val int) {
68-
t.root = t._delete(t.root, val)
69-
}
70-
71-
func (t *treap) lowerBound(val int) (lb *node) {
72-
for o := t.root; o != nil; {
73-
switch c := o.cmp(val); {
74-
case c == 0:
75-
lb = o
76-
o = o.ch[0]
77-
case c > 0:
78-
o = o.ch[1]
79-
default:
80-
return o
1+
func containsNearbyAlmostDuplicate(nums []int, k int, t int) bool {
2+
n := len(nums)
3+
left, right := 0, 0
4+
rbt := redblacktree.NewWithIntComparator()
5+
for right < n {
6+
cur := nums[right]
7+
right++
8+
if p, ok := rbt.Floor(cur); ok && cur-p.Key.(int) <= t {
9+
return true
8110
}
82-
}
83-
return
84-
}
85-
86-
func containsNearbyAlmostDuplicate(nums []int, k, t int) bool {
87-
s := &treap{}
88-
for i, num := range nums {
89-
if lb := s.lowerBound(num - t); lb != nil && lb.val <= num+t {
11+
if p, ok := rbt.Ceiling(cur); ok && p.Key.(int)-cur <= t {
9012
return true
9113
}
92-
s.put(num)
93-
if i >= k {
94-
s.delete(nums[i-k])
14+
rbt.Put(cur, struct{}{})
15+
if right-left > k {
16+
rbt.Remove(nums[left])
17+
left++
9518
}
9619
}
9720
return false
98-
}
21+
}

‎solution/0200-0299/0220.Contains Duplicate III/README.md‎

Lines changed: 14 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -116,100 +116,23 @@ public:
116116
### **Go**
117117
118118
```go
119-
import "math/rand"
120-
121-
type node struct {
122-
ch [2]*node
123-
priority int
124-
val int
125-
}
126-
127-
func (o *node) cmp(b int) int {
128-
switch {
129-
case b < o.val:
130-
return 0
131-
case b > o.val:
132-
return 1
133-
default:
134-
return -1
135-
}
136-
}
137-
138-
func (o *node) rotate(d int) *node {
139-
x := o.ch[d^1]
140-
o.ch[d^1] = x.ch[d]
141-
x.ch[d] = o
142-
return x
143-
}
144-
145-
type treap struct {
146-
root *node
147-
}
148-
149-
func (t *treap) _put(o *node, val int) *node {
150-
if o == nil {
151-
return &node{priority: rand.Int(), val: val}
152-
}
153-
d := o.cmp(val)
154-
o.ch[d] = t._put(o.ch[d], val)
155-
if o.ch[d].priority > o.priority {
156-
o = o.rotate(d ^ 1)
157-
}
158-
return o
159-
}
160-
161-
func (t *treap) put(val int) {
162-
t.root = t._put(t.root, val)
163-
}
164-
165-
func (t *treap) _delete(o *node, val int) *node {
166-
if d := o.cmp(val); d >= 0 {
167-
o.ch[d] = t._delete(o.ch[d], val)
168-
return o
169-
}
170-
if o.ch[1] == nil {
171-
return o.ch[0]
172-
}
173-
if o.ch[0] == nil {
174-
return o.ch[1]
175-
}
176-
d := 0
177-
if o.ch[0].priority > o.ch[1].priority {
178-
d = 1
179-
}
180-
o = o.rotate(d)
181-
o.ch[d] = t._delete(o.ch[d], val)
182-
return o
183-
}
184-
185-
func (t *treap) delete(val int) {
186-
t.root = t._delete(t.root, val)
187-
}
188-
189-
func (t *treap) lowerBound(val int) (lb *node) {
190-
for o := t.root; o != nil; {
191-
switch c := o.cmp(val); {
192-
case c == 0:
193-
lb = o
194-
o = o.ch[0]
195-
case c > 0:
196-
o = o.ch[1]
197-
default:
198-
return o
119+
func containsNearbyAlmostDuplicate(nums []int, k int, t int) bool {
120+
n := len(nums)
121+
left, right := 0, 0
122+
rbt := redblacktree.NewWithIntComparator()
123+
for right < n {
124+
cur := nums[right]
125+
right++
126+
if p, ok := rbt.Floor(cur); ok && cur-p.Key.(int) <= t {
127+
return true
199128
}
200-
}
201-
return
202-
}
203-
204-
func containsNearbyAlmostDuplicate(nums []int, k, t int) bool {
205-
s := &treap{}
206-
for i, num := range nums {
207-
if lb := s.lowerBound(num - t); lb != nil && lb.val <= num+t {
129+
if p, ok := rbt.Ceiling(cur); ok && p.Key.(int)-cur <= t {
208130
return true
209131
}
210-
s.put(num)
211-
if i >= k {
212-
s.delete(nums[i-k])
132+
rbt.Put(cur, struct{}{})
133+
if right-left > k {
134+
rbt.Remove(nums[left])
135+
left++
213136
}
214137
}
215138
return false

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /