Sort Array With Squares | Algorithm Simplified | Tutorial 10

preview_player
Показать описание
Given an array of both positive and negative integers 'arr[]' which are sorted. The task is to sort the square of the numbers of the Array.

Simple solution is to first convert each array element into its square and then apply any “O(nlogn)” sorting algorithm to sort the array elements.

Рекомендации по теме
Комментарии
Автор

adding the reverse statement at the end makes it O(n)
class Solution(object):
def sortedSquares(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
length = len(nums)
result = [None] * length
index = length-1
i, j = 0, index

while i <= j:
iresult = nums[i] ** 2
jresult = nums[j] ** 2

if iresult >= jresult:
result[index] = iresult
i += 1
else:
result[index] = jresult
j -= 1
index -= 1
return result

madoksback
Автор

why is the bits header file not working in my vscode?

vipulgupta
Автор

how to sort the array in decreasing order

backenddevelop
Автор

U talk to fast I nearly heard anything

chaltunimohussoiuh
Автор

WTF is my second grade child getting this kind of HW?

Badalexander