Leetcode Note: Go - Find the Distance Value Between Two Arrays

Find the Distance Value Between Two Arrays - LeetCode
https://leetcode.com/problems/find-the-distance-value-between-two-arrays/

  • Go 言語で取り組んだメモ

回答

Go Solution using Sorted Array - Find the Distance Value Between Two Arrays - LeetCode
https://leetcode.com/problems/find-the-distance-value-between-two-arrays/solutions/843087/go-solution-using-sorted-array/

func findTheDistanceValue(arr1 []int, arr2 []int, d int) int {
	sort.Ints(arr2)

	var count int
	for _, num := range arr1 {
		minIdx := sort.SearchInts(arr2, num-d)
		if minIdx < len(arr2) && arr2[minIdx] >= num-d && arr2[minIdx] <= num+d {
			continue
		}
		count++
	}

	return count
}