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]

Credits:
Special thanks to @elmirap for adding this problem and creating all test cases.

解法1:把每一个数带入方程计算结果,然后对结果排序。Time: O(nlogn),不是题目想要的

解法2:根据抛物线方程式的特点,a>0则抛物线开口朝上,两端的值比中间的大,a<0则抛物线开口朝下,则两端的值比中间的小,a=0时,则为直线方法,是单调递增或递减的。同时因为给定数组nums是有序的,所以才有O(n)的解法。

当a>0,两端的值比中间的大,从后边往中间填数,用两个指针分别指向数组的开头和结尾,比较两个数,将其中较大的数存入res,然后该指针向中间移,重复比较过程,直到把res都填满。

当a<0,两端的值比中间的小,从前边往中间填数,用两个指针分别指向数组的开头和结尾,比较两个数,将其中较小的数存入res,然后该指针向中间移,重复比较过程,直到把res都填满。

当a=0,函数是单调递增或递减的,那么从前往后填和从后往前填都可以,也可以将这种情况和a>0合并。

Java:

public class Solution {
private int calcu(int x, int a, int b, int c){
return a*x*x + b*x + c;
} public int[] sortTransformedArray(int[] nums, int a, int b, int c) {
int index;
if(a > 0){
index = nums.length - 1 ;
} else {
index = 0;
}
int result[] = new int[nums.length];
int i = 0;
int j = nums.length - 1;
if(a > 0){
while(i <= j){
result[index--] = calcu(nums[i],a,b,c) > calcu(nums[j],a,b,c) ? calcu(nums[i++],a,b,c):calcu(nums[j--],a,b,c);
}
}else{
while(i <= j){
result[index++] = calcu(nums[i],a,b,c) < calcu(nums[j],a,b,c) ? calcu(nums[i++],a,b,c):calcu(nums[j--],a,b,c);
}
}
return result;
}
}  

Python: wo

class Solution():
def sortTransformedArray(self, nums, a, b, c):
if not nums:
return []
res = [0] * len(nums)
i, j = 0, len(nums) - 1
index = len(nums) - 1 if a >= 0 else 0
while i <= j:
if a >= 0:
calc_i = self.calc(nums[i], a, b, c)
calc_j = self.calc(nums[j], a, b, c)
if calc_i >= calc_j:
res[index] = calc_i
i += 1
else:
res[index] = calc_j
j -= 1
index -= 1
else:
calc_i = self.calc(nums[i], a, b, c)
calc_j = self.calc(nums[j], a, b, c)
if calc_i <= calc_j:
res[index] = calc_i
i += 1
else:
res[index] = calc_j
j -= 1
index += 1 return res def calc(self, x, a, b, c):
return a * x * x + b * x + c if __name__ == '__main__':
print Solution().sortTransformedArray([], 1, 3, 5)
print Solution().sortTransformedArray([2], 1, 3, 5)
print Solution().sortTransformedArray([-4, -2, 2, 4], 1, 3, 5)
print Solution().sortTransformedArray([-4, -2, 2, 4], -1, 3, 5)
print Solution().sortTransformedArray([-4, -2, 2, 4], 0, 3, 5)

C++:

class Solution {
public:
vector<int> sortTransformedArray(vector<int>& nums, int a, int b, int c) {
int n = nums.size(), i = 0, j = n - 1;
vector<int> res(n);
int idx = a >= 0 ? n - 1 : 0;
while (i <= j) {
if (a >= 0) {
res[idx--] = cal(nums[i], a, b, c) >= cal(nums[j], a, b, c) ? cal(nums[i++], a, b, c) : cal(nums[j--], a, b, c);
} else {
res[idx++] = cal(nums[i], a, b, c) >= cal(nums[j], a, b, c) ? cal(nums[j--], a, b, c) : cal(nums[i++], a, b, c);
}
}
return res;
}
int cal(int x, int a, int b, int c) {
return a * x * x + b * x + c;
}
};

C++:

class Solution {
public:
vector<int> sortTransformedArray(vector<int>& nums, int a, int b, int c) {
if(nums.size() ==0) return {};
vector<int> result;
int left = 0, right = nums.size()-1;
auto func = [=](int x) { return a*x*x + b*x + c; };
while(left <= right)
{
int val1 = func(nums[left]), val2 = func(nums[right]);
if(a > 0) result.push_back(val1>=val2?val1:val2);
if(a > 0) val1>val2?left++:right--;
if(a <= 0) result.push_back(val1>=val2?val2:val1);
if(a <= 0) val1>val2?right--:left++;
}
if(a > 0) reverse(result.begin(), result.end());
return result;
}
};

  

  

 

All LeetCode Questions List 题目汇总

[LeetCode] 360. Sort Transformed Array 排序转换后的数组的更多相关文章

  1. LeetCode 360. Sort Transformed Array

    原题链接在这里:https://leetcode.com/problems/sort-transformed-array/description/ 题目: Given a sorted array o ...

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

    [抄题]: Given a sorted array of integers nums and integer values a, b and c. Apply a quadratic functio ...

  3. 360. Sort Transformed Array

    一元二次方程...仿佛回到了初中. 主要看a的情况来分情况讨论: =0,一次函数,根据b的正负单调递增递减就行了. <0,凸状..从nums[]左右两边开始往中间一边比较一边 从右往左 放: 0 ...

  4. 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( ...

  5. [LeetCode] 912. Sort an Array 数组排序

    Given an array of integers nums, sort the array in ascending order. Example 1: Input: [5,2,3,1] Outp ...

  6. [LeetCode] 75. Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  7. 【LeetCode】Find Minimum in Rotated Sorted Array 找到旋转后有序数组中的最小值

     本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4032570.html 原题: Suppose a sorted array is ...

  8. [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( ...

  9. Sort Transformed Array -- LeetCode

    Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...

随机推荐

  1. moviepy草码

    第一下. # coding=utf-8 from moviepy.editor import * from moviepy.video.tools.subtitles import Subtitles ...

  2. 如何使用git,进行项目的管理

    1.首先,现在git上个创建一个项目, 2.然后在本地创建一个springboot工程 3.使用git命令   git init 将这个springboot项目交给git进行管理 4.创建一个dev分 ...

  3. NumPy的Linalg线性代数库探究

    1.矩阵的行列式 from numpy import * A=mat([[1,2,4,5,7],[9,12,11,8,2],[6,4,3,2,1],[9,1,3,4,5],[0,2,3,4,1]]) ...

  4. HDU - 3644:A Chocolate Manufacturer's Problem(模拟退火, 求多边形内最大圆半径)

    pro:给定一个N边形,然后给半径为R的圆,问是否可以放进去.  问题转化为多边形的最大内接圆半径.(N<50): sol:乍一看,不就是二分+半平面交验证是否有核的板子题吗. 然而事情并没有那 ...

  5. 安装cuda及之后更新环境变量的方法

    安装参考:https://www.cnblogs.com/fanfzj/p/8521728.html 更新环境变量: 将 CUDA.CUPTI 和 cuDNN 安装目录添加到 %PATH% 环境变量中 ...

  6. 实现:调用API函数ShowWindow()来隐藏窗口

    只需要将相应代码复制即可. 代码如下: #include <iostream> #include <windows.h> int main() { HWND hDos; //声 ...

  7. 从.NET/CLR返回的hresult:0x8013XXXX的解释

    什么是0x8013XXXX 有时您可能会遇到从.NET返回的神秘HRESULT,它以0x8013开头,例如0x80131522.不幸的是,Visual Studio附带的错误查找并不能真正处理那些奇怪 ...

  8. Log4net 单独创建配置文件(三)

    1.建立ASP.Net空的Web程序,添加Default.aspx窗体 2.添加web配置文件命名为:log4net.config,添加配置 <?xml version="1.0&qu ...

  9. Android入门教程(二)

    Hello World 项目 首先当我们启动Android Studio的虚拟机时,可以看到第一个项目Hello World,那么虚拟机中的Hello World!是如何书写的呢? 看看虚拟机运行结果 ...

  10. Apache ranger整合hive报错记录

    版本信息如下: hadoop2.9.2 hive 2.x ranger 最新版2.1.0 在hive端部署完ranger 插件以后,在使用beeline连接查询数据库时报错,报错信息如下: verbo ...