- Notifications
You must be signed in to change notification settings - Fork0
0977: Squares of a sorted array#4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions0977-Squares_of_a_Sorted_Array/main.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// let nums = [-4, -1, 0, 3, 10]; | ||
let nums = [-7, -3, 2, 3, 11]; | ||
function sortedSquaresByMapping(nums: number[]): number[] { | ||
return nums.map((i) => i * i).sort((a, b) => a - b); | ||
} | ||
function sortedSquaresFromTwoEnds(nums: number[]): number[] { | ||
const ans = new Array(nums.length); | ||
let left = 0, | ||
right = nums.length - 1; | ||
for (let i = nums.length - 1; i >= 0; i--) { | ||
if (Math.abs(nums[left]) > Math.abs(nums[right])) { | ||
ans[i] = nums[left] * nums[left]; | ||
left++; | ||
} else { | ||
ans[i] = nums[right] * nums[right]; | ||
right--; | ||
} | ||
} | ||
return ans; | ||
} | ||
function sortedSquaresByUnshifting(nums: number[]): number[] { | ||
const ans: number[] = []; | ||
let left = 0, | ||
right = nums.length - 1; | ||
while (left <= right) { | ||
if (Math.abs(nums[left]) > nums[right]) { | ||
ans.unshift(nums[left] ** 2); | ||
left++; | ||
} else { | ||
ans.unshift(nums[right] ** 2); | ||
right--; | ||
} | ||
} | ||
return ans; | ||
} | ||
console.time("sortedSquaresByMapping"); | ||
let resultByMapping = sortedSquaresByMapping(nums); | ||
console.timeEnd("sortedSquaresByMapping"); | ||
console.time("sortedSquaresFromTwoEnds"); | ||
let resultFromTwoEnds = sortedSquaresFromTwoEnds(nums); | ||
console.timeEnd("sortedSquaresFromTwoEnds"); | ||
console.time("sortedSquaresByUnshifting"); | ||
let resultByUnshifting = sortedSquaresByUnshifting(nums); | ||
console.timeEnd("sortedSquaresByUnshifting"); | ||
console.log(resultByUnshifting); | ||
console.log(resultByMapping); | ||
console.log(resultFromTwoEnds); |
21 changes: 21 additions & 0 deletions0977-Squares_of_a_Sorted_Array/readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# 977. Squares of a Sorted Array | ||
Given an integer array `nums` sorted in <b>non-decreasing</b> order, return <i>an array of <b>the squares of each number</b> sorted in non-decreasing order</i>. | ||
## Example 1: | ||
> <span style="color: white;">Input</span>: nums = [-4, -1, 0, 3, 10]<br> | ||
> <span style="color: white;">Output</span>: [0, 1, 9, 16, 100]<br> | ||
> <span style="color: white;">Explanation</span>: After squaring, the array becomes [16, 1, 0, 9, 100].<br> | ||
> After sorting, it becomes [0, 1, 9, 16, 100]. | ||
## Example 2: | ||
> <span style="color: white;">Input</span>: nums = [-7, -3, 2, 3, 11]<br> | ||
> <span style="color: white;">Output</span>: [4, 9, 9, 49, 121] | ||
## Constraints: | ||
- 1 <= nums.length <= 10<sup>4</sup> | ||
- -10<sup>4</sup> <= nums[i] <= 10<sup>4</sup> | ||
- `nums` is sorted in <b>non-decreasing</b> order. |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.