Skip to content

Commit 0e8cee2

Browse files
committed
📝 [11] t&s complexity
1 parent 6eea12f commit 0e8cee2

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

11/solution.js

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* @return {number}
44
*/
55
const maxArea = (nums) => {
6+
// O(n)
67
let l = 0, r = nums.length - 1, maxArea = 0;
78

89
while (l < r) {
@@ -18,3 +19,11 @@ const maxArea = (nums) => {
1819

1920
return maxArea;
2021
}
22+
23+
/*
24+
25+
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.
26+
27+
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.
28+
29+
*/

0 commit comments

Comments
 (0)