-
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 first heap priority queue problem
- Loading branch information
Showing
2 changed files
with
123 additions
and
60 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,62 @@ | ||
from typing import List, Tuple | ||
import heapq | ||
import unittest | ||
|
||
|
||
def number_of_targets(row: List[int]) -> int: | ||
"""Using binary seach we find what is the lefiest index where number 1 is. | ||
For this specific problem we are targetting the last index of number 1.""" | ||
|
||
left = 0 | ||
right = len(row) - 1 | ||
|
||
while left <= right: | ||
middle = (left + right) // 2 | ||
|
||
if row[middle] == 1: | ||
left = middle + 1 | ||
else: | ||
right = middle - 1 | ||
|
||
return left | ||
|
||
|
||
class Solution: | ||
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]: | ||
"""Using heap we create a priority qeue based in the number of ones for each row. More ones least the priority""" | ||
|
||
heap: List[Tuple[int, int]] = [] | ||
|
||
for i, row in enumerate(mat): | ||
heapq.heappush(heap, (number_of_targets(row), i)) | ||
|
||
return [heapq.heappop(heap)[1] for _ in range(k)] | ||
|
||
|
||
class TestCase(unittest.TestCase): | ||
def test_first(self): | ||
self.assertEqual( | ||
Solution().kWeakestRows( | ||
mat=[ | ||
[1, 1, 0, 0, 0], | ||
[1, 1, 1, 1, 0], | ||
[1, 0, 0, 0, 0], | ||
[1, 1, 0, 0, 0], | ||
[1, 1, 1, 1, 1], | ||
], | ||
k=3, | ||
), | ||
[2, 0, 3], | ||
) | ||
|
||
def test_second(self): | ||
self.assertEqual( | ||
Solution().kWeakestRows( | ||
mat=[[1, 0, 0, 0], [1, 1, 1, 1], [1, 0, 0, 0], [1, 0, 0, 0]], k=2 | ||
), | ||
[0, 2], | ||
) | ||
|
||
|
||
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,60 +1,61 @@ | ||
| Problem Number | Tag | URL | | ||
| -------------- | ---------- | -------------------------------------- | | ||
| 1.py | Hash table | https://leetcode.com/problems/two-sum/ | | ||
| 1051.py | | | | ||
| 1207.py | | | | ||
| 1356.py | | | | ||
| 1365.py | | | | ||
| 1370.py | | | | ||
| 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 | | | | ||
| 2060.py | | | | ||
| 2103.py | | | | ||
| 2154.py | | | | ||
| 2206.py | | | | ||
| 2215.py | | | | ||
| 2283.py | | | | ||
| 2325.py | | | | ||
| 2341.py | | | | ||
| 2363.py | | | | ||
| 2367-2.py | | | | ||
| 2367.py | | | | ||
| 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/ | | ||
| 1051.py | | | | ||
| 1207.py | | | | ||
| 1337.py | Heap | https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix | | ||
| 1356.py | | | | ||
| 1365.py | | | | ||
| 1370.py | | | | ||
| 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 | | | | ||
| 2060.py | | | | ||
| 2103.py | | | | ||
| 2154.py | | | | ||
| 2206.py | | | | ||
| 2215.py | | | | ||
| 2283.py | | | | ||
| 2325.py | | | | ||
| 2341.py | | | | ||
| 2363.py | | | | ||
| 2367-2.py | | | | ||
| 2367.py | | | | ||
| 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 | | | |