[Java] Leetcode 977. Squares of a Sorted Array [Two Pointers #4]

preview_player
Показать описание
In this video, I'm going to show you how to solve Leetcode 977. Squares of a Sorted Array which is related toTwo Pointers.

Here’s a quick rundown of what you’re about to learn:
⭐️ Course Contents ⭐️
⌨️ (0:00) Question
⌨️ (1:50) Solution 1 (Large square values to small square values)
⌨️ (5:30) Solution 2 (Small square values to large square values)

In the end, you’ll have a really good understanding on how to solve Leetcode 977. Squares of a Sorted Array and questions that are similar to thisTwo Pointers.

#leetcode #programming #softwareengineering #computerscience
Рекомендации по теме
Комментарии
Автор

Please Make your dsa tutorial public 😢😢

Kaafirpeado-ayesha
Автор

Solution with typescript
function sortedSquares(nums: number[]) {
//base case
let n :number = nums.length ;
if(n < 2 ){
nums[0] = nums[0] ** 2 ;
return nums;
}

//define 2 pointers
let left :number = 0;
let right :number = n - 1 ;
let result :number[] = [] ;
let i :number = n - 1 ;
while(i >= 0){
let check :boolean = nums[left]**2 < nums[right]**2;
if(check){
result[i--] = nums[right]**2;
right -- ;
}else{
result[i--] = nums[left]** 2;
left++;
}
}
return result;
};

minhnhatnguyen
Автор

how is the space complexity O(1) despite creating a new array with size 'n' (the result array)? shouldn't it be O(n) instead?

ahmadhesham
Автор

the first time I tried this exercise with javascript, I wrote this.

Math.pow(nums).sort();

hahaha

ferreiradelima