Skip to content

Commit 3cf3e1f

Browse files
committed
problem: 0977 squares of a sorted array
1 parent fe78ff7 commit 3cf3e1f

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# 0977. Squares of a Sorted Array
2+
3+
* Difficulty: easy
4+
* Link: https://leetcode.com/problems/squares-of-a-sorted-array/
5+
* Topics: Array-String
6+
7+
# Clarification
8+
9+
1. Check the inputs and outputs
10+
- INPUT: List[int]
11+
- OUTPUT: List[int]
12+
13+
# Naive Solution
14+
15+
### Thought Process
16+
17+
1. squares of each number
18+
2. sort it
19+
- Implement
20+
21+
```python
22+
class Solution:
23+
def sortedSquares(self, nums: List[int]) -> List[int]:
24+
"""
25+
1. squares of each number
26+
2. sort it
27+
"""
28+
for idx, num in enumerate(nums):
29+
nums[idx] = num ** 2
30+
nums.sort()
31+
return nums
32+
```
33+
34+
35+
### Complexity
36+
37+
- Time complexity:$O(nlogn)$
38+
39+
![Untitled](./Untitled.png)
40+
41+
- Space complexity:$O(1)$
42+
43+
### Problems & Improvement
44+
45+
- **Follow up:** Squaring each element and sorting the new array is very trivial, could you find an `O(n)` solution using a different approach?
46+
47+
# Improvement
48+
49+
### Thought Process
50+
51+
- `nums` is sorted in **non-decreasing** order.
52+
1. split the array into two array
53+
1. negative array
54+
2. positive array
55+
56+
```python
57+
[-4,-1,0,3,10]
58+
^^^^^ ^^^^^^
59+
```
60+
61+
2. Reverse the negative array
62+
3. begin to traverse two array
63+
1. square the num and check which element is larger, insert the larger one
64+
- Implement
65+
66+
```python
67+
class Solution:
68+
def sortedSquares(self, nums: List[int]) -> List[int]:
69+
# split the array into two array (by linked list)
70+
positive = []
71+
negative = nums
72+
for idx, num in enumerate(nums):
73+
if num >= 0:
74+
positive = nums[idx:]
75+
negative = nums[:idx]
76+
break
77+
78+
# reverse the negative array
79+
negative = negative[::-1]
80+
81+
# traverse the array
82+
result = []
83+
p, n = 0, 0
84+
while p < len(positive) and n < len(negative):
85+
if abs(negative[n]) < abs(positive[p]):
86+
result.append(negative[n] ** 2)
87+
n += 1
88+
else:
89+
result.append(positive[p] ** 2)
90+
p += 1
91+
for i in range(p, len(positive)):
92+
result.append(positive[i] ** 2)
93+
for i in range(n, len(negative)):
94+
result.append(negative[i] ** 2)
95+
96+
return result
97+
```
98+
99+
100+
### Complexity
101+
102+
- Time complexity: $O(n)$
103+
104+
![Untitled](./Untitled%201.png)
105+
106+
- Space complexity:$O(n)$
107+
108+
# Improvement
109+
110+
### Thought Process
111+
112+
- 可以不用 Reverse Array,因為 **non-decreasing** order,兩側最大,從兩側往內夾擊
113+
- implement
114+
115+
```python
116+
class Solution:
117+
def sortedSquares(self, nums: List[int]) -> List[int]:
118+
# split the array into two array (by linked list)
119+
right = len(nums) - 1
120+
left = 0
121+
# traverse the array
122+
result = [0] * len(nums)
123+
resultIdx = len(nums) - 1
124+
while resultIdx >=0 :
125+
if abs(nums[left]) > abs(nums[right]):
126+
result[resultIdx] = nums[left] ** 2
127+
left += 1
128+
else:
129+
result[resultIdx] = nums[right] ** 2
130+
right -= 1
131+
resultIdx -=1
132+
133+
return result
134+
```
135+
136+
137+
### Complexity
138+
139+
- Time complexity: $O(n)$
140+
141+
![Untitled](./Untitled%202.png)
142+
143+
- Space complexity:$O(1)$
144+
145+
# Note
146+
147+
- **[Python: A comparison of lots of approaches! [Sorting, two pointers, deque, iterator, generator]](https://leetcode.com/problems/squares-of-a-sorted-array/discuss/310865/Python%3A-A-comparison-of-lots-of-approaches!-Sorting-two-pointers-deque-iterator-generator)**
65 KB
Loading
36.5 KB
Loading
18.6 KB
Loading

0 commit comments

Comments
 (0)