Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Nov 29, 2023
1 parent 0219b7e commit defb23e
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions leetcode/220.ContainsDuplicateIII/containsDuplicateIII.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package main

import (
"fmt"
)

func containsNearbyAlmostDuplicate(nums []int, indexDiff int, valueDiff int) bool {
// Input: nums = [1,2,3,1], indexDiff = 3, valueDiff = 0
// Output: true
// (i, j) = (0, 3)
// abs(i-j) <= indexDiff, abs(nums[i] - nums[j]) <= valueDiff
// return true

if (indexDiff < 1) || (valueDiff < 0) {
return false
}

for i := 0; i < len(nums); i++ {
for j := i + 1; j <= min(len(nums)-1, i+indexDiff); j++ {
if abs(nums[i], nums[j]) <= valueDiff {
return true
}
}
}

return false
}

// Sliding window
func containsNearbyAlmostDuplicate1(nums []int, indexDiff int, valueDiff int) bool {
if valueDiff < 0 {
return false
}

buckets := make(map[int]int)
width := valueDiff + 1

for i, num := range nums {
bucket := getBucket(num, width)

if _, ok := buckets[bucket]; ok {
return true
}

if val, ok := buckets[bucket-1]; ok && abs(num, val) < width {
return true
}

if val, ok := buckets[bucket+1]; ok && abs(num, val) < width {
return true
}

buckets[bucket] = num
if i >= indexDiff {
delete(buckets, getBucket(nums[i-indexDiff], width))
}
}

return false
}

func getBucket(num, width int) int {
if num >= 0 {
return num / width
}
return (num+1)/width - 1
}

func min(a, b int) int {
if a < b {
return a
}
return b
}

func abs(a, b int) int {
if a > b {
return a - b
}
return b - a
}

func main() {
nums := []int{1, 2, 3, 1}
indexDiff := 3
valueDiff := 0

fmt.Println(containsNearbyAlmostDuplicate1(nums, indexDiff, valueDiff))
}

0 comments on commit defb23e

Please sign in to comment.