Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions linked-list-cycle/doh6077.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
'''
# 141. Linked List Cycle

1. Create an empty list to keep track of visited nodes.
2. Traverse the linked list starting from the head node.

'''
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
record = []
if head == None:
return False

while head.next:
record.append(head)
if head.next in record:
return True
head = head.next
return False
25 changes: 25 additions & 0 deletions maximum-product-subarray/doh6077.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#152. Maximum Product Subarray

# https://leetcode.com/problems/maximum-product-subarray/
#


# Three cases to consider
# negative number
# positive number
# zero - we reset the curMin and curMax to 1 ( neutral element for multiplication)
class Solution:
def maxProduct(self, nums: List[int]) -> int:
res = max(nums)

curMin, curMax = 1, 1

for num in nums:
if num == 0 :
curMin, curMax = 1, 1
continue
temp = curMax * num
curMax = max(curMax * num, curMin * num, num)
curMin = min(curMin * num, temp, num)
res = max(res, curMax)
return res
90 changes: 90 additions & 0 deletions pacific-atlantic-water-flow/doh6077.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# # 417. Pacific Atlantic Water Flow
from collections import deque

class Solution:
# Time Complexity O(m*n)

def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
#Two BFS
# intersection of two hash sets
p_que = deque()
p_seen = set()

a_que = deque()
a_seen = set()

m, n = len(heights), len(heights[0])
for j in range(n):
p_que.append((0,j))
p_seen.add((0,j))

for i in range(1,m):
p_que.append((i,0))
p_seen.add((i,0))

for i in range(m):
a_que.append((i, n-1))
a_seen.add((i, n-1))

for j in range(n-1):
a_que.append((m-1,j))
a_seen.add((m-1,j))

def get_coords(que,seen):
coords = set()
while que:
i, j = que.popleft()
coords.add((i,j))
for i_off, j_off in [(0,1), (1,0), (-1,0), (0,-1)]:
r, c = i+i_off, j +j_off
if 0 <= r < m and 0 <= c < n and heights[r][c] >= heights[i][j] and (r,c) not in seen:
seen.add((r,c))
que.append((r,c))
return coords

p_coords = get_coords(p_que, p_seen)
a_coords = get_coords(a_que, a_seen)
return list(p_coords.intersection(a_coords))

# # First Draft

# class Solution:
# def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:

# # first Attempt
# self.res = []
# self.pacific = []
# self.atlantic = []
# # Brute Force
# # DFS - every single position
# # 1. Pacific Ocean ( in a row: the left side is less than the number) or ( in a column, the previous value is less than the number)
# # 2. Atlantic Ocean ( in a row: the right side is less than the number) or (in a column, the next values are less than the number)
# # 3. if the number satisfies both then we add the coordinates of the number to the result

# def dfs(list_check):
# find_max = max(list_check)
# max_index = list_check.index(find_max)


# # pacific
# if max_index != 0:
# check_paci = [0, max_index]
# self.pacific.append(check_paci)
# if max_index != len(list_check):
# check_atlantic = [max_index, 0]
# self.atlantic.append(check_atlantic)

# if len(heights) == 1:
# return [[0,0]]

# for i, row in enumerate(heights):

# dfs(row)

# columns = list(zip(*heights)) # each column becomes a tuple
# for c, col in enumerate(columns):
# dfs(col)
# print(self.pacific)
# print(self.atlantic)

# return self.res
11 changes: 11 additions & 0 deletions sum-of-two-integers/doh6077.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public int getSum(int a, int b) {
while (b != 0) {
int tmp = (a & b) << 1;
a = a ^ b;
b = tmp;
}
return a;

}
}