552.Find the Minimum and Maximum with Constant Space
Easy
Given an array
nums, return [minimum, maximum].
Besides time, algorithms use memory. Space complexitySpace ComplexityHow much extra memory an algorithm needs, as a function of the input size n — not counting the input itself. measures the extra memory an algorithm needs beyond its input. Sorting a copy needs a second array as big as the first (O(n) space); a single pass that remembers only the smallest and largest values so far needs two variables, whatever the input size — constant spaceConstant Space O(1)Extra memory that stays the same no matter how large the input is..
Example 1:
Input: nums = [7,-2,9,4]
Output: [-2,9]
Example 2:
Input: nums = [5]
Output: [5,5]
+ 5 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ nums.length ≤ 1000 - ●
-1000000 ≤ nums[i] ≤ 1000000
nums =
[7, -2, 9, 4]