Skip to content

Commit

Permalink
Adding solution of 215, 230, 234, 236, 2338, 239, 240 problems
Browse files Browse the repository at this point in the history
  • Loading branch information
Garvit244 committed May 12, 2018
1 parent 4ae07ef commit 6321dbb
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 0 deletions.
30 changes: 30 additions & 0 deletions 200-300q/215.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'''
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
Example 1:
Input: [3,2,1,5,6,4] and k = 2
Output: 5
Example 2:
Input: [3,2,3,1,2,4,5,5,6] and k = 4
Output: 4
'''

class Solution(object):
def findKthLargest(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
heap = []
import heapq
for num in nums:
heapq.heappush(heap, -(num))

result = 0
for _ in range(k):
result = heapq.heappop(heap)

return -(result)
48 changes: 48 additions & 0 deletions 200-300q/230.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'''
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
Note:
You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
Example 1:
Input: root = [3,1,4,null,2], k = 1
Output: 1
'''

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution(object):
def kthSmallest(self, root, k):
"""
:type root: TreeNode
:type k: int
:rtype: int
"""

if not root:
return 0

stack = [root]
count, curr = 0, root


while stack:
if curr.left:
stack.append(curr.left)
curr = curr.left
else:
val = stack.pop()
count += 1
if count == k:
return val.val

if val.right:
stack.append(val.right)
curr = val.right
return float('-inf')
28 changes: 28 additions & 0 deletions 200-300q/234.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution(object):
def isPalindrome(self, head):
"""
:type head: ListNode
:rtype: bool
"""
rev = None
slow, fast = head, head.next
while fast and fast.next:
fast = fast.next.next
temp = slow
slow = slow.next
temp.next = rev
rev = temp

if fast:
slow = slow.next

while rev and rev.val == slow.val:
rev = rev.next
slow = slow.next
return not rev
49 changes: 49 additions & 0 deletions 200-300q/236.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'''
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
Given the following binary search tree: root = [3,5,1,6,2,0,8,null,null,7,4]
_______3______
/ \
___5__ ___1__
/ \ / \
6 _2 0 8
/ \
7 4
Example 1:
Input: root, p = 5, q = 1
Output: 3
Explanation: The LCA of of nodes 5 and 1 is 3.
'''

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution(object):
def lowestCommonAncestor(self, root, p, q):
"""
:type root: TreeNode
:type p: TreeNode
:type q: TreeNode
:rtype: TreeNode
"""

if not root:
return None

if root == p or root == q:
return root

l = self.lowestCommonAncestor(root.left, p, q)
r = self.lowestCommonAncestor(root.right, p, q)

if l and r:
return root
return l if l else r
30 changes: 30 additions & 0 deletions 200-300q/238.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'''
Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
Example:
Input: [1,2,3,4]
Output: [24,12,8,6]
1 1 2 6
12 8 6
'''

class Solution(object):
def productExceptSelf(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
if not nums:
return []

dp = [1]*len(nums)

for index in range(1,len(nums)):
dp[index] = dp[index-1]*nums[index-1]
print dp
right = 1
for index in range(len(nums)-1, -1, -1):
dp[index] *= right
right *= nums[index]
return dp
Empty file added 200-300q/239.py
Empty file.
40 changes: 40 additions & 0 deletions 200-300q/240.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'''
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
Consider the following matrix:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Example 1:
Input: matrix, target = 5
Output: true
'''

class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""

if not matrix:
return False

left, right = 0, len(matrix[0])-1
while left < len(matrix) and right >= 0:
if matrix[left][right] == target:
return True
elif matrix[left][right] < target:
left += 1
else:
right -= 1
return False

0 comments on commit 6321dbb

Please sign in to comment.