-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds example of binary search where you find for negatives and positives
- Loading branch information
Showing
2 changed files
with
135 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from typing import List | ||
import unittest | ||
|
||
|
||
def binary_search_negatives(nums, size): | ||
right = size - 1 | ||
left = 0 | ||
|
||
while left <= right: | ||
middle = (left + right) // 2 | ||
|
||
if nums[middle] > 0: | ||
right -= 1 | ||
elif nums[middle] < 0: | ||
left += 1 | ||
else: | ||
right -= 1 | ||
|
||
return left | ||
|
||
|
||
def binary_search_positives(nums, left, size): | ||
right = size - 1 | ||
|
||
while left <= right: | ||
middle = (left + right) // 2 | ||
|
||
if nums[middle] <= 0: | ||
left += 1 | ||
elif nums[middle] > 0: | ||
right -= 1 | ||
|
||
return size - left | ||
|
||
|
||
class Solution: | ||
def maximumCount(self, nums: List[int]) -> int: | ||
size = len(nums) | ||
negatives = binary_search_negatives(nums, size) | ||
positives = binary_search_positives(nums, negatives, size) | ||
|
||
return max(negatives, positives) | ||
|
||
|
||
class Test(unittest.TestCase): | ||
def setUp(self): | ||
self.solution = Solution() | ||
|
||
def test_first(self): | ||
self.assertEqual(self.solution.maximumCount(nums=[-2, -1, -1, 1, 2, 3]), 3) | ||
|
||
def test_second(self): | ||
self.assertEqual(self.solution.maximumCount(nums=[5, 20, 66, 1314]), 4) | ||
|
||
def test_third(self): | ||
self.assertEqual(self.solution.maximumCount(nums=[-3, -2, -1, 0, 0, 1, 2]), 3) | ||
|
||
def test_fourth(self): | ||
nums = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] | ||
self.assertEqual(self.solution.maximumCount(nums), 0) | ||
|
||
def test_fifth(self): | ||
nums = [-2, -1, -1, 0, 0, 0] | ||
self.assertEqual(self.solution.maximumCount(nums), 3) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,67 @@ | ||
| Problem Number | Tag | URL | | ||
| -------------- | ---------------------- | -------------------------------------------------------------------------- | | ||
| 1.py | Hash table | https://leetcode.com/problems/two-sum/ | | ||
| 977.py | Sorting | https://leetcode.com/problems/squares-of-a-sorted-array/ | | ||
| 1051.py | | | | ||
| 1207.py | | | | ||
| 1337.py | Heap | https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix | | ||
| 1351.py | Binary Search | https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix | | ||
| 1356.py | | | | ||
| 1365.py | | | | ||
| 1370.py | | | | ||
| 1406.py | Sorting | https://leetcode.com/problems/minimum-subsequence-in-non-increasing-order/ | | ||
| 1436.py | | | | ||
| 1464.py | | | | ||
| 1475.py | | | | ||
| 1512.py | | | | ||
| 1636.py | | | | ||
| 1656.py | | | | ||
| 1684.py | | | | ||
| 1710.py | | | | ||
| 1742.py | | | | ||
| 1748.py | | | | ||
| 1832.py | | | | ||
| 1859.py | | | | ||
| 1876.py | | | | ||
| 1920.py | | | | ||
| 1935.py | | | | ||
| 1941.py | | | | ||
| 20.py | | | | ||
| 2006.py | | | | ||
| 2032.py | | | | ||
| 2037.py | | | | ||
| 2053.py | | | | ||
| 2089.py | Sorting, Binary Search | https://leetcode.com/problems/find-target-indices-after-sorting-array | | ||
| 2060.py | | | | ||
| 2103.py | | | | ||
| 2154.py | | | | ||
| 2206.py | | | | ||
| 2215.py | | | | ||
| 2283.py | | | | ||
| 2325.py | | | | ||
| 2341.py | | | | ||
| 2363.py | | | | ||
| 2367-2.py | | | | ||
| 2367.py | | | | ||
| 2389.py | Soring/PrefixSum | https://leetcode.com/problems/longest-subsequence-with-limited-sum | | ||
| 2418.py | | | | ||
| 2475.py | | | | ||
| 2500.py | | | | ||
| 2545.py | | | | ||
| 2570.py | | | | ||
| 2670.py | | | | ||
| 2716.py | | | | ||
| 2733.py | | | | ||
| 2744.py | | | | ||
| 2848.py | | | | ||
| 2913.py | | | | ||
| 2932.py | | | | ||
| 414.py | | | | ||
| 496.py | | | | ||
| 56.py | | | | ||
| 561.py | | | | ||
| 771.py | | | | ||
| 804.py | | | | ||
| 905.py | | | | ||
| 961.py | | | | ||
| Problem Number | Tag | URL | | ||
| -------------- | ---------------------- | ------------------------------------------------------------------------------------ | | ||
| 1.py | Hash table | https://leetcode.com/problems/two-sum/ | | ||
| 977.py | Sorting | https://leetcode.com/problems/squares-of-a-sorted-array/ | | ||
| 1051.py | | | | ||
| 1207.py | | | | ||
| 1337.py | Heap | https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix | | ||
| 1351.py | Binary Search | https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix | | ||
| 1356.py | | | | ||
| 1365.py | | | | ||
| 1370.py | | | | ||
| 1406.py | Sorting | https://leetcode.com/problems/minimum-subsequence-in-non-increasing-order/ | | ||
| 1436.py | | | | ||
| 1464.py | | | | ||
| 1475.py | | | | ||
| 1512.py | | | | ||
| 1636.py | | | | ||
| 1656.py | | | | ||
| 1684.py | | | | ||
| 1710.py | | | | ||
| 1742.py | | | | ||
| 1748.py | | | | ||
| 1832.py | | | | ||
| 1859.py | | | | ||
| 1876.py | | | | ||
| 1920.py | | | | ||
| 1935.py | | | | ||
| 1941.py | | | | ||
| 20.py | | | | ||
| 2006.py | | | | ||
| 2032.py | | | | ||
| 2037.py | | | | ||
| 2053.py | | | | ||
| 2089.py | Sorting, Binary Search | https://leetcode.com/problems/find-target-indices-after-sorting-array | | ||
| 2060.py | | | | ||
| 2103.py | | | | ||
| 2154.py | | | | ||
| 2206.py | | | | ||
| 2215.py | | | | ||
| 2283.py | | | | ||
| 2325.py | | | | ||
| 2341.py | | | | ||
| 2363.py | | | | ||
| 2367-2.py | | | | ||
| 2367.py | | | | ||
| 2389.py | Soring/PrefixSum | https://leetcode.com/problems/longest-subsequence-with-limited-sum | | ||
| 2418.py | | | | ||
| 2475.py | | | | ||
| 2500.py | | | | ||
| 2529.py | Binary Search | https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer | | ||
| 2545.py | | | | ||
| 2570.py | | | | ||
| 2670.py | | | | ||
| 2716.py | | | | ||
| 2733.py | | | | ||
| 2744.py | | | | ||
| 2848.py | | | | ||
| 2913.py | | | | ||
| 2932.py | | | | ||
| 414.py | | | | ||
| 496.py | | | | ||
| 56.py | | | | ||
| 561.py | | | | ||
| 771.py | | | | ||
| 804.py | | | | ||
| 905.py | | | | ||
| 961.py | | | |