LeetCode Arrays
In this post, I’ll explore common array-based LeetCode questions, covering easy and medium difficulty levels.
Problem1: Two Sum
-
Problem Description: Given an array of integers and a target value, determine if there are two numbers in the array that add up to the target. Return the indices of the two numbers.
- Example:
- Input: nums = [2, 7, 11, 15], target = 9
- Output: [0, 1] (Because 2 + 7 = 9)
- There are many ways to solve this question, but for the sake of practicing arrays, we will include array-based solutions, which may not be the most optimized.
Using Arrays | Using Dictionary |
---|---|
This method will be more straightforward but less efficient than using a dictionary. It involves using two loops to check every pair of numbers in the array to see if they add up to the target. 3/13 |
In Python, a dictionary is a type of hash table that can map keys to values. For this problem, we can use a dictionary to remember the numbers we've seen in the array as well as their indices. We'll use this to store the array numbers as keys and their indices as values. 3/13 |
Sorting and Two Pointers | |
First sort the array, then use two pointers to narrow down the search. If the sum is less than the target, move the left pointer forward. If the sum is greater than the target, move the right pointer backward. |
Problem2: Best Time to Buy and Sell Stock
-
Problem Description: You have an array where each element represents the price of a given stock on a single day. Find the maximum profit you can achieve by making a single transaction (buying once and selling once).
-
Example:
- Input: prices = [7, 1, 5, 3, 6, 4]
- Output: 5 (Buy on day 2 when the price is 1, sell on day 5 when the price is 6)
Brute Force | One Pass |
---|---|
Easy to understand |
Tracking the minimum price seen so far as we go through the price list. Using a infinite big value, so every value you see will be min price |
Problem3: Contains Duplicate
-
Problem Description: Determine if a given array of integers contains any duplicate elements.
-
Example:
- Input: nums = [1, 2, 3, 1]
- Output: true
Brute Force | Using Set |
---|---|
comparing every element in the array with every other element. |
we can use a set to store these elements, because sets do not allow duplicate values. As we iterate through the array, we check if the current element is already in the set. If it is, this means we have found a duplicate, and we can return true |
Problem4: Single Number
-
Problem Description: In an array where every number appears twice except for one, find the single number.
-
Example:
- Input: nums = [4, 1, 2, 1, 2]
- Output: 4
Brute Force | ...TBD |
---|---|
|
|
Problem5: Product of Array Except Self
-
Problem Description: Given an array of integers, calculate an output array where each element is the product of all the other elements in the input array without using division.
-
Example:
- Input: nums = [1, 2, 3, 4]
- Output: [24, 12, 8, 6]
Problem6: 3Sum
-
Problem Description: Find all unique triplets in an array that sum to zero.
-
Example:
- Input: nums = [-1, 0, 1, 2, -1, -4]
- Output: [[-1, -1, 2], [-1, 0, 1]]
Problem7: Spiral Matrix
-
Problem Description: Given an m x n matrix, return all elements of the matrix in spiral order.
-
Example:
- Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
- Output: [1,2,3,6,9,8,7,4,5]
Problem8: Container With Most Water
-
Problem Description: You are given an array representing heights of walls. Calculate the maximum amount of water that can be contained between any two walls, assuming they form a container.
-
Example:
- Input: heights = [1,8,6,2,5,4,8,3,7]
- Output: 49