Skip to content

Commit 6321dbb

Browse files
committed
Adding solution of 215, 230, 234, 236, 2338, 239, 240 problems
1 parent 4ae07ef commit 6321dbb

File tree

7 files changed

+225
-0
lines changed

7 files changed

+225
-0
lines changed

200-300q/215.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
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.
3+
4+
Example 1:
5+
6+
Input: [3,2,1,5,6,4] and k = 2
7+
Output: 5
8+
Example 2:
9+
10+
Input: [3,2,3,1,2,4,5,5,6] and k = 4
11+
Output: 4
12+
'''
13+
14+
class Solution(object):
15+
def findKthLargest(self, nums, k):
16+
"""
17+
:type nums: List[int]
18+
:type k: int
19+
:rtype: int
20+
"""
21+
heap = []
22+
import heapq
23+
for num in nums:
24+
heapq.heappush(heap, -(num))
25+
26+
result = 0
27+
for _ in range(k):
28+
result = heapq.heappop(heap)
29+
30+
return -(result)

200-300q/230.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'''
2+
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
3+
4+
Note:
5+
You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
6+
7+
Example 1:
8+
9+
Input: root = [3,1,4,null,2], k = 1
10+
Output: 1
11+
'''
12+
13+
# Definition for a binary tree node.
14+
# class TreeNode(object):
15+
# def __init__(self, x):
16+
# self.val = x
17+
# self.left = None
18+
# self.right = None
19+
20+
class Solution(object):
21+
def kthSmallest(self, root, k):
22+
"""
23+
:type root: TreeNode
24+
:type k: int
25+
:rtype: int
26+
"""
27+
28+
if not root:
29+
return 0
30+
31+
stack = [root]
32+
count, curr = 0, root
33+
34+
35+
while stack:
36+
if curr.left:
37+
stack.append(curr.left)
38+
curr = curr.left
39+
else:
40+
val = stack.pop()
41+
count += 1
42+
if count == k:
43+
return val.val
44+
45+
if val.right:
46+
stack.append(val.right)
47+
curr = val.right
48+
return float('-inf')

200-300q/234.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Definition for singly-linked list.
2+
# class ListNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution(object):
8+
def isPalindrome(self, head):
9+
"""
10+
:type head: ListNode
11+
:rtype: bool
12+
"""
13+
rev = None
14+
slow, fast = head, head.next
15+
while fast and fast.next:
16+
fast = fast.next.next
17+
temp = slow
18+
slow = slow.next
19+
temp.next = rev
20+
rev = temp
21+
22+
if fast:
23+
slow = slow.next
24+
25+
while rev and rev.val == slow.val:
26+
rev = rev.next
27+
slow = slow.next
28+
return not rev

200-300q/236.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'''
2+
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
3+
4+
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).”
5+
6+
Given the following binary search tree: root = [3,5,1,6,2,0,8,null,null,7,4]
7+
8+
_______3______
9+
/ \
10+
___5__ ___1__
11+
/ \ / \
12+
6 _2 0 8
13+
/ \
14+
7 4
15+
Example 1:
16+
17+
Input: root, p = 5, q = 1
18+
Output: 3
19+
Explanation: The LCA of of nodes 5 and 1 is 3.
20+
'''
21+
22+
# Definition for a binary tree node.
23+
# class TreeNode(object):
24+
# def __init__(self, x):
25+
# self.val = x
26+
# self.left = None
27+
# self.right = None
28+
29+
class Solution(object):
30+
def lowestCommonAncestor(self, root, p, q):
31+
"""
32+
:type root: TreeNode
33+
:type p: TreeNode
34+
:type q: TreeNode
35+
:rtype: TreeNode
36+
"""
37+
38+
if not root:
39+
return None
40+
41+
if root == p or root == q:
42+
return root
43+
44+
l = self.lowestCommonAncestor(root.left, p, q)
45+
r = self.lowestCommonAncestor(root.right, p, q)
46+
47+
if l and r:
48+
return root
49+
return l if l else r

200-300q/238.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
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].
3+
4+
Example:
5+
6+
Input: [1,2,3,4]
7+
Output: [24,12,8,6]
8+
1 1 2 6
9+
12 8 6
10+
'''
11+
12+
class Solution(object):
13+
def productExceptSelf(self, nums):
14+
"""
15+
:type nums: List[int]
16+
:rtype: List[int]
17+
"""
18+
if not nums:
19+
return []
20+
21+
dp = [1]*len(nums)
22+
23+
for index in range(1,len(nums)):
24+
dp[index] = dp[index-1]*nums[index-1]
25+
print dp
26+
right = 1
27+
for index in range(len(nums)-1, -1, -1):
28+
dp[index] *= right
29+
right *= nums[index]
30+
return dp

200-300q/239.py

Whitespace-only changes.

200-300q/240.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'''
2+
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
3+
4+
Integers in each row are sorted in ascending from left to right.
5+
Integers in each column are sorted in ascending from top to bottom.
6+
Consider the following matrix:
7+
8+
[
9+
[1, 4, 7, 11, 15],
10+
[2, 5, 8, 12, 19],
11+
[3, 6, 9, 16, 22],
12+
[10, 13, 14, 17, 24],
13+
[18, 21, 23, 26, 30]
14+
]
15+
Example 1:
16+
17+
Input: matrix, target = 5
18+
Output: true
19+
'''
20+
21+
class Solution(object):
22+
def searchMatrix(self, matrix, target):
23+
"""
24+
:type matrix: List[List[int]]
25+
:type target: int
26+
:rtype: bool
27+
"""
28+
29+
if not matrix:
30+
return False
31+
32+
left, right = 0, len(matrix[0])-1
33+
while left < len(matrix) and right >= 0:
34+
if matrix[left][right] == target:
35+
return True
36+
elif matrix[left][right] < target:
37+
left += 1
38+
else:
39+
right -= 1
40+
return False

0 commit comments

Comments
 (0)