LeetCode 0360. Sort Transformed Array Solution in Java, Python, C++, JavaScript, Go & Rust | Explanation + Code

CoderIndeed
0
0360. Sort Transformed Array

Description

Given a sorted integer array nums and three integers a, b and c, apply a quadratic function of the form f(x) = ax2 + bx + c to each element nums[i] in the array, and return the array in a sorted order.

 

Example 1:

Input: nums = [-4,-2,2,4], a = 1, b = 3, c = 5
Output: [3,9,15,33]

Example 2:

Input: nums = [-4,-2,2,4], a = -1, b = 3, c = 5
Output: [-23,-5,1,7]

 

Constraints:

  • 1 <= nums.length <= 200
  • -100 <= nums[i], a, b, c <= 100
  • nums is sorted in ascending order.

 

Follow up: Could you solve it in O(n) time?

Solutions

Solution 1: Math + Two Pointers

By mathematical knowledge, the graph of a quadratic function is a parabola. When a \gt 0, the parabola opens upwards and its vertex is the minimum value; when a \lt 0, the parabola opens downwards and its vertex is the maximum value.

Since the array nums is already sorted, we can use two pointers at both ends of the array. Depending on the sign of a, we decide whether to fill the result array from the beginning or the end with the larger (or smaller) values.

The time complexity is O(n), and the space complexity is O(n), where n is the length of the array nums.

PythonJavaC++GoTypeScriptJavaScript
class Solution: def sortTransformedArray( self, nums: List[int], a: int, b: int, c: int ) -> List[int]: def f(x: int) -> int: return a * x * x + b * x + c n = len(nums) i, j = 0, n - 1 ans = [0] * n for k in range(n): y1, y2 = f(nums[i]), f(nums[j]) if a > 0: if y1 > y2: ans[n - k - 1] = y1 i += 1 else: ans[n - k - 1] = y2 j -= 1 else: if y1 > y2: ans[k] = y2 j -= 1 else: ans[k] = y1 i += 1 return ans(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !