-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
29 lines (22 loc) · 1.03 KB
/
solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* @param {number[]} height
* @return {number}
*/
const maxArea = (nums) => {
// O(n)
let l = 0, r = nums.length - 1, maxArea = 0;
while (l < r) {
let area = ((r - l) * Math.min(nums[l], nums[r]));
maxArea = Math.max(area, maxArea);
if (nums[l] >= nums[r]) {
r--;
} else {
l++;
}
}
return maxArea;
}
/*
The space complexity of the given code is O(1) because it uses a constant amount of additional space regardless of the size of the input nums array. The code only declares a few variables (l, r, maxArea, area), but their space usage remains constant.
The time complexity of the code is O(n), where n is the length of the input nums array. The code uses a two-pointer approach to find the maximum area between two lines in the array. It iterates over the array using the two pointers (l and r), and in each iteration, it computes the area and updates the maxArea variable. Since each element in the array is visited once, the time complexity is linear with respect to the size of the input array.
*/