forked from Garvit244/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding solution of 215, 230, 234, 236, 2338, 239, 240 problems
- Loading branch information
Showing
7 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |