Skip to content

Commit 71d6554

Browse files
committed
Sync LeetCode submission Runtime - 155 ms (54.12%), Memory - 19 MB (10.92%)
1 parent 6f81fe3 commit 71d6554

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1+
# Approach 2: Two Pointer
2+
3+
# Time: O(N)
4+
# Space: O(N)
5+
16
class Solution:
27
def sortedSquares(self, nums: List[int]) -> List[int]:
38
n = len(nums)
4-
left, right = 0, n-1
59
result = [0] * n
6-
7-
for i in range(n-1, -1, -1):
10+
left, right = 0, n - 1
11+
12+
for i in range(n - 1, -1, -1):
813
if abs(nums[left]) < abs(nums[right]):
914
square = nums[right]
1015
right -= 1
1116
else:
1217
square = nums[left]
1318
left += 1
14-
result[i] = square ** 2
15-
19+
result[i] = square * square
20+
1621
return result
1722

0 commit comments

Comments
 (0)