Array
Array
[ ]: class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
index_map = {}
n = len(nums)
for i in range(n):
complement = target - nums[i]
if complement in index_map:
return [i, index_map[complement]]
else:
index_map[nums[i]] = i
return
1
Input: prices = [7,6,4,3,1] Output: 0 Explanation: In this case, no transactions are done and the
max profit = 0.
[ ]: class Solution:
def maxProfit(self, prices: List[int]) -> int:
for i in range(n):
min_price = min(min_price, prices[i])
max_profit = max(max_profit, prices[i] - min_price)
return max_profit
[ ]: class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
#initialization
n = len(nums)
l, r = [1]*n, [1]*n
result = [1]*n
2
for i in range(n-2, -1, -1):
r[i] = r[i+1] * nums[i+1]
0.5 Maximum Sum Subarray : subarray with the largest sum, and return its
sum.
Given an integer array nums, find the subarray with the largest sum, and return its sum.
Example 1:
Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: The subarray [4,-1,2,1] has the largest
sum 6. Example 2:
Input: nums = [1] Output: 1 Explanation: The subarray [1] has the largest sum 1.
[ ]: class Solution:
def maxSubArray(self, nums: List[int]) -> int:
l , r = 0,0
cur_sum, max_sum = nums[0], nums[0]
n = len(nums)
3
prefix = prefix * nums[i]
suffix = suffix * nums[n-i-1]
max_prod = max(max_prod, max(prefix, suffix))
return max_prod
[ ]: class Solution:
def findMin(self, nums: List[int]) -> int:
start = 0
end = len(nums) - 1
0.8 Search-in-rotated-sorted-array/description
There is an integer array nums sorted in ascending order (with distinct values). Prior to being passed
to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such
that the resulting array is [nums[k], nums[k+1], …, nums[n-1], nums[0], nums[1], …, nums[k-1]] (0-
indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].
4
Given the array nums after the possible rotation and an integer target, return the index of target
if it is in nums, or -1 if it is not in nums.
You must write an algorithm with O(log n) runtime complexity.
Example 1:
Input: nums = [4,5,6,7,0,1,2], target = 0 Output: 4 Example 2:
Input: nums = [4,5,6,7,0,1,2], target = 3 Output: -1
[ ]: class Solution:
def search(self, nums: List[int], target: int) -> int
start = 0
end = len(nums) -1
0.9 3sum
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i
!= k, and j != k, and nums[i] + nums[j] + nums[k] == 0. Notice that the solution set must not
contain duplicate triplets.
Example 1:
Input: nums = [-1,0,1,2,-1,-4] Output: [[-1,-1,2],[-1,0,1]] Explanation: nums[0] + nums[1] + nums[2]
= (-1) + 0 + 1 = 0. nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0. nums[0] + nums[3] +
nums[4] = (-1) + 2 + (-1) = 0. The distinct triplets are [-1,0,1] and [-1,-1,2]. Notice that the order
of the output and the order of the triplets does not matter.
5
Example 2:
Input: nums = [0,1,1] Output: [] Explanation: The only possible triplet does not sum up to 0.
[ ]: class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()
n = len(nums)
result = []
for i in range(n-2):
if i> 0 and nums[i] == nums[i-1]:
continue
j = i+1
k = n-1
while j < k:
if ((nums[i] + nums[j] + nums[k]) == 0) :
result.append([nums[i], nums[j], nums[k]])
j += 1
k -= 1
elif nums[i] + nums[j] + nums[k] < 0:
j += 1
else:
k -= 1
return result
0.10 container-with-most-water
You are given an integer array height of length n. There are n vertical lines drawn such that the
two endpoints of the ith line are (i, 0) and (i, height[i]).
Find two lines that together with the x-axis form a container, such that the container contains the
most water. Return the maximum amount of water a container can store. Notice that you may
not slant the container.
Example 1:
Input: height = [1,8,6,2,5,4,8,3,7] Output: 49 Explanation: The above vertical lines are represented
by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can
contain is 49.
Example 2:
Input: height = [1,1] Output: 1
6
[ ]: class Solution:
def maxArea(self, height: List[int]) -> int:
n = len(height)
i = 0
j = n-1
max_area = float('-inf')
while i < j:
curr_max_area = (j-i)*(min(height[i],height[j]))
max_area = max(max_area, curr_max_area)
if height[i] < height[j]:
i +=1
else:
j -=1
return max_area
[ ]:
[ ]: