Sorting and Searching
Here are some LeetCode problems that involve sorting as an intermediate step in the problem-solving process:
“973. K Closest Points to Origin”: You need to sort the points by their distance to the origin to find the K closest points.
“179. Largest Number”: The problem involves sorting numbers in a specific way to form the largest possible number.
“56. Merge Intervals”: Intervals need to be sorted to easily identify overlapping intervals.
“15. 3Sum”: The problem becomes simpler to solve if the input array is sorted, as we can use the two-pointer approach.
“75. Sort Colors”: The problem requires sorting an array with only three distinct elements.
“148. Sort List”: The problem involves sorting a linked list.
“215. Kth Largest Element in an Array”: A common approach is to sort the array first, making it trivial to find the Kth largest element.
“454. 4Sum II”: By creating sums of pairs and sorting these sums, the problem becomes a two-sum problem.
“57. Insert Interval”: After inserting the new interval, the array of intervals needs to be sorted.
“350. Intersection of Two Arrays II”: Sorting both arrays first makes it straightforward to find their intersection.
Here are some LeetCode problems that involve searching as an intermediate step in the problem-solving process:
“704. Binary Search”: This problem requires you to implement binary search, a fundamental searching algorithm.
“33. Search in Rotated Sorted Array”: This problem is a variant of binary search where the array has been rotated.
“81. Search in Rotated Sorted Array II”: A similar problem to the one above, but this time the array may contain duplicates.
“34. Find First and Last Position of Element in Sorted Array”: This problem involves using binary search to find the first and last occurrence of a target number in a sorted array.
“74. Search a 2D Matrix”: This problem involves searching in a 2D matrix where the rows and columns are sorted.
“240. Search a 2D Matrix II”: This problem involves searching in a 2D matrix where the rows and columns are sorted, but with a different arrangement than the above problem.
“153. Find Minimum in Rotated Sorted Array”: The problem involves finding the minimum in a sorted and rotated array, which can be done using binary search.
“162. Find Peak Element”: Binary search can be used to find a peak element in an array where nums[i] ≠ nums[i+1].
“35. Search Insert Position”: This problem involves finding the position to insert a target value in a sorted array, using binary search.
“278. First Bad Version”: In this problem, you need to use binary search to minimize the number of calls to a given API to find the first bad version.