Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Square of numbers in non-decreasing order

"""
Given an integer array sorted in non-decreasing order, return an array of the squares of each number sorted in
non-decreasing order.
"""


def sortedSquares(nums):
    n = len(nums)
    start, end = 0, n - 1
    result = [0] * n
    index = n - 1

    while end > -1 and index > -1:
        if abs(nums[start]) > abs(nums[end]):
            result[index] = nums[start] ** 2
            start += 1
        else:
            result[index] = nums[end] ** 2
            end -= 1
        index -= 1

    return result


print(sortedSquares([-4, -3, -2, 0, 1, 2, 5, 10]))
Comment

PREVIOUS NEXT
Code Example
Python :: $ sudo pip install pdml2flow-frame-inter-arrival-time 
Python :: hoe maak je machten in python 
Python :: python return right operand if left is falsy 
Python :: django override help text 
Python :: pandas resample backfill 
Python :: fruit shop using list in python 
Python :: cool advances python ptoject ideas 
Python :: DateTime object representing DateTime in Python 
Python :: pickle save 
Python :: how to tell python to create a random numer 
Python :: pandas show complete string 
Python :: group consecutive numbers in list python 
Python :: how to input multiple integers in python 
Python :: how to flip a list backwards in python 
Python :: python blueprint 
Python :: fourreau de maroquin 
Python :: pandas filter and change value 
Python :: how to get absolute path in python 
Python :: create zero array in python 
Python :: python print exception type and message 
Python :: for loop for multiple scatter plots 
Python :: gonad 
Python :: pil save image 
Python :: default style matplotlib python 
Python :: repeat 10 times python 
Python :: pandas print dataframe dtypes 
Python :: python3 inorder generator 
Python :: print bold and udeline in text python 
Python :: discord python command alias 
Python :: python nameerror input 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =