Exemplo n.º 1
0
            for row in range(m):
                if treasureMap[row][col] == "1":
                    findTreasure(row, col, treasureMap)
                    num_islands += 1

        return num_islands


if __name__ == '__main__':
    s = Solution()
    TR(
        (
            T(
                [
                    ["0", "1", "0"],
                    ["1", "1", "1"],
                    ["0", "0", "0"]
                ], expected = 1,
            ),
            T(
                [
                    ["1","0","1","0"],
                    ["0","1","0","1"],
                    ["1","0","1","0"],
                    ["0","1","1","1"]
                ],
                expected = 6
            ),
            T(
                [
                    ["1","1","1","1","0"],
Exemplo n.º 2
0
    total_product, out = None, []
    num_of_zeros = 0

    for num in nums:
        if num == 0:
            num_of_zeros += 1
            continue
        if total_product is None: total_product = 1
        total_product *= num

    if num_of_zeros > 1: return [0] * len(nums)

    for num in nums:
        if num_of_zeros == 1 and num != 0: out.append(0)
        elif num == 1 or num == 0: out.append(total_product)
        else: out.append(repeated_subtraction(total_product, num))

    return out


if __name__ == '__main__':
    TR(
        (T([1, 2, 3, 4], [24, 12, 8, 6]), T([1], [1]), T([5, 10], [10, 5]),
         T([1, 0], [0, 1]), T([0, 1, 2], [2, 0, 0]), T([0, 0], [0, 0]),
         T([0, 1, 0], [0, 0, 0]), T([1, 1, 1, 0], [0, 0, 0, 1]),
         T([0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0]), T(
             [1, -1], [-1, 1]), T([1, -2, -10, 2], [40, -20, -4, 20])),
        product_except_self,
        iterable_comparator,
    )()
Exemplo n.º 3
0
            res.append(f"{i}/{denominator}")

    return res


def simplifiedFractions(n: int) -> List[str]:
    # if n = 4, then I need to compute the result for each 0, 1, ... n to obtain the result. this can be store in a dict
    res = defaultdict(list)

    for i in range(2, n + 1):
        res[i] = helper(i)
    final_res: List[str] = []

    for v in res.values(): final_res.extend(v)

    return final_res


if __name__ == '__main__':
    TR(
        (
            T(2, ["1/2"]),
            T(1, []),
            T(3, ["1/2", "1/3", "2/3"]),
            T(4, ["1/2", "1/3", "2/3", "1/4", "3/4"]),
            T(5, ["1/2", "1/3", "2/3", "1/4", "3/4", "1/5", "2/5", "3/5", "4/5"]),
            T(6, ["1/2", "1/3", "2/3", "1/4", "3/4", "1/5", "2/5", "3/5", "4/5", "1/6", "5/6"])
        ),
        simplifiedFractions
    )()
    ...
Exemplo n.º 4
0
    prev = root
    for ix, branch in enumerate(happy_tree):
        lefts = branch[1]
        if lefts:
            prev.left = make_tree(lefts)
        if ix == 0: continue
        t = Tree(branch[0])
        prev.right = t
        prev = t
    return root

# I will implement T=O(log_2(N)), S=O(N)
# another solution I could think of is T=O(N ** 2) and S=O(1)
def bst_from_preorder(self, preorder: List[int]) -> TreeNode:
    happy_tree = construct_branch(preorder)
    root = make_tree(happy_tree)
    return root


if __name__ == '__main__':
    TR(
        (
            T(),
            T(),
            T(),
            T(),
            T(),
        ),
        bst_from_preorder,
    )()
Exemplo n.º 5
0
        curr_node = next_node  # Move to next node.
    head = prev_node
    return head


# prev, next, actual_next
def reverse_list(head: ListNode) -> ListNode:
    if head is None: return None
    prev = head
    next = prev.next
    prev.next = None
    if next is None: return head
    actual_next = next.next
    next.next = prev
    prev = next
    while actual_next is not None:
        next_ = actual_next.next
        actual_next.next = prev
        prev = actual_next
        actual_next = next_
    return prev


if __name__ == '__main__':
    TR((
        T(ListNode(1, ListNode(2, ListNode(3))),
          ListNode(3, ListNode(2, ListNode(1)))),
        T(ListNode(1), ListNode(1)),
        T(ListNode(2, ListNode(5)), ListNode(5, ListNode(2))),
    ), reverse_list)()
Exemplo n.º 6
0
#     mins_left = [prices[0]]
#     max_right = deque([prices[-1]])
#     for i in range(1, n):
#         price = prices[i]
#         if mins_left and mins_left[-1] > price:
#             mins_left.append(price)
#         else:
#             mins_left.append(mins_left[-1])
#     for i in range(n - 2, -1, -1):
#         price =  prices[i]
#         if max_right and max_right[0] < price:
#             max_right.appendleft(price)
#         else:
#             max_right.appendleft(max_right[0])
#     max_profit = 0
#     for i in range(0, n):
#         max_profit = max(max_profit, max_right[i] - mins_left[i])
#     return max_profit


if __name__ == '__main__':
    TR(
        (
            T(case=[7,1,2,3,4,5,6], expected=5),
            T([7,6,5,4], 0),
            T([1,2,3,4,10], 9),
            T([2,1,2,1,0,0,1], 1)
        ),
        main
    )()
Exemplo n.º 7
0
    curr_sub_len = 1
    curr_char = s[0]

    stck = [curr_char]

    for char in s[1:]:
        if char == stck[-1]:
            curr_sub_len += 1

            if curr_sub_len > longest_sub_len:
                longest_sub_len = curr_sub_len
                longest_char = char

        else:
            _ = stck.pop()
            stck.append(char)
            curr_char = char
            curr_sub_len = 1

    return longest_sub_len


if __name__ == '__main__':
    TR((
        T("aabaa", 2),
        T("tourist", 1),
        T("abc", 1),
        T("leetcode", 2),
        T("hooraaaaaaaaaaay", 11),
    ), maxPower)()
    ...
Exemplo n.º 8
0
def num_contiguous(nums: List[int]) -> int:
    val_idx_map = defaultdict(list)
    curr, prev, r = 0, 0, 0
    for idx, n in enumerate(nums):
        curr = val_mapper[n] + prev
        if curr == 0: r = max(r, idx + 1)
        val_idx_map[curr].append(idx)
        r = max(r, val_idx_map[curr][-1] - val_idx_map[curr][0])
        prev = curr
    return r


if __name__ == '__main__':
    TR(
        (
            T([0, 0, 0, 1, 1, 1, 0], 6),
            T([0, 0, 1, 1, 1, 0, 0, 0, 0, 1], 8),
            T([0, 0, 0, 0, 0], 0),
            T([1, 1, 1, 1, 1, 1, 1, 1], 0),
            T([1, 0, 1, 1], 2),
            T([], 0),
            T([0], 0),
            T([0, 0, 1, 0, 0, 0, 1, 1], 6),  # actual = 6
            T([0, 0, 1, 0, 0, 0, 0, 1, 1], 4),  # actual = 4
        ),
        num_contiguous)()

# let 0 map to -1 and 1 map to 1, then
# [0,  0, 1, 0, 0, 0, 1,  1] would become
# [-1,-2,-1,-2,-3,-4, -3, -2]