360. Sort Transformed Array

Given a sorted array of integers nums and integer values ab and c. Apply a 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:

nums = [-4, -2, 2, 4], a = 1, b = 3, c = 5,

Result: [3, 9, 15, 33]

nums = [-4, -2, 2, 4], a = -1, b = 3, c = 5

Result: [-23, -5, 1, 7]

最generic经典的方法就是用radix sort
public int[] SortTransformedArray(int[] nums, int a, int b, int c) {
        var result = new int[nums.Count()];
        for(int  i = 0; i< nums.Count() ; i++)
        {
            result[i] = a*nums[i]*nums[i] + b * nums[i] + c;
        }
        RadixSort(result);
        return result;
    }

    public void RadixSort(int[] data)
{
    int i, j;
    int[] temp = new int[data.Length];

    for (int shift = 31; shift > -1; --shift)
    {
        j = 0;

        for (i = 0; i < data.Length; ++i)
        {
            bool move = (data[i] << shift) >= 0;

            if (shift == 0 ? !move : move)
                data[i - j] = data[i];
            else
                temp[j++] = data[i];
        }

        Array.Copy(temp, 0, data, data.Length - j, j);
    }
}
另一种利用了二次方程的性质,利用2 pointer。但是要注意a的正负,如果a是负的,最后得到的结果要reverse,因为reverse是O(n)的,也符合要求。或者如果a<0,index从大往小了走。
public int[] SortTransformedArray(int[] nums, int a, int b, int c) {
        var res = new int[nums.Count()];
        double center = -1*(double)b/(2*a);
        int index =0;
        int i=0;
        for(;i< nums.Count();i++)
        {
            if(nums[i]>=center) break;
        }
        int leftMove = i-1;
        int rightMove =i;

        while(index<nums.Count())
        {
            if(leftMove<0) res[index++] = FunctionT(a,b,c,nums[rightMove++]);
            else if(rightMove>=nums.Count()) res[index++] = FunctionT(a,b,c,nums[leftMove--]);
            else if(Math.Abs(nums[leftMove] - center) - Math.Abs(nums[rightMove] - center) >0)
            {
                res[index++] = FunctionT(a,b,c,nums[rightMove++]);
            }
            else
            {
                res[index++] = FunctionT(a,b,c,nums[leftMove--]);
            }

        }
        if(a<0) Array.Reverse(res);
        return res;
    }

    public int FunctionT(int a, int b,int c, int x)
    {
        return (a*x + b)*x +c;
    }
时间: 2024-12-19 20:37:25

360. Sort Transformed Array的相关文章

Leetcode 360: Sort Transformed Array

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: nums =

360. Sort Transformed Array二元一次方程返回大数序列

[抄题]: 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

Sort Transformed Array

Given a sorted array of integers nums and integer values a, b and c. Apply a 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: nums = [-4, -2,

[LeetCode] Sort Transformed Array 变换数组排序

Given a sorted array of integers nums and integer values a, b and c. Apply a 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: nums = [-4, -2,

leetcode360 - Sort Transformed Array - medium

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: n

Sort the Array

Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers. Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array,

Codeforces Round #258 (Div. 2) B. Sort the Array

Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers. Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array,

Codeforces Round #258 (Div. 2) B. Sort the Array(简单题)

题目链接:http://codeforces.com/contest/451/problem/B ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943/ma

(周日赛)Sort the Array

题意:一段数字,逆置其中两个使其递增 DescriptionBeing a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of ndistinct integers. Unfortunately, the size of a is too small. You want a bigger array! Your friends agre