Array¶
Table of Contents¶
- 1. Two Sum (Easy)
- 121. Best Time to Buy and Sell Stock (Easy)
- 169. Majority Element (Easy)
- 217. Contains Duplicate (Easy)
- 57. Insert Interval (Medium)
- 15. 3Sum (Medium)
- 238. Product of Array Except Self (Medium)
- 39. Combination Sum (Medium)
- 56. Merge Intervals (Medium)
- 75. Sort Colors (Medium)
- 11. Container With Most Water (Medium)
1. Two Sum¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, hash table
- Return the indices of the two numbers such that they add up to a specific target.
Approach | Time Complexity | Space Complexity |
---|---|---|
Hashmap | O(n) | O(n) |
1. Two Sum - Python Solution
from typing import List
# Hashmap
def twoSum(nums: List[int], target: int) -> List[int]:
hashmap = {} # val: idx
for idx, val in enumerate(nums):
if (target - val) in hashmap:
return [hashmap[target - val], idx]
hashmap[val] = idx
nums = [2, 7, 11, 15]
target = 18
assert twoSum(nums, target) == [1, 2]
1. Two Sum - C++ Solution
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
vector<int> twoSum(vector<int> &nums, int target) {
unordered_map<int, int> hashmap;
for (size_t i = 0; i < nums.size(); i++) {
int complement = target - nums[i];
if (hashmap.find(complement) != hashmap.end()) {
return {hashmap[complement], (int)i};
}
hashmap[nums[i]] = (int)i;
}
return {-1, -1};
}
int main() {
vector<int> nums = {2, 7, 11, 15};
int target = 9;
vector<int> result = twoSum(nums, target);
cout << result[0] << ", " << result[1] << endl;
return 0;
}
121. Best Time to Buy and Sell Stock¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, dynamic programming
- Return the maximum profit that can be achieved from buying on one day and selling on another day.
121. Best Time to Buy and Sell Stock - Python Solution
from typing import List
# Brute Force
def maxProfitBF(prices: List[int]) -> int:
max_profit = 0
n = len(prices)
for i in range(n):
for j in range(i + 1, n):
max_profit = max(max_profit, prices[j] - prices[i])
return max_profit
# DP
def maxProfitDP(prices: List[int]) -> int:
dp = [[0] * 2 for _ in range(len(prices))]
dp[0][0] = -prices[0] # buy
dp[0][1] = 0 # sell
for i in range(1, len(prices)):
dp[i][0] = max(dp[i - 1][0], -prices[i]) # the lowest price to buy
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i])
return dp[-1][1]
# Greedy
def maxProfitGreedy(prices: List[int]) -> int:
max_profit = 0
seen_min = prices[0]
for i in range(1, len(prices)):
max_profit = max(max_profit, prices[i] - seen_min)
seen_min = min(seen_min, prices[i])
return max_profit
# Fast Slow Pointers
def maxProfitFS(prices: List[int]) -> int:
max_profit = 0
slow, fast = 0, 1
while fast < len(prices):
if prices[fast] > prices[slow]:
max_profit = max(max_profit, prices[fast] - prices[slow])
else:
slow = fast
fast += 1
return max_profit
# |------------|------- |---------|
# | Approach | Time | Space |
# |------------|--------|---------|
# | Brute Force| O(n^2)| O(1) |
# | DP | O(n) | O(n) |
# | Greedy | O(n) | O(1) |
# | Fast Slow | O(n) | O(1) |
# |------------|--------|---------|
prices = [7, 1, 5, 3, 6, 4]
print(maxProfitBF(prices)) # 5
print(maxProfitDP(prices)) # 5
print(maxProfitGreedy(prices)) # 5
print(maxProfitFS(prices)) # 5
121. Best Time to Buy and Sell Stock - C++ Solution
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
class Solution
{
public:
int maxProfit(vector<int> &prices)
{
if (prices.size() <= 1)
return 0;
int seen_min = prices[0];
int res = 0;
for (int &price : prices)
{
res = max(res, price - seen_min);
seen_min = min(seen_min, price);
}
return res;
}
};
int main()
{
vector<int> prices = {7, 1, 5, 3, 6, 4};
Solution obj;
cout << obj.maxProfit(prices) << endl;
return 0;
}
169. Majority Element¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, hash table, divide and conquer, sorting, counting
- Return the majority element in an array. The majority element is the element that appears more than
n // 2
times.
num |
count |
res |
---|---|---|
2 | 1 | 2 |
2 | 2 | 2 |
1 | 1 | 2 |
1 | 0 | 2 |
1 | 1 | 1 |
2 | 0 | 1 |
2 | 1 | 2 |
169. Majority Element - Python Solution
from collections import defaultdict
from typing import List
# Hash Map
def majorityElementHashMap(nums: List[int]) -> int:
n = len(nums)
freq = defaultdict(int)
for num in nums:
freq[num] += 1
if freq[num] > n // 2:
return num
# Array - Boyer-Moore Voting Algorithm
def majorityElementArray(nums: List[int]) -> int:
res = None
count = 0
for num in nums:
if count == 0:
res = num
count += 1 if num == res else -1
return res
# | Algorithm | Time Complexity | Space Complexity |
# |-----------|-----------------|------------------|
# | HashMap | O(N) | O(N) |
# | Array | O(N) | O(1) |
# |-----------|-----------------|------------------|
nums = [2, 2, 1, 1, 1, 2, 2]
print(majorityElementArray(nums)) # 2
print(majorityElementHashMap(nums)) # 2
217. Contains Duplicate¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, hash table, sorting
- Return True if the array contains any duplicates, otherwise return False.
217. Contains Duplicate - Python Solution
from typing import List
# Brute Force
def containsDuplicateBF(nums: List[int]) -> bool:
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
if nums[i] == nums[j]:
return True
return False
# Sort
def containsDuplicateSort(nums: List[int]) -> bool:
nums.sort()
for i in range(1, len(nums)):
if nums[i] == nums[i - 1]:
return True
return False
# Set
def containsDuplicateSet(nums: List[int]) -> bool:
seen = set()
for i in nums:
if i in seen:
return True
seen.add(i)
return False
# |-------------|-----------------|--------------|
# | Approach | Time | Space |
# |-------------|-----------------|--------------|
# | Brute Force | O(n^2) | O(1) |
# | Sort | O(n log n) | O(1) |
# | Set | O(n) | O(n) |
# |-------------|-----------------|--------------|
print(containsDuplicateBF([1, 2, 3, 1])) # True
print(containsDuplicateSort([1, 2, 3, 1])) # True
print(containsDuplicateSet([1, 2, 3, 1])) # True
57. Insert Interval¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array
57. Insert Interval - Python Solution
from typing import List
# Interval
def insert(
intervals: List[List[int]], newInterval: List[int]
) -> List[List[int]]:
n = len(intervals)
if n == 0:
return [newInterval]
if newInterval[1] < intervals[0][0]:
return [newInterval] + intervals
if newInterval[0] > intervals[-1][1]:
return intervals + [newInterval]
i = 0
result = []
while i < n and intervals[i][1] < newInterval[0]:
result.append(intervals[i])
i += 1
while i < n and intervals[i][0] <= newInterval[1]:
newInterval[0] = min(newInterval[0], intervals[i][0])
newInterval[1] = max(newInterval[1], intervals[i][1])
i += 1
result.append(newInterval)
while i < n:
result.append(intervals[i])
i += 1
return result
intervals = [[1, 3], [6, 9]]
newInterval = [2, 5]
print(insert(intervals, newInterval)) # [[1, 5], [6, 9]]
15. 3Sum¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, two pointers, sorting
15. 3Sum - Python Solution
from typing import List
# Left Right Pointers
def threeSum(nums: List[int]) -> List[List[int]]:
nums.sort()
res = []
n = len(nums)
for i in range(n - 2):
if i > 0 and nums[i] == nums[i - 1]:
continue
left, right = i + 1, n - 1
while left < right:
total = nums[i] + nums[left] + nums[right]
if total > 0:
right -= 1
elif total < 0:
left += 1
else:
res.append([nums[i], nums[left], nums[right]])
while left < right and nums[left] == nums[left + 1]:
left += 1
while left < right and nums[right] == nums[right - 1]:
right -= 1
left += 1
right -= 1
return res
nums = [-1, 0, 1, 2, -1, -4]
assert threeSum(nums) == [[-1, -1, 2], [-1, 0, 1]]
15. 3Sum - C++ Solution
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> res;
int n = nums.size();
for (int i = 0; i < n - 2; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int left = i + 1, right = n - 1;
while (left < right) {
int total = nums[i] + nums[left] + nums[right];
if (total > 0)
right--;
else if (total < 0)
left++;
else {
res.push_back({nums[i], nums[left], nums[right]});
while (left < right && nums[left] == nums[left + 1]) left++;
while (left < right && nums[right] == nums[right - 1]) right--;
left++;
right--;
}
}
}
return res;
}
int main() {
vector<int> nums = {-1, 0, 1, 2, -1, -4};
vector<vector<int>> res = threeSum(nums);
for (auto& v : res) {
for (int i : v) {
cout << i << " ";
}
cout << endl;
}
return 0;
}
238. Product of Array Except Self¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, prefix sum
- Classic Prefix Sum problem
- Return an array
output
such thatoutput[i]
is equal to the product of all the elements ofnums
exceptnums[i]
.
Approach | Time | Space |
---|---|---|
Prefix | O(n) | O(n) |
Prefix (Optimized) | O(n) | O(1) |
238. Product of Array Except Self - Python Solution
from typing import List
# Prefix
def productExceptSelf(nums: List[int]) -> List[int]:
n = len(nums)
prefix = [1 for _ in range(n)]
suffix = [1 for _ in range(n)]
for i in range(1, n):
prefix[i] = nums[i - 1] * prefix[i - 1]
for i in range(n - 2, -1, -1):
suffix[i] = nums[i + 1] * suffix[i + 1]
result = [i * j for i, j in zip(prefix, suffix)]
return result
# Prefix (Optimized)
def productExceptSelfOptimized(nums: List[int]) -> List[int]:
n = len(nums)
result = [1 for _ in range(n)]
prefix = 1
for i in range(n):
result[i] = prefix
prefix *= nums[i]
suffix = 1
for i in range(n - 1, -1, -1):
result[i] *= suffix
suffix *= nums[i]
return result
print(productExceptSelf([1, 2, 3, 4]))
# [24, 12, 8, 6]
print(productExceptSelfOptimized([1, 2, 3, 4]))
# [24, 12, 8, 6]
238. Product of Array Except Self - C++ Solution
#include <vector>
#include <iostream>
using namespace std;
// Prefix Sum
class Solution
{
public:
vector<int> productExceptSelf(vector<int> &nums)
{
int n = nums.size();
vector<int> prefix(n, 1);
vector<int> suffix(n, 1);
vector<int> res(n, 1);
for (int i = 1; i < n; i++)
{
prefix[i] = prefix[i - 1] * nums[i - 1];
}
for (int i = n - 2; i >= 0; i--)
{
suffix[i] = suffix[i + 1] * nums[i + 1];
}
for (int i = 0; i < n; i++)
{
res[i] = prefix[i] * suffix[i];
}
return res;
}
};
int main()
{
vector<int> nums = {1, 2, 3, 4};
Solution obj;
vector<int> result = obj.productExceptSelf(nums);
for (int i = 0; i < result.size(); i++)
{
cout << result[i] << "\n";
}
cout << endl;
// 24, 12, 8, 6
return 0;
}
39. Combination Sum¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, backtracking
39. Combination Sum - Python Solution
from typing import List
def combinationSum(candidates: List[int], target: int) -> List[List[int]]:
n = len(candidates)
res, path = [], []
def dfs(total, start):
if total > target:
return
if total == target:
res.append(path.copy())
return
for i in range(start, n):
total += candidates[i]
path.append(candidates[i])
dfs(total, i)
total -= candidates[i]
path.pop()
dfs(0, 0)
return res
if __name__ == "__main__":
print(combinationSum([2, 3, 5], 8))
# [[2, 2, 2, 2], [2, 3, 3], [3, 5]]
print(combinationSum([2, 3, 6, 7], 7))
# [[2, 2, 3], [7]]
56. Merge Intervals¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, sorting
- Merge all overlapping intervals.
56. Merge Intervals - Python Solution
from typing import List
# Intervals
def merge(intervals: List[List[int]]) -> List[List[int]]:
n = len(intervals)
if n <= 1:
return intervals
intervals.sort(key=lambda x: x[0])
res = [intervals[0]]
for i in range(1, n):
if intervals[i][0] <= res[-1][1]:
res[-1][1] = max(res[-1][1], intervals[i][1])
else:
res.append(intervals[i])
return res
print(merge([[1, 3], [2, 6], [8, 10], [15, 18]]))
# [[1, 6], [8, 10], [15, 18]]
56. Merge Intervals - C++ Solution
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
// Interval
vector<vector<int>> merge(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end());
vector<vector<int>> res;
for (auto& range : intervals) {
if (!res.empty() && range[0] <= res.back()[1]) {
res.back()[1] = max(res.back()[1], range[1]);
} else {
res.emplace_back(range);
}
}
return res;
}
int main() {
vector<vector<int>> intervals = {{1, 3}, {2, 6}, {8, 10}, {15, 18}};
vector<vector<int>> res = merge(intervals);
for (auto& range : res) {
cout << range[0] << ", " << range[1] << endl;
}
return 0;
}
75. Sort Colors¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, two pointers, sorting
75. Sort Colors - Python Solution
from copy import deepcopy
from typing import List
# Left Right Pointers
def sort_colors_lr_pointers(nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
n = len(nums)
left = 0
for right in range(n):
if nums[right] == 0:
nums[left], nums[right] = nums[right], nums[left]
left += 1
for right in range(left, n):
if nums[right] == 1:
nums[left], nums[right] = nums[right], nums[left]
left += 1
# Three Pointers
def sort_colors_three_pointers(nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
left, right = 0, len(nums) - 1
cur = 0
while cur <= right:
if nums[cur] == 0:
nums[left], nums[cur] = nums[cur], nums[left]
left += 1
cur += 1
elif nums[cur] == 2:
nums[right], nums[cur] = nums[cur], nums[right]
right -= 1
else:
cur += 1
nums = [2, 0, 2, 1, 1, 0]
nums1, nums2 = deepcopy(nums), deepcopy(nums)
sort_colors_lr_pointers(nums1)
print(nums1) # [0, 0, 1, 1, 2, 2]
sort_colors_three_pointers(nums2)
print(nums2) # [0, 0, 1, 1, 2, 2]
11. Container With Most Water¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, two pointers, greedy
- Return the maximum area of water that can be trapped between the vertical lines.
11. Container With Most Water - Python Solution
from typing import List
# Brute Force
def maxAreaBF(height: List[int]) -> int:
max_area = 0
for i in range(len(height)):
for j in range(i + 1, len(height)):
h = min(height[i], height[j])
w = j - i
max_area = max(max_area, h * w)
return max_area
# Left Right Pointers
def maxAreaLR(height: List[int]) -> int:
left, right = 0, len(height) - 1
res = 0
while left < right:
h = min(height[left], height[right])
w = right - left
res = max(res, h * w)
if height[left] < height[right]:
left += 1
else:
right -= 1
return res
# |------------|------- |---------|
# | Approach | Time | Space |
# |------------|--------|---------|
# | Brute Force| O(n^2) | O(1) |
# | Left Right | O(n) | O(1) |
# |------------|--------|---------|
height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
print(maxAreaBF(height)) # 49
print(maxAreaLR(height)) # 49
11. Container With Most Water - C++ Solution
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int maxArea(vector<int>& height) {
int left = 0, right = height.size() - 1;
int res = 0;
while (left < right) {
int h = min(height[left], height[right]);
int w = right - left;
res = max(res, h * w);
if (height[left] < height[right])
left++;
else
right--;
}
return res;
}
int main() {
vector<int> height = {1, 8, 6, 2, 5, 4, 8, 3, 7};
cout << maxArea(height) << endl; // 49
return 0;
}