Skip to content

Commit

Permalink
Adds 1385 binary search
Browse files Browse the repository at this point in the history
  • Loading branch information
joffilyfe committed Dec 30, 2023
1 parent 24e3ff6 commit 11f0cd2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
49 changes: 49 additions & 0 deletions 1385.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from copy import copy
from typing import List
import unittest


class Solution:
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
arr2.sort()
size = len(arr2) - 1
count = len(arr1)

for number in arr1:
left = 0
right = size

while left <= right:
middle = (left + right) // 2
if abs(arr2[middle] - number) <= d:
count -= 1
break
elif number < arr2[middle]:
right = middle - 1
else:
left = middle + 1

return count


class Test(unittest.TestCase):
def setUp(self):
self.solution = Solution()

def test_first(self):
self.assertEqual(
self.solution.findTheDistanceValue(arr1=[4, 5, 8], arr2=[1, 8, 9, 10], d=2),
2,
)

def test_second(self):
self.assertEqual(
self.solution.findTheDistanceValue(
arr1=[1, 4, 2, 3], arr2=[-4, -3, 6, 10, 20, 30], d=3
),
2,
)


if __name__ == "__main__":
unittest.main()
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
| 1356.py | | |
| 1365.py | | |
| 1370.py | | |
| 1385.py | Binary Search | https://leetcode.com/problems/find-the-distance-value-between-two-arrays |
| 1406.py | Sorting | https://leetcode.com/problems/minimum-subsequence-in-non-increasing-order/ |
| 1436.py | | |
| 1464.py | | |
Expand Down

0 comments on commit 11f0cd2

Please sign in to comment.