Leetcode - Arrays |
26. Remove Duplicates from Sorted Array[E]
https://leetcode.com/problems/remove-duplicates-from-sorted-array/
Description
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
Example 1:
1 | Given nums = [1,1,2], |
Example 2:
1 | Given nums = [0,0,1,1,1,2,2,3,3,4], |
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
Internally you can think of this:
1 | // nums is passed in by reference. (i.e., without making a copy) |
Solution
1 | class Solution: |
https://leetcode.com/problems/remove-duplicates-from-sorted-array/solution/
27. Remove Element[E]
Description
Given an array nums and a value val, remove all instances of that value in-place and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.
Example 1:
1 | Given nums = [3,2,2,3], val = 3, |
Example 2:
1 | Given nums = [0,1,2,2,3,0,4,2], val = 2, |
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
Internally you can think of this:
1 | // nums is passed in by reference. (i.e., without making a copy) |
Solution
1 | class Solution: |
33. Search in Rotated Sorted Array[M]
https://leetcode.com/problems/search-in-rotated-sorted-array/
Description
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
Your algorithm’s runtime complexity must be in the order of O(log n).
Example 1:
1 | Input: nums = [4,5,6,7,0,1,2], target = 0 |
Example 2:
1 | Input: nums = [4,5,6,7,0,1,2], target = 3 |
Solution
1 |
34. Find First and Last Position of Element in Sorted Array[M]
https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
Description
Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.
Your algorithm’s runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1].
Example 1:
1 | Input: nums = [5,7,7,8,8,10], target = 8 |
Example 2:
1 | Input: nums = [5,7,7,8,8,10], target = 6 |
Constraints:
0 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9numsis a non decreasing array.-10^9 <= target <= 10^9
Solution
https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/solution/
35. Search Insert Position[E]
https://leetcode.com/problems/search-insert-position/
Description
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Example 1:
1 | Input: [1,3,5,6], 5 |
Example 2:
1 | Input: [1,3,5,6], 2 |
Example 3:
1 | Input: [1,3,5,6], 7 |
Example 4:
1 | Input: [1,3,5,6], 0 |
Solution
1 | class Solution: |
66. Plus One[E]
https://leetcode.com/problems/plus-one/
Description
Given a non-empty array of digits representing a non-negative integer, increment one to the integer.
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit.
You may assume the integer does not contain any leading zero, except the number 0 itself.
Example 1:
1 | Input: [1,2,3] |
Example 2:
1 | Input: [4,3,2,1] |
Solution
1 | class Solution: |
1 | class Solution: |
78. Subsets[M]
https://leetcode.com/problems/subsets/
Description
Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
1 | Input: nums = [1,2,3] |
Solution
https://leetcode.com/problems/subsets/solution/
1 | class Solution: |
90. Subsets II[M]
https://leetcode.com/problems/subsets-ii/
Description
iven a collection of integers that might contain duplicates, *nums*, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
1 | Input: [1,2,2] |
Solution
1 | class Solution: |
162. Find Peak Element[M]
https://leetcode.com/problems/find-peak-element/
Description
A peak element is an element that is greater than its neighbors.
Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index.
The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.
You may imagine that nums[-1] = nums[n] = -∞.
Example 1:
1 | Input: nums = [1,2,3,1] |
Example 2:
1 | Input: nums = [1,2,1,3,5,6,4] |
Follow up: Your solution should be in logarithmic complexity.
Solution
https://leetcode.com/discuss/88467/tricky-problem-tricky-solution
1 |
169. Majority Element[E]
https://leetcode.com/problems/majority-element/
Description
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
You may assume that the array is non-empty and the majority element always exist in the array.
Example 1:
1 | Input: [3,2,3] |
Example 2:
1 | Input: [2,2,1,1,1,2,2] |
Solution
https://leetcode.com/problems/majority-element/solution/
1 |
229. Majority Element II[M]
https://leetcode.com/problems/majority-element-ii/
Description
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times.
Note: The algorithm should run in linear time and in O(1) space.
Example 1:
1 | Input: [3,2,3] |
Example 2:
1 | Input: [1,1,1,3,3,2,2,2] |
Solution
1 |
283. Move Zeroes[E]
https://leetcode.com/problems/move-zeroes/
Description
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
Example:
1 | Input: [0,1,0,3,12] |
Note:
- You must do this in-place without making a copy of the array.
- Minimize the total number of operations.
Solution
https://leetcode.com/problems/move-zeroes/solution/
1 | class Solution: |
605. Can Place Flowers[E]
https://leetcode.com/problems/can-place-flowers/
Description
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die.
Given a flowerbed (represented as an array containing 0 and 1, where 0 means empty and 1 means not empty), and a number n, return if n new flowers can be planted in it without violating the no-adjacent-flowers rule.
Example 1:
1 | Input: flowerbed = [1,0,0,0,1], n = 1 |
Example 2:
1 | Input: flowerbed = [1,0,0,0,1], n = 2 |
Note:
- The input array won’t violate no-adjacent-flowers rule.
- The input array size is in the range of [1, 20000].
- n is a non-negative integer which won’t exceed the input array size.
Solution
https://leetcode.com/problems/can-place-flowers/solution/
1 | class Solution: |
703. Kth Largest Element in a Stream[E]
https://leetcode.com/problems/kth-largest-element-in-a-stream/
Description
Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.
Your KthLargest class will have a constructor which accepts an integer k and an integer array nums, which contains initial elements from the stream. For each call to the method KthLargest.add, return the element representing the kth largest element in the stream.
Example:
1 | int k = 3; |
Note:
You may assume that nums’ length ≥ k-1 and k ≥ 1.
Solution
1 |
724. Find Pivot Index[E]
https://leetcode.com/problems/find-pivot-index/
Description
Given an array of integers nums, write a method that returns the “pivot” index of this array.
We define the pivot index as the index where the sum of all the numbers to the left of the index is equal to the sum of all the numbers to the right of the index.
If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.
Example 1:
1 | Input: nums = [1,7,3,6,5,6] |
Example 2:
1 | Input: nums = [1,2,3] |
Constraints:
- The length of
numswill be in the range[0, 10000]. - Each element
nums[i]will be an integer in the range[-1000, 1000].
Solution
https://leetcode.com/problems/find-pivot-index/solution/
1 | class Solution: |
962. Maximum Width Ramp[M]
https://leetcode.com/problems/maximum-width-ramp/
Description
Given an array A of integers, a ramp is a tuple (i, j) for which i < j and A[i] <= A[j]. The width of such a ramp is j - i.
Find the maximum width of a ramp in A. If one doesn’t exist, return 0.
Example 1:
1 | Input: [6,0,8,2,1,5] |
Example 2:
1 | Input: [9,8,1,0,1,9,4,0,4,1] |
Note:
2 <= A.length <= 500000 <= A[i] <= 50000
Solution
https://leetcode.com/problems/maximum-width-ramp/solution/
1 | class Solution: |
832. Flipping an Image[E]
https://leetcode.com/problems/flipping-an-image/
Description
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image.
To flip an image horizontally means that each row of the image is reversed. For example, flipping [1, 1, 0] horizontally results in [0, 1, 1].
To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0. For example, inverting [0, 1, 1] results in [1, 0, 0].
Example 1:
1 | Input: [[1,1,0],[1,0,1],[0,0,0]] |
Example 2:
1 | Input: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]] |
Notes:
1 <= A.length = A[0].length <= 200 <= A[i][j] <= 1
Solution
https://leetcode.com/problems/flipping-an-image/solution/
1 |
836. Rectangle Overlap[E]
https://leetcode.com/problems/rectangle-overlap/
Description
A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bottom-left corner, and (x2, y2) are the coordinates of its top-right corner.
Two rectangles overlap if the area of their intersection is positive. To be clear, two rectangles that only touch at the corner or edges do not overlap.
Given two (axis-aligned) rectangles, return whether they overlap.
Example 1:
1 | Input: rec1 = [0,0,2,2], rec2 = [1,1,3,3] |
Example 2:
1 | Input: rec1 = [0,0,1,1], rec2 = [1,0,2,1] |
Notes:
- Both rectangles
rec1andrec2are lists of 4 integers. - All coordinates in rectangles will be between
-10^9and10^9.
Solution
https://leetcode.com/problems/rectangle-overlap/solution/
1 |





