-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path977-squares-of-a-sorted-array.cpp
More file actions
32 lines (26 loc) · 1011 Bytes
/
977-squares-of-a-sorted-array.cpp
File metadata and controls
32 lines (26 loc) · 1011 Bytes
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
30
31
32
// Title: Squares of a Sorted Array
// Description:
// Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.
// Link: https://leetcode.com/problems/squares-of-a-sorted-array/
// Time complexity: O(n)
// Space complexity: O(n)
class Solution {
public:
std::vector<int> sortedSquares(std::vector<int> &nums) {
const std::size_t N = nums.size();
std::vector<int> result(N); std::size_t prevIndex = N;
// remaining range [i, j) to process
std::size_t i = 0, j = N;
// merge numbers from both sides
while (i != j) {
int a = nums[i], b = nums[j-1];
// take the number with a larger absolute value
if (std::abs(a) > std::abs(b)) {
result[--prevIndex] = a * a; ++i;
} else {
result[--prevIndex] = b * b; --j;
}
}
return result;
}
};