360. Sort Transformed Array
Sort Transformed Array http://www.cnblogs.com/grandyang/p/5595614.html solution 1: time nlogn . n is the number of elements in the int[] nums Use heap class Solution { public int[] sortTransformedArray(int[] nums, int a, int b, int c) { PriorityQueue<Integer> pq = new PriorityQueue<>(); // default min heap for(int x : nums){ int result = a * x * x + b * x + c; pq.offer(result); } int[] res = new int[nums.length]; for(int i = 0; i < nums.length; i++){ int cur = pq.poll(); res[i] = cur; } return res; } } Solution 2 : Use math : didn’t use middle = - b/2a , didn’t use it to compare how close the I and j from the middle, instead , directly use product to compare I and j , and two pointers moving towards the inner //. wrong result class Solution { public int[] sortTransformedArray(int[] nums, int a, int b, int c) { // two pointers // when a > 0, the points on the side have biggest , so fill in the res[] from the end // when a < 0, the points on the side have smallest, so fill in the res[] from the start // when a = 0, bx + c , depends on b, it could be increaisng or decreasing // we can fill in the result from either direction int i = 0; int j = nums.length - 1; int[] res = new int[nums.length]; if(a >= 0){ int index = nums.length - 1; while (i <= j){ // int left = calculate(nums[i], a, b, c); // int right = calculate(nums[j], a, b, c); res[index--] = calculate(nums[i], a, b, c) < calculate(nums[j], a, b, c) ? calculate(nums[j--], a, b, c) : calculate(nums[i++], a, b, c); } } if(a < 0){ while (i <= j){ int index = 0; // int left = calculate(nums[i], a, b, c); // int right = calculate(nums[j], a, b, c); res[index++] = calculate(nums[i], a, b, c) < calculate(nums[j], a, b, c) ? calculate(nums[j--], a, b, c) : calculate(nums[i++], a, b, c); } } return res; } private int calculate(int num, int a, int b, int c){ return a * num * num + b * num + c; } } // [-4,-2,2,4] -1 3 5 Output: [-23,0,0,0] Expected: [-23,-5,1,7] // others code public class Solution { public int[] sortTransformedArray(int[] nums, int a, int b, int c) { int n = nums.length; int[] sorted = new int[n]; int i = 0, j = n - 1; int index = a >= 0 ? n - 1 : 0; while (i <= j) { if (a >= 0) { sorted[index--] = quad(nums[i], a, b, c) >= quad(nums[j], a, b, c) ? quad(nums[i++], a, b, c) : quad(nums[j--], a, b, c); } else { sorted[index++] = quad(nums[i], a, b, c) >= quad(nums[j], a, b, c) ? quad(nums[j--], a, b, c) : quad(nums[i++], a, b, c); } } return sorted; } private int quad(int x, int a, int b, int c) { return a * x * x + b * x + c; } }
Solution 1 didn’t use the list is sorted, use a min heap to put all the elements in and poll them from the top until the min heap is empty
Solution 2 : uses the list is sorted and the polynomial function’s graph look, two pointers, o(n)
Given a sorted array of integers nums and integer values a, b and c. Apply a quadratic function of the form f(x) = ax2 + bx + c to each element x in the array.
The returned array must be in sorted order.
Expected time complexity: O(n)
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]
posted on 2018-08-28 20:54 猪猪🐷 阅读(180) 评论(0) 收藏 举报
浙公网安备 33010602011771号