Day - 36
Assalamu alaykum, everyone! How are you, dears? I am posting daily solutions for LeetCode problems! And today is the 36th day of this coding challenge! I am going to say Thanks for you. Because you are spending your time to read my posts, and maybe you are sharing. I will be happy, if my posts help someone.
As I said, consistency is the key to success! I want that you start your own challenges. Because, challenges make you stronger. You learn something to complete the task in your challenge. So, start your own challenges to learn new things!
So, what's today's solution?
/*
?* @param {number[]} nums
?* @return {number[]}
?*/
var sortedSquares = function (nums) {
? const results = new Array(nums.length);
? let left_idx = 0,
? ? right_idx = nums.length - 1,
// keep track where our next insert position
// will be in the results array
? ? next_highest_square_index = nums.length - 1;
? while (left_idx <= right_idx) {
? ? const left_el = Math.pow(nums[left_idx], 2);
? ? const right_el = Math.pow(nums[right_idx], 2);
? ? if (left_el > right_el) {
? ? ? results[next_highest_square_index] = left_el;
? ? ? next_highest_square_index -= 1;
? ? ? left_idx += 1;
? ? } else {
? ? ? results[next_highest_square_index] = right_el;
? ? ? next_highest_square_index -= 1;
? ? ? right_idx -= 1;
? ? }
? }
? return results;
};
That's all. Thanks for your attention. I try to come back with more solutions! See you!