Given an integer array `nums` sorted in non-decreasing order, return an array of the squares of each number, also sorted in non-decreasing order. Example 1: Input: nums = [-4,-1,0,3,10] Output: [0,1,9,16,100] Explanation: After squaring, the array becomes [16,1,0,9,100]. After sorting, it becomes [0,1,9,16,100]. Example 2: Input: nums = [-7,-3,2,3,11] Output: [4,9,9,49,121] Constraints: - 1 <= nums.length <= [phone] <= nums[i] <= 104 - `nums` is sorted in non-decreasing order.
Asked at
AI answer
Используйте два указателя: квадрат наибольшего по модулю элемента находится на одном из концов отсортированного массива. Сравнивайте квадраты крайних элементов и записывайте больший в конец результирующего массива. Сложность — O(n) по времени и O(n) по дополнительной памяти.
def sorted_squares(nums: list[int]) -> list[int]:
n = len(nums)
result = [0] * n
left, right = 0, n - 1
for i in range(n - 1, -1, -1):
if abs(nums[left]) > abs(nums[right]):
result[i] = nums[left] ** 2
left += 1
else:
result[i] = nums[right] ** 2
right -= 1
return result
Например, для [-4, -1, 0, 3, 10] функция вернёт [0, 1, 9, 16, 100].
Also asked at СБЕР
- Do you have any questions about the company and the role?
- Tell us about your experience: on which projects you worked at [company] and [company], what technologies you used, and what you were responsible for?
- Tell us about yourself: how did you come into development, what teams and products have you worked on, what functionality did you perform?
- What databases have you had to work with as a developer?
- Tell me about the product/project you worked on and your main tasks.
Similar questions
- Return the index of an element in a sorted array that has been cyclically shifted, or -1 if the element is not found. For example, given the array [4, 5, 6, 7, 0, 1, 2], searching for 0 should return 4, while searching for 3 should return -1.
- There are two sorted lists (arrays). # You need to write a function that creates a new sorted list by merging. # 1st list: 1, 2, 2, 5, 7, 14 # 2nd list: 4, 6, 6, 7, 9, 14, 15 # answer: 1, 2, 2, 4, 5, 6, 6, 7, 9, 14, 15
- # 1. Square each number using lambda. # The result should be a list of numbers. L = [1, 2, 3, 4, 5]
- There are two sorted lists (arrays). # You need to write a function that creates a new sorted list with the union. # 1st list: 1, 2, 2, 5, 7, 14 # 2nd list: 2, 2, 2, 4, 6, 6, 7, 9, 14, 15 # answer: 1, 2, 2, 2, 4, 5, 6, 6, 7, 9, 14, 15 def union(arr1: list[int], arr2: list[int]) -> list[int]: pass --- def union(arr1: list[int], arr2: list[int]) -> list[int]: result = [] i = 0 j = 0 while i < len(arr1) and j < len(arr2): if arr1[i] < arr2[j]: result.append(arr1[i]) i += 1 elif arr1[i] > arr2[j]: result.append(arr2[j]) j += 1 else: result.append(arr1[i]) result.append(arr2[j]) i += 1 j += 1 result.extend(arr1[i:]) result.extend(arr2[j:]) return result
- """ Given a sequence of integers. Find the minimal possible product of a pair of elements in the sequence (a pair is two elements, not necessarily consecutive). For example, for the sequence 9 4 2 5 3, the answer will be 6. """ def find_min_product(arr: list[int]) -> int: ...