leetcode blind 75 : array problems

preview_player
Показать описание
1. Two Sum
Solution Strategy: Use a hash map to store the difference between the target and the current number. For each number, check if its complement exists in the hash map. If it does, return the indices.
Time Complexity: O(n)
2. Best Time to Buy and Sell Stock
Solution Strategy: Track the minimum price as you iterate through the array. Calculate the maximum profit by comparing the current price with the minimum price.
Time Complexity: O(n)
3. Contains Duplicate
Solution Strategy: Use a set to track the numbers as you iterate. If a number is already in the set, return True. Otherwise, add the number to the set.
Time Complexity: O(n)
4. Product of Array Except Self
Solution Strategy: Use two arrays for prefix and suffix products. The result for each element is the product of all elements to its left (prefix) and right (suffix).
Time Complexity: O(n)
5. Maximum Subarray
Solution Strategy: Use Kadane's algorithm. Track the maximum sum so far and update it as you iterate through the array.
Time Complexity: O(n)
6. Maximum Product Subarray
Solution Strategy: Maintain both the maximum and minimum products at each step. When encountering a negative number, swap the max and min products.
Time Complexity: O(n)
7. Find Minimum in Rotated Sorted Array
Solution Strategy: Use binary search to find the minimum element. Compare the middle element with the rightmost element to decide which half to search.
Time Complexity: O(log n)
8. Search in Rotated Sorted Array
Solution Strategy: Use binary search, taking into account the rotation. Determine which half of the array is sorted and adjust the search range accordingly.
Time Complexity: O(log n)
9. 3 Sum
Solution Strategy: Sort the array and use a two-pointer approach. Fix one element and use two pointers to find the other two elements.
Time Complexity: O(n^2)
10. Container With Most Water
Solution Strategy: Use a two-pointer approach, moving the pointer with the shorter height towards the center to maximize the area.
Time Complexity: O(n)
Рекомендации по теме