-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquares-of-a-sorted-array.js
69 lines (59 loc) · 1.72 KB
/
squares-of-a-sorted-array.js
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Square of a Sorted Array (https://leetcode.com/problems/squares-of-a-sorted-array/)
- Given an integer array, nums, sorted in non-decreasing order
- Return an array of the squares of each number sorted in non-decreasing order
Constraints:
- There can be negative integers or 0
- There can be duplicates
*/
// ---- Test Cases ----
const nums1 = [-4, -1, 0, 3, 10]; // [0, 1, 9, 16, 100]
const nums2 = [-7, -3, 2, 3, 11]; // [4, 9, 9, 49, 121]
const nums3 = [-1, 1, 1, 5]; // [1, 1, 1, 25]
// ---- Solution 1 ----
/*
Square each element and sort the new array
*/
const sortedSquares = function (nums) {
return nums.map((num) => num ** 2).sort((a, b) => a - b);
};
console.log(sortedSquares(nums1)); // [0, 1, 9, 16, 100]
console.log(sortedSquares(nums2)); // [4, 9, 9, 49, 121]
console.log(sortedSquares(nums3)); // [1, 1, 1, 25]
// ---- Space and Time Complexity 1 ----
/*
Time: O(n log n) // It depends on different browser
Space: O(n)
*/
// ---- Solution 2 ----
/*
Two Pointer Technique
*/
const sortedSquares2 = function (nums) {
const ans = new Array(nums.length);
let left = 0;
let right = nums.length - 1;
let highestPos = nums.length - 1;
while (highestPos >= 0) {
const leftSquaredVal = nums[left] ** 2;
const rightSquaredVal = nums[right] ** 2;
if (leftSquaredVal > rightSquaredVal) {
ans[highestPos] = leftSquaredVal;
highestPos--;
left++;
} else {
ans[highestPos] = rightSquaredVal;
highestPos--;
right--;
}
}
return ans;
};
console.log(sortedSquares2(nums1)); // [0, 1, 9, 16, 100]
console.log(sortedSquares2(nums2)); // [4, 9, 9, 49, 121]
console.log(sortedSquares2(nums3)); // [1, 1, 1, 25]
// ---- Space and Time Complexity 2 ----
/*
Time: O(n)
Space: O(n)
*/