Leetcode - Arrays | Sorted Array
75. Sort Colors[M]
https://leetcode.com/problems/sort-colors/
Description
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note: You are not suppose to use the library’s sort function for this problem.
Example:
1 | Input: [2,0,2,1,1,0] |
Follow up:
- A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0’s, 1’s, and 2’s, then overwrite array with total number of 0’s, then 1’s and followed by 2’s. - Could you come up with a one-pass algorithm using only constant space?
Solution
1 | class Solution: |
80. Remove Duplicates from Sorted Array II[M]
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/
Description
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice 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,1,2,2,3], |
Example 2:
1 | Given nums = [0,0,1,1,1,1,2,3,3], |
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 |
153. Find Minimum in Rotated Sorted Array[M]
https://leetcode.com/problems/find-minimum-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]).
Find the minimum element.
You may assume no duplicate exists in the array.
Example 1:
1 | Input: [3,4,5,1,2] |
Example 2:
1 | Input: [4,5,6,7,0,1,2] |
Solution
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/solution/
1 | def findMin(self, nums: List[int]) -> int: |
215. Kth Largest Element in an Array[M]
https://leetcode.com/problems/kth-largest-element-in-an-array/
Description
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
Example 1:
1 | Input: [3,2,1,5,6,4] and k = 2 |
Example 2:
1 | Input: [3,2,3,1,2,4,5,5,6] and k = 4 |
Note:
You may assume k is always valid, 1 ≤ k ≤ array’s length.
Solution
238. Product of Array Except Selfs[M]
https://leetcode.com/problems/product-of-array-except-self/
Description
Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
Example:
1 | Input: [1,2,3,4] |
Constraint: It’s guaranteed that the product of the elements of any prefix or suffix of the array (including the whole array) fits in a 32 bit integer.
Note: Please solve it without division and in O(n).
Follow up:
Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)
Solution
https://leetcode.com/problems/product-of-array-except-self/solution/
1 | class Solution: |
349. Intersection of Two Arrays[E]
https://leetcode.com/problems/intersection-of-two-arrays/
Description
Given two arrays, write a function to compute their intersection.
Example 1:
1 | Input: nums1 = [1,2,2,1], nums2 = [2,2] |
Example 2:
1 | Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] |
Note:
- Each element in the result must be unique.
- The result can be in any order.
Solution
https://leetcode.com/problems/intersection-of-two-arrays/solution/
1 | class Solution: |
350. Intersection of Two Arrays II[E]
https://leetcode.com/problems/intersection-of-two-arrays-ii/
Description
Given two arrays, write a function to compute their intersection.
Example 1:
1 | Input: nums1 = [1,2,2,1], nums2 = [2,2] |
Example 2:
1 | Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] |
Note:
- Each element in the result should appear as many times as it shows in both arrays.
- The result can be in any order.
Follow up:
- What if the given array is already sorted? How would you optimize your algorithm?
- What if nums1’s size is small compared to nums2’s size? Which algorithm is better?
- What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
Solution
1 | class Solution: |
384. Shuffle an Array[M]
https://leetcode.com/problems/shuffle-an-array/
Description
Shuffle a set of numbers without duplicates.
Example:
1 | // Init an array with set 1, 2, and 3. |
Solution
https://leetcode.com/problems/shuffle-an-array/solution/
1 |
453. Minimum Moves to Equal Array Elements[E]
https://leetcode.com/problems/minimum-moves-to-equal-array-elements/
Description
Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1.
Example:
1 | Input: |
Solution
1 | class Solution: |
665. Non-decreasing Array[E]
https://leetcode.com/problems/non-decreasing-array/
Description
Given an array nums with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.
We define an array is non-decreasing if nums[i] <= nums``[i + 1] holds for every i (0-based) such that (0 <= i <= n - 2).
Example 1:
1 | Input: nums = [4,2,3] |
Example 2:
1 | Input: nums = [4,2,1] |
Constraints:
1 <= n <= 10 ^ 4- 10 ^ 5 <= nums[i] <= 10 ^ 5
Solution
1 |
697. Degree of an Array[E]
https://leetcode.com/problems/degree-of-an-array/
Description
Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements.
Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums.
Example 1:
1 | Input: nums = [1,2,2,3,1] |
Example 2:
1 | Input: nums = [1,2,2,3,1,4,2] |
Constraints:
nums.lengthwill be between 1 and 50,000.nums[i]will be an integer between 0 and 49,999.
Solution
1 | class Solution: |
852. Peak Index in a Mountain Array[E]
https://leetcode.com/problems/peak-index-in-a-mountain-array/
Description
Let’s call an array A a mountain if the following properties hold:
A.length >= 3- There exists some
0 < i < A.length - 1such thatA[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]
Given an array that is definitely a mountain, return any i such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1].
Example 1:
1 | Input: [0,1,0] |
Example 2:
1 | Input: [0,2,1,0] |
Note:
3 <= A.length <= 100000 <= A[i] <= 10^6- A is a mountain, as defined above.
Solution
https://leetcode.com/problems/peak-index-in-a-mountain-array/solution/
1 |
905. Sort Array By Paritys[E]
https://leetcode.com/problems/sort-array-by-parity/
Description
Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.
You may return any answer array that satisfies this condition.
Example 1:
1 | Input: [3,1,2,4] |
Note:
1 <= A.length <= 50000 <= A[i] <= 5000
Solution
1 | class Solution: |
922. Sort Array By Parity II[E]
https://leetcode.com/problems/sort-array-by-parity-ii/
Description
Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even.
Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even.
You may return any answer array that satisfies this condition.
Example 1:
1 | Input: [4,2,5,7] |
Note:
2 <= A.length <= 20000A.length % 2 == 00 <= A[i] <= 1000
Solution
https://leetcode.com/problems/sort-array-by-parity-ii/solution/
1 |





