From cf71ff736fc52eef82cf51a727eaea60f01e36ac Mon Sep 17 00:00:00 2001 From: Joffily Ferreira Date: Thu, 28 Dec 2023 12:14:32 -0300 Subject: [PATCH] Adds first heap priority queue problem --- 1337.py | 62 ++++++++++++++++++++++++++++ README.md | 121 +++++++++++++++++++++++++++--------------------------- 2 files changed, 123 insertions(+), 60 deletions(-) create mode 100644 1337.py diff --git a/1337.py b/1337.py new file mode 100644 index 0000000..a55c8ca --- /dev/null +++ b/1337.py @@ -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() diff --git a/README.md b/README.md index 0b04cc9..8a2ae7d 100644 --- a/README.md +++ b/README.md @@ -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 | | |