Skip to content

Commit 4a76d3e

Browse files
committed
0360 added.
1 parent 8bdef52 commit 4a76d3e

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
project(cpp_Sort_Transformed_Array)
3+
4+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
5+
6+
set(SOURCE_FILES main.cpp)
7+
add_executable(cpp_Sort_Transformed_Array ${SOURCE_FILES})
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/// Source : https://leetcode.com/problems/sort-transformed-array/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2016-12-26
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <cassert>
8+
#include <cmath>
9+
10+
using namespace std;
11+
12+
13+
/// Mathematics
14+
/// Based on the middle axis -b/(2a) to get the proper order of the transformation
15+
/// Need to pay attention when a == 0
16+
///
17+
/// Time Complexity: O(n)
18+
/// Space Complexity: O(1)
19+
class Solution {
20+
public:
21+
vector<int> sortTransformedArray(const vector<int>& nums, int a, int b, int c) {
22+
23+
vector<int> res;
24+
25+
if(a == 0){
26+
// If a == 0, the result of the transform is linear
27+
for(int i = 0 ; i < nums.size() ; i ++)
28+
res.push_back(applyTransform(nums[i], a, b, c));
29+
30+
// If b < 0, we need to reverse the result to make the res be sorted
31+
// from small elements to large elements
32+
if(b < 0)
33+
reverseArray(res);
34+
}
35+
else{
36+
// If a != 0, we need to find the middle axis first
37+
// which can be calculated as - b / 2*a
38+
double m = - double(b) / double(2 * a);
39+
//cout<<"m = "<<m<<endl;
40+
41+
// Then, find the first element index which is larger or equal than m
42+
int r = bsearch(nums, m);
43+
int l = r - 1;
44+
//cout<<"l = "<<l<<" , r = "<<r<<endl;
45+
46+
// Similar to merge process,
47+
// we use the two pointers tecnique to make the transform in order
48+
while(l >= 0 || r < nums.size()){
49+
if(l == -1)
50+
res.push_back(applyTransform(nums[r++], a, b, c));
51+
else if(r == nums.size())
52+
res.push_back(applyTransform(nums[l--], a, b, c));
53+
else if(m - double(nums[l]) <= double(nums[r]) - m)
54+
res.push_back(applyTransform(nums[l--], a, b, c));
55+
else // m - double(nums[l]) > double(nums[r]) - m
56+
res.push_back(applyTransform(nums[r++], a, b, c));
57+
}
58+
59+
// If a < 0, we need to reverse the result to make the res be sorted
60+
if(a < 0)
61+
reverseArray(res);
62+
}
63+
64+
return res;
65+
}
66+
67+
private:
68+
int applyTransform(int x, int a, int b, int c){
69+
return a * x * x + b * x + c;
70+
}
71+
72+
void reverseArray(vector<int> &v){
73+
int i = 0, j = v.size() - 1;
74+
while(i < j)
75+
swap(v[i ++] , v[j --]);
76+
return;
77+
}
78+
79+
// Find the index of first element >= target
80+
int bsearch(const vector<int> &vec, double target){
81+
82+
int l = 0, r = vec.size();
83+
while(l < r){
84+
int m = l + (r - l) / 2;
85+
if(vec[m] < target)
86+
l = m + 1;
87+
else // vec[m] >= target
88+
r = m;
89+
}
90+
91+
assert(l == r);
92+
return r;
93+
}
94+
};
95+
96+
97+
void print_vec(const vector<int>& vec){
98+
for(int e: vec)
99+
cout << e << " ";
100+
cout << endl;
101+
}
102+
103+
int main() {
104+
105+
vector<int> nums = {-4, -2, 2, 4};
106+
print_vec(Solution().sortTransformedArray(nums, 1, 3, 5));
107+
print_vec(Solution().sortTransformedArray(nums, -1, 3, 5));
108+
109+
return 0;
110+
}

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
234234
| 349 | [Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/description/) | [] | [C++](0349-Intersection-of-Two-Arrays/cpp-0349/) | [Java](0349-Intersection-of-Two-Arrays/java-0349/src/) | |
235235
| 350 | [Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/description/) | [] | [C++](0350-Intersection-of-Two-Arrays-II/cpp-0350/) | [Java](0350-Intersection-of-Two-Arrays-II/java-0350/src/) | |
236236
| | | | | | |
237+
| 360 | [Sort Transformed Array](https://leetcode.com/problems/sort-transformed-array/description/) | [] | [C++](0360-Sort-Transformed-Array/cpp-0360/) | | |
238+
| | | | | | |
237239
| 370 | [Range Addition](https://leetcode.com/problems/range-addition/description/) | | [C++](0370-Range-Addition/cpp-0370/) | | |
238240
| | | | | | |
239241
| 374 | [Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower/description/) | [solution](https://leetcode.com/problems/guess-number-higher-or-lower/solution/) | [C++](0374-Guess-Number-Higher-or-Lower/cpp-0374/) | | |

0 commit comments

Comments
 (0)