diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 057. \345\200\274\345\222\214\344\270\213\346\240\207\344\271\213\345\267\256\351\203\275\345\234\250\347\273\231\345\256\232\347\232\204\350\214\203\345\233\264\345\206\205/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 057. \345\200\274\345\222\214\344\270\213\346\240\207\344\271\213\345\267\256\351\203\275\345\234\250\347\273\231\345\256\232\347\232\204\350\214\203\345\233\264\345\206\205/README.md" index 8c2b27b5d2b0a..9696bd7c15449 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 057. \345\200\274\345\222\214\344\270\213\346\240\207\344\271\213\345\267\256\351\203\275\345\234\250\347\273\231\345\256\232\347\232\204\350\214\203\345\233\264\345\206\205/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 057. \345\200\274\345\222\214\344\270\213\346\240\207\344\271\213\345\267\256\351\203\275\345\234\250\347\273\231\345\256\232\347\232\204\350\214\203\345\233\264\345\206\205/README.md" @@ -117,100 +117,23 @@ public: ### **Go** ```go -import "math/rand" - -type node struct { - ch [2]*node - priority int - val int -} - -func (o *node) cmp(b int) int { - switch { - case b < o.val: - return 0 - case b > o.val: - return 1 - default: - return -1 - } -} - -func (o *node) rotate(d int) *node { - x := o.ch[d^1] - o.ch[d^1] = x.ch[d] - x.ch[d] = o - return x -} - -type treap struct { - root *node -} - -func (t *treap) _put(o *node, val int) *node { - if o == nil { - return &node{priority: rand.Int(), val: val} - } - d := o.cmp(val) - o.ch[d] = t._put(o.ch[d], val) - if o.ch[d].priority > o.priority { - o = o.rotate(d ^ 1) - } - return o -} - -func (t *treap) put(val int) { - t.root = t._put(t.root, val) -} - -func (t *treap) _delete(o *node, val int) *node { - if d := o.cmp(val); d >= 0 { - o.ch[d] = t._delete(o.ch[d], val) - return o - } - if o.ch[1] == nil { - return o.ch[0] - } - if o.ch[0] == nil { - return o.ch[1] - } - d := 0 - if o.ch[0].priority > o.ch[1].priority { - d = 1 - } - o = o.rotate(d) - o.ch[d] = t._delete(o.ch[d], val) - return o -} - -func (t *treap) delete(val int) { - t.root = t._delete(t.root, val) -} - -func (t *treap) lowerBound(val int) (lb *node) { - for o := t.root; o != nil; { - switch c := o.cmp(val); { - case c == 0: - lb = o - o = o.ch[0] - case c > 0: - o = o.ch[1] - default: - return o +func containsNearbyAlmostDuplicate(nums []int, k int, t int) bool { + n := len(nums) + left, right := 0, 0 + rbt := redblacktree.NewWithIntComparator() + for right < n { + cur := nums[right] + right++ + if p, ok := rbt.Floor(cur); ok && cur-p.Key.(int) <= t { + return true } - } - return -} - -func containsNearbyAlmostDuplicate(nums []int, k, t int) bool { - s := &treap{} - for i, num := range nums { - if lb := s.lowerBound(num - t); lb != nil && lb.val <= num+t { + if p, ok := rbt.Ceiling(cur); ok && p.Key.(int)-cur <= t { return true } - s.put(num) - if i >= k { - s.delete(nums[i-k]) + rbt.Put(cur, struct{}{}) + if right-left > k { + rbt.Remove(nums[left]) + left++ } } return false diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 057. \345\200\274\345\222\214\344\270\213\346\240\207\344\271\213\345\267\256\351\203\275\345\234\250\347\273\231\345\256\232\347\232\204\350\214\203\345\233\264\345\206\205/Solution.go" "b/lcof2/\345\211\221\346\214\207 Offer II 057. \345\200\274\345\222\214\344\270\213\346\240\207\344\271\213\345\267\256\351\203\275\345\234\250\347\273\231\345\256\232\347\232\204\350\214\203\345\233\264\345\206\205/Solution.go" index b2b4c3125428a..1f70de096f2f8 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 057. \345\200\274\345\222\214\344\270\213\346\240\207\344\271\213\345\267\256\351\203\275\345\234\250\347\273\231\345\256\232\347\232\204\350\214\203\345\233\264\345\206\205/Solution.go" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 057. \345\200\274\345\222\214\344\270\213\346\240\207\344\271\213\345\267\256\351\203\275\345\234\250\347\273\231\345\256\232\347\232\204\350\214\203\345\233\264\345\206\205/Solution.go" @@ -1,98 +1,21 @@ -import "math/rand" - -type node struct { - ch [2]*node - priority int - val int -} - -func (o *node) cmp(b int) int { - switch { - case b < o.val: - return 0 - case b > o.val: - return 1 - default: - return -1 - } -} - -func (o *node) rotate(d int) *node { - x := o.ch[d^1] - o.ch[d^1] = x.ch[d] - x.ch[d] = o - return x -} - -type treap struct { - root *node -} - -func (t *treap) _put(o *node, val int) *node { - if o == nil { - return &node{priority: rand.Int(), val: val} - } - d := o.cmp(val) - o.ch[d] = t._put(o.ch[d], val) - if o.ch[d].priority > o.priority { - o = o.rotate(d ^ 1) - } - return o -} - -func (t *treap) put(val int) { - t.root = t._put(t.root, val) -} - -func (t *treap) _delete(o *node, val int) *node { - if d := o.cmp(val); d >= 0 { - o.ch[d] = t._delete(o.ch[d], val) - return o - } - if o.ch[1] == nil { - return o.ch[0] - } - if o.ch[0] == nil { - return o.ch[1] - } - d := 0 - if o.ch[0].priority > o.ch[1].priority { - d = 1 - } - o = o.rotate(d) - o.ch[d] = t._delete(o.ch[d], val) - return o -} - -func (t *treap) delete(val int) { - t.root = t._delete(t.root, val) -} - -func (t *treap) lowerBound(val int) (lb *node) { - for o := t.root; o != nil; { - switch c := o.cmp(val); { - case c == 0: - lb = o - o = o.ch[0] - case c > 0: - o = o.ch[1] - default: - return o +func containsNearbyAlmostDuplicate(nums []int, k int, t int) bool { + n := len(nums) + left, right := 0, 0 + rbt := redblacktree.NewWithIntComparator() + for right < n { + cur := nums[right] + right++ + if p, ok := rbt.Floor(cur); ok && cur-p.Key.(int) <= t { + return true } - } - return -} - -func containsNearbyAlmostDuplicate(nums []int, k, t int) bool { - s := &treap{} - for i, num := range nums { - if lb := s.lowerBound(num - t); lb != nil && lb.val <= num+t { + if p, ok := rbt.Ceiling(cur); ok && p.Key.(int)-cur <= t { return true } - s.put(num) - if i >= k { - s.delete(nums[i-k]) + rbt.Put(cur, struct{}{}) + if right-left > k { + rbt.Remove(nums[left]) + left++ } } return false -} \ No newline at end of file +} diff --git a/solution/0200-0299/0220.Contains Duplicate III/README.md b/solution/0200-0299/0220.Contains Duplicate III/README.md index cd7364e2b89af..5bab4efcf2b71 100644 --- a/solution/0200-0299/0220.Contains Duplicate III/README.md +++ b/solution/0200-0299/0220.Contains Duplicate III/README.md @@ -116,100 +116,23 @@ public: ### **Go** ```go -import "math/rand" - -type node struct { - ch [2]*node - priority int - val int -} - -func (o *node) cmp(b int) int { - switch { - case b < o.val: - return 0 - case b > o.val: - return 1 - default: - return -1 - } -} - -func (o *node) rotate(d int) *node { - x := o.ch[d^1] - o.ch[d^1] = x.ch[d] - x.ch[d] = o - return x -} - -type treap struct { - root *node -} - -func (t *treap) _put(o *node, val int) *node { - if o == nil { - return &node{priority: rand.Int(), val: val} - } - d := o.cmp(val) - o.ch[d] = t._put(o.ch[d], val) - if o.ch[d].priority > o.priority { - o = o.rotate(d ^ 1) - } - return o -} - -func (t *treap) put(val int) { - t.root = t._put(t.root, val) -} - -func (t *treap) _delete(o *node, val int) *node { - if d := o.cmp(val); d >= 0 { - o.ch[d] = t._delete(o.ch[d], val) - return o - } - if o.ch[1] == nil { - return o.ch[0] - } - if o.ch[0] == nil { - return o.ch[1] - } - d := 0 - if o.ch[0].priority > o.ch[1].priority { - d = 1 - } - o = o.rotate(d) - o.ch[d] = t._delete(o.ch[d], val) - return o -} - -func (t *treap) delete(val int) { - t.root = t._delete(t.root, val) -} - -func (t *treap) lowerBound(val int) (lb *node) { - for o := t.root; o != nil; { - switch c := o.cmp(val); { - case c == 0: - lb = o - o = o.ch[0] - case c > 0: - o = o.ch[1] - default: - return o +func containsNearbyAlmostDuplicate(nums []int, k int, t int) bool { + n := len(nums) + left, right := 0, 0 + rbt := redblacktree.NewWithIntComparator() + for right < n { + cur := nums[right] + right++ + if p, ok := rbt.Floor(cur); ok && cur-p.Key.(int) <= t { + return true } - } - return -} - -func containsNearbyAlmostDuplicate(nums []int, k, t int) bool { - s := &treap{} - for i, num := range nums { - if lb := s.lowerBound(num - t); lb != nil && lb.val <= num+t { + if p, ok := rbt.Ceiling(cur); ok && p.Key.(int)-cur <= t { return true } - s.put(num) - if i >= k { - s.delete(nums[i-k]) + rbt.Put(cur, struct{}{}) + if right-left > k { + rbt.Remove(nums[left]) + left++ } } return false diff --git a/solution/0200-0299/0220.Contains Duplicate III/README_EN.md b/solution/0200-0299/0220.Contains Duplicate III/README_EN.md index f3bbeb6268717..c2ed8c5c8ecc6 100644 --- a/solution/0200-0299/0220.Contains Duplicate III/README_EN.md +++ b/solution/0200-0299/0220.Contains Duplicate III/README_EN.md @@ -94,100 +94,23 @@ public: ### **Go** ```go -import "math/rand" - -type node struct { - ch [2]*node - priority int - val int -} - -func (o *node) cmp(b int) int { - switch { - case b < o.val: - return 0 - case b > o.val: - return 1 - default: - return -1 - } -} - -func (o *node) rotate(d int) *node { - x := o.ch[d^1] - o.ch[d^1] = x.ch[d] - x.ch[d] = o - return x -} - -type treap struct { - root *node -} - -func (t *treap) _put(o *node, val int) *node { - if o == nil { - return &node{priority: rand.Int(), val: val} - } - d := o.cmp(val) - o.ch[d] = t._put(o.ch[d], val) - if o.ch[d].priority > o.priority { - o = o.rotate(d ^ 1) - } - return o -} - -func (t *treap) put(val int) { - t.root = t._put(t.root, val) -} - -func (t *treap) _delete(o *node, val int) *node { - if d := o.cmp(val); d >= 0 { - o.ch[d] = t._delete(o.ch[d], val) - return o - } - if o.ch[1] == nil { - return o.ch[0] - } - if o.ch[0] == nil { - return o.ch[1] - } - d := 0 - if o.ch[0].priority > o.ch[1].priority { - d = 1 - } - o = o.rotate(d) - o.ch[d] = t._delete(o.ch[d], val) - return o -} - -func (t *treap) delete(val int) { - t.root = t._delete(t.root, val) -} - -func (t *treap) lowerBound(val int) (lb *node) { - for o := t.root; o != nil; { - switch c := o.cmp(val); { - case c == 0: - lb = o - o = o.ch[0] - case c > 0: - o = o.ch[1] - default: - return o +func containsNearbyAlmostDuplicate(nums []int, k int, t int) bool { + n := len(nums) + left, right := 0, 0 + rbt := redblacktree.NewWithIntComparator() + for right < n { + cur := nums[right] + right++ + if p, ok := rbt.Floor(cur); ok && cur-p.Key.(int) <= t { + return true } - } - return -} - -func containsNearbyAlmostDuplicate(nums []int, k, t int) bool { - s := &treap{} - for i, num := range nums { - if lb := s.lowerBound(num - t); lb != nil && lb.val <= num+t { + if p, ok := rbt.Ceiling(cur); ok && p.Key.(int)-cur <= t { return true } - s.put(num) - if i >= k { - s.delete(nums[i-k]) + rbt.Put(cur, struct{}{}) + if right-left > k { + rbt.Remove(nums[left]) + left++ } } return false diff --git a/solution/0200-0299/0220.Contains Duplicate III/Solution.go b/solution/0200-0299/0220.Contains Duplicate III/Solution.go index b2b4c3125428a..1f70de096f2f8 100644 --- a/solution/0200-0299/0220.Contains Duplicate III/Solution.go +++ b/solution/0200-0299/0220.Contains Duplicate III/Solution.go @@ -1,98 +1,21 @@ -import "math/rand" - -type node struct { - ch [2]*node - priority int - val int -} - -func (o *node) cmp(b int) int { - switch { - case b < o.val: - return 0 - case b > o.val: - return 1 - default: - return -1 - } -} - -func (o *node) rotate(d int) *node { - x := o.ch[d^1] - o.ch[d^1] = x.ch[d] - x.ch[d] = o - return x -} - -type treap struct { - root *node -} - -func (t *treap) _put(o *node, val int) *node { - if o == nil { - return &node{priority: rand.Int(), val: val} - } - d := o.cmp(val) - o.ch[d] = t._put(o.ch[d], val) - if o.ch[d].priority > o.priority { - o = o.rotate(d ^ 1) - } - return o -} - -func (t *treap) put(val int) { - t.root = t._put(t.root, val) -} - -func (t *treap) _delete(o *node, val int) *node { - if d := o.cmp(val); d >= 0 { - o.ch[d] = t._delete(o.ch[d], val) - return o - } - if o.ch[1] == nil { - return o.ch[0] - } - if o.ch[0] == nil { - return o.ch[1] - } - d := 0 - if o.ch[0].priority > o.ch[1].priority { - d = 1 - } - o = o.rotate(d) - o.ch[d] = t._delete(o.ch[d], val) - return o -} - -func (t *treap) delete(val int) { - t.root = t._delete(t.root, val) -} - -func (t *treap) lowerBound(val int) (lb *node) { - for o := t.root; o != nil; { - switch c := o.cmp(val); { - case c == 0: - lb = o - o = o.ch[0] - case c > 0: - o = o.ch[1] - default: - return o +func containsNearbyAlmostDuplicate(nums []int, k int, t int) bool { + n := len(nums) + left, right := 0, 0 + rbt := redblacktree.NewWithIntComparator() + for right < n { + cur := nums[right] + right++ + if p, ok := rbt.Floor(cur); ok && cur-p.Key.(int) <= t { + return true } - } - return -} - -func containsNearbyAlmostDuplicate(nums []int, k, t int) bool { - s := &treap{} - for i, num := range nums { - if lb := s.lowerBound(num - t); lb != nil && lb.val <= num+t { + if p, ok := rbt.Ceiling(cur); ok && p.Key.(int)-cur <= t { return true } - s.put(num) - if i >= k { - s.delete(nums[i-k]) + rbt.Put(cur, struct{}{}) + if right-left > k { + rbt.Remove(nums[left]) + left++ } } return false -} \ No newline at end of file +}