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 top interview question of id between 200-300
- Loading branch information
Showing
17 changed files
with
704 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,66 @@ | ||
''' | ||
There are two sorted arrays nums1 and nums2 of size m and n respectively. | ||
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). | ||
Example 1: | ||
nums1 = [1, 3] | ||
nums2 = [2] | ||
The median is 2.0 | ||
Example 2: | ||
nums1 = [1, 2] | ||
nums2 = [3, 4] | ||
The median is (2 + 3)/2 = 2.5 | ||
''' | ||
|
||
class Solution(object): | ||
def findMedianSortedArrays(self, nums1, nums2): | ||
""" | ||
:type nums1: List[int] | ||
:type nums2: List[int] | ||
:rtype: float | ||
""" | ||
|
||
if len(nums1) > len(nums2): | ||
nums1, nums2 = nums2, nums1 | ||
|
||
x, y = len(nums1), len(nums2) | ||
low , high = 0, x | ||
|
||
while low <= high: | ||
partitionx = (low+high)/2 | ||
partitiony = (x+y+1)/2 - partitionx | ||
if partitionx == 0: | ||
maxLeftX = float('-inf') | ||
else: | ||
maxLeftX = nums1[partitionx-1] | ||
|
||
if partitionx == x: | ||
minRightX = float('inf') | ||
else: | ||
minRightX = nums1[partitionx] | ||
|
||
if partitiony == 0: | ||
maxLeftY = float('-inf') | ||
else: | ||
maxLeftY = nums2[partitiony-1] | ||
|
||
if partitiony == y: | ||
minRightY = float('inf') | ||
else: | ||
minRightY = nums2[partitiony] | ||
|
||
if maxLeftX <= minRightY and maxLeftY <= minRightX: | ||
if((x+y)%2 == 0): | ||
return (max(maxLeftX, maxLeftY) + min(minRightX, minRightY))/2.0 | ||
else: | ||
return max(maxLeftY, maxLeftX) | ||
elif maxLeftX > minRightY: | ||
high = partitionx - 1 | ||
else: | ||
low = partitionx + 1 | ||
|
||
|
||
print Solution().findMedianSortedArrays([1,2], [3, 4]) |
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,33 @@ | ||
''' | ||
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. | ||
Example 1: | ||
Input: n = 12 | ||
Output: 3 | ||
Explanation: 12 = 4 + 4 + 4. | ||
Example 2: | ||
Input: n = 13 | ||
Output: 2 | ||
Explanation: 13 = 4 + 9. | ||
''' | ||
|
||
class Solution(object): | ||
def numSquares(self, n): | ||
""" | ||
:type n: int | ||
:rtype: int | ||
""" | ||
mapping = {} | ||
squares = [num*num for num in range(1, int(pow(n, 0.5)) + 1)] | ||
for square in squares: | ||
mapping[square] = 1 | ||
|
||
for val in range(1, n+1): | ||
if val not in mapping: | ||
mapping[val] = float('inf') | ||
for square in squares: | ||
if square < val: | ||
mapping[val] = min(mapping[val], mapping[square] + mapping[val-square]) | ||
return mapping[n] |
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,32 @@ | ||
''' | ||
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. | ||
Example 1: | ||
Input: [1,3,4,2,2] | ||
Output: 2 | ||
Example 2: | ||
Input: [3,1,3,4,2] | ||
Output: 3 | ||
''' | ||
|
||
class Solution(object): | ||
def findDuplicate(self, nums): | ||
""" | ||
:type nums: List[int] | ||
:rtype: int | ||
""" | ||
slow, fast = nums[0], nums[0] | ||
while True: | ||
slow = nums[slow] | ||
fast = nums[nums[fast]] | ||
if slow == fast: | ||
break | ||
|
||
num1= nums[0] | ||
num2 = slow | ||
while num1 != num2: | ||
num1 = nums[num1] | ||
num2 = nums[num2] | ||
return num2 |
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,33 @@ | ||
class Solution(object): | ||
def gameOfLife(self, board): | ||
""" | ||
:type board: List[List[int]] | ||
:rtype: void Do not return anything, modify board in-place instead. | ||
""" | ||
index = [] | ||
|
||
def around(i, j, board): | ||
count = 0 | ||
for k in range(i-1, i+2): | ||
for l in range(j-1, j+2): | ||
if 0<=k < len(board) and 0 <= l < len(board[0]): | ||
if board[k][l] == 1: | ||
count += 1 | ||
|
||
return count-1 if board[i][j] == 1 else count | ||
|
||
for i in range(len(board)): | ||
for j in range(len(board[0])): | ||
count = around(i, j, board) | ||
if board[i][j] == 1: | ||
if count > 3 or count < 2: | ||
index.append([i, j, 0]) | ||
else: | ||
if count == 3: | ||
index.append([i, j, 1]) | ||
|
||
while index: | ||
i, j, value = index.pop() | ||
board[i][j] =value | ||
|
||
|
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,62 @@ | ||
''' | ||
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. | ||
Examples: | ||
[2,3,4] , the median is 3 | ||
[2,3], the median is (2 + 3) / 2 = 2.5 | ||
Design a data structure that supports the following two operations: | ||
void addNum(int num) - Add a integer number from the data stream to the data structure. | ||
double findMedian() - Return the median of all elements so far. | ||
For example: | ||
addNum(1) | ||
addNum(2) | ||
findMedian() -> 1.5 | ||
addNum(3) | ||
findMedian() -> 2 | ||
''' | ||
import heapq | ||
class MedianFinder(object): | ||
|
||
def __init__(self): | ||
""" | ||
initialize your data structure here. | ||
""" | ||
self.max_heap = [] | ||
self.min_heap = [] | ||
|
||
|
||
def addNum(self, num): | ||
""" | ||
:type num: int | ||
:rtype: void | ||
""" | ||
if not self.max_heap or num > -self.max_heap[0]: | ||
heapq.heappush(self.min_heap, num) | ||
|
||
if len(self.min_heap) > len(self.max_heap) + 1: | ||
heapq.heappush(self.max_heap, -heapq.heapop(self.min_heap)) | ||
else: | ||
heapq.heappush(self.max_heap, -num) | ||
if len(self.max_heap) > len(self.min_heap): | ||
heapq.heappush(self.min_heap, -heapq.heapop(self.max_heap)) | ||
|
||
def findMedian(self): | ||
""" | ||
:rtype: float | ||
""" | ||
print self.max_heap, self.min_heap | ||
if len(self.max_heap) == len(self.min_heap): | ||
return (-self.max_heap[0]+self.min_heap[0] )/2.0 | ||
else: | ||
return self.min_heap[0] | ||
|
||
|
||
|
||
# Your MedianFinder object will be instantiated and called as such: | ||
# obj = MedianFinder() | ||
# obj.addNum(num) | ||
# param_2 = obj.findMedian() |
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,43 @@ | ||
''' | ||
Given an unsorted array of integers, find the length of longest increasing subsequence. | ||
For example, | ||
Given [10, 9, 2, 5, 3, 7, 101, 18], | ||
The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than one LIS combination, it is only necessary for you to return the length. | ||
Your algorithm should run in O(n2) complexity. | ||
Follow up: Could you improve it to O(n log n) time complexity? | ||
''' | ||
|
||
class Solution(object): | ||
def lengthOfLIS(self, nums): | ||
""" | ||
:type nums: List[int] | ||
:rtype: int | ||
""" | ||
|
||
if len(nums) <= 1: | ||
return len(nums) | ||
|
||
count = [0 for _ in range(len(nums))] | ||
result = 1 | ||
count[0] = nums[0] | ||
|
||
for index in range(1, len(nums)): | ||
if nums[index] < count[0]: | ||
count[0] = nums[index] | ||
elif nums[index] > count[result-1]: | ||
count[result] = nums[index] | ||
result += 1 | ||
else: | ||
left, right = -1, result-1 | ||
while (right-left > 1): | ||
mid = (left+right)/2 | ||
if count[mid] >= nums[index]: | ||
right = mid | ||
else: | ||
left = mid | ||
count[right] = nums[index] | ||
|
||
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,57 @@ | ||
''' | ||
You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i]. | ||
Example: | ||
Given nums = [5, 2, 6, 1] | ||
To the right of 5 there are 2 smaller elements (2 and 1). | ||
To the right of 2 there is only 1 smaller element (1). | ||
To the right of 6 there is 1 smaller element (1). | ||
To the right of 1 there is 0 smaller element. | ||
Return the array [2, 1, 1, 0]. | ||
''' | ||
|
||
class TreeNode(object): | ||
def __init__(self, val): | ||
self.right = None | ||
self.left = None | ||
self.val = val | ||
self.count = 1 | ||
|
||
class Solution(object): | ||
def countSmaller(self, nums): | ||
""" | ||
:type nums: List[int] | ||
:rtype: List[int] | ||
""" | ||
if len(nums) == 0: | ||
return [] | ||
|
||
node = TreeNode(nums[len(nums)-1]) | ||
result = [0] | ||
for index in range(len(nums)-2, -1, -1): | ||
result.append(self.insertNode(node, nums[index])) | ||
|
||
return result[::-1] | ||
|
||
def insertNode(self, node, val): | ||
totalCount = 0 | ||
while True: | ||
if val <= node.val: | ||
node.count += 1 | ||
if node.left is None: | ||
node.left = TreeNode(val) | ||
break | ||
else: | ||
node = node.left | ||
else: | ||
totalCount += node.count | ||
if node.right is None: | ||
node.right = TreeNode(val) | ||
break | ||
else: | ||
node = node.right | ||
|
||
return totalCount | ||
|
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 @@ | ||
''' | ||
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1. | ||
Example 1: | ||
coins = [1, 2, 5], amount = 11 | ||
return 3 (11 = 5 + 5 + 1) | ||
Example 2: | ||
coins = [2], amount = 3 | ||
return -1. | ||
''' | ||
|
||
class Solution(object): | ||
def coinChange(self, coins, amount): | ||
""" | ||
:type coins: List[int] | ||
:type amount: int | ||
:rtype: int | ||
""" | ||
if not coins: | ||
return 0 | ||
|
||
dp = [float('inf') for _ in range(amount+1)] | ||
dp[0] = 0 | ||
|
||
for val in range(1, amount+1): | ||
for coin in coins: | ||
if coin <= val: | ||
dp[val] = min(dp[val-coin]+1, dp[val]) | ||
return dp[amount] if dp[amount] != float('inf') else -1 |
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,18 @@ | ||
''' | ||
Given an integer, write a function to determine if it is a power of three. | ||
Follow up: | ||
Could you do it without using any loop / recursion? | ||
''' | ||
|
||
class Solution(object): | ||
def isPowerOfThree(self, n): | ||
""" | ||
:type n: int | ||
:rtype: bool | ||
""" | ||
if n <= 0: | ||
return False | ||
|
||
import math | ||
return (math.log10(n)/math.log10(3))%1 == 0 |
Oops, something went wrong.