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
15 changes: 15 additions & 0 deletions container-with-most-water/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# idea : two-pointer
class Solution:
def maxArea(self, height: List[int]) -> int:
max_area = 0
start, end = 0, len(height) - 1
while start < end:
area = (end - start) * min(height[start], height[end])
max_area = max(max_area, area)
if height[start] < height[end]:
start += 1
else:
end -= 1
return max_area


13 changes: 13 additions & 0 deletions longest-increasing-subsequence/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# idea: DP
# O(N^2)
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
LIS = [1] * len(nums)
for i in range(len(nums) - 1, -1, -1):
for j in range(i+1, len(nums)):
if nums[i] < nums[j]:
LIS[i] = max(LIS[i], 1+LIS[j])
return max(LIS)



34 changes: 34 additions & 0 deletions spiral-matrix/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# idea : -
# O(m*n)
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
res = []
left, right = 0, len(matrix[0])
top, bottom = 0, len(matrix)

while left < right and top < bottom:
# get every i in the top row
for i in range(left, right):
res.append(matrix[top][i])
top+=1

# get every i in the right col
for i in range(top, bottom):
res.append(matrix[i][right - 1])
right -= 1

if not (left < right and top < bottom):
break
#get every i in the bottom row
for i in range(right - 1, left - 1, -1):
res.append(matrix[bottom-1][i])
bottom -= 1

# get every i in the left col
for i in range(bottom -1, top-1, -1):
res.append(matrix[i][left])
left += 1
return res



22 changes: 22 additions & 0 deletions valid-parentheses/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# idea : stack

class Solution:
def isValid(self, s: str) -> bool:
mapping = {"(": ")", "{": "}", "[": "]"}
stack = []

for ch in s:
if ch in mapping:
stack.append(ch)
else:
if not stack:
return False
if mapping[stack.pop()] != ch:
return False
if stack:
return False

return True