We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent cc707ca commit 6d786d4Copy full SHA for 6d786d4
Squares_of_a_Sorted_Array.py
@@ -0,0 +1,33 @@
1
+# Given an array of integers A sorted in non-decreasing order, return an
2
+# array of the squares of each number, also in sorted non-decreasing order.
3
+#
4
+# Example 1:
5
6
+# Input: [-4,-1,0,3,10]
7
+# Output: [0,1,9,16,100]
8
9
+# Example 2:
10
11
+# Input: [-7,-3,2,3,11]
12
+# Output: [4,9,9,49,121]
13
+
14
15
+class Solution:
16
+ def sortedSquares(self, A):
17
+ negative_stack = []
18
+ res = []
19
20
+ for num in A:
21
+ if num < 0:
22
+ negative_stack.append(num)
23
+ continue
24
25
+ while len(negative_stack) and -negative_stack[-1] <= num:
26
+ res.append(negative_stack.pop() ** 2)
27
28
+ res.append(num ** 2)
29
30
+ while len(negative_stack):
31
32
33
+ return res
0 commit comments