Skip to content

Commit 7bd35e2

Browse files
committed
feat: add solutions to lc problem: No.0360. Sort Transformed Array
1 parent 0d6fda1 commit 7bd35e2

File tree

6 files changed

+449
-4
lines changed

6 files changed

+449
-4
lines changed

solution/0300-0399/0360.Sort Transformed Array/README.md

Lines changed: 158 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,183 @@
2222
<strong>输出: </strong>[-23,-5,1,7]
2323
</pre>
2424

25-
2625
## 解法
2726

2827
<!-- 这里可写通用的实现逻辑 -->
2928

29+
双指针。
30+
31+
利用抛物线的性质,i,j 分别指向排序数组首尾,从两端向中间夹逼。
32+
33+
-`a > 0`,抛物线向上,两端具有最大值,比较两端点的较大值,添加到结果数组。
34+
-`a < 0`,抛物线向下,两端具有最小值,比较两端点的较小值,添加到结果数组。
35+
-`a == 0`,合并到以上的任意一种情况均可。
36+
3037
<!-- tabs:start -->
3138

3239
### **Python3**
3340

3441
<!-- 这里可写当前语言的特殊实现逻辑 -->
3542

3643
```python
37-
44+
class Solution:
45+
def sortTransformedArray(self, nums: List[int], a: int, b: int, c: int) -> List[int]:
46+
def f(x):
47+
return a * x * x + b * x + c
48+
49+
n = len(nums)
50+
i, j, k = 0, n - 1, 0 if a < 0 else n - 1
51+
res = [0] * n
52+
while i <= j:
53+
v1, v2 = f(nums[i]), f(nums[j])
54+
if a < 0:
55+
if v1 <= v2:
56+
res[k] = v1
57+
i += 1
58+
else:
59+
res[k] = v2
60+
j -= 1
61+
k += 1
62+
else:
63+
if v1 >= v2:
64+
res[k] = v1
65+
i += 1
66+
else:
67+
res[k] = v2
68+
j -= 1
69+
k -= 1
70+
return res
3871
```
3972

4073
### **Java**
4174

4275
<!-- 这里可写当前语言的特殊实现逻辑 -->
4376

4477
```java
78+
class Solution {
79+
public int[] sortTransformedArray(int[] nums, int a, int b, int c) {
80+
int n = nums.length;
81+
int i = 0, j = n - 1, k = a < 0 ? 0 : n - 1;
82+
int[] res = new int[n];
83+
while (i <= j) {
84+
int v1 = f(a, b, c, nums[i]), v2 = f(a, b, c, nums[j]);
85+
if (a < 0) {
86+
if (v1 <= v2) {
87+
res[k] = v1;
88+
++i;
89+
} else {
90+
res[k] = v2;
91+
--j;
92+
}
93+
++k;
94+
} else {
95+
if (v1 >= v2) {
96+
res[k] = v1;
97+
++i;
98+
} else {
99+
res[k] = v2;
100+
--j;
101+
}
102+
--k;
103+
}
104+
}
105+
return res;
106+
}
107+
108+
private int f(int a, int b, int c, int x) {
109+
return a * x * x + b * x + c;
110+
}
111+
}
112+
```
113+
114+
### **C++**
115+
116+
```cpp
117+
class Solution {
118+
public:
119+
vector<int> sortTransformedArray(vector<int> &nums, int a, int b, int c) {
120+
int n = nums.size();
121+
int i = 0, j = n - 1, k = a < 0 ? 0 : n - 1;
122+
vector<int> res(n);
123+
while (i <= j)
124+
{
125+
int v1 = f(a, b, c, nums[i]), v2 = f(a, b, c, nums[j]);
126+
if (a < 0)
127+
{
128+
if (v1 <= v2)
129+
{
130+
res[k] = v1;
131+
++i;
132+
}
133+
else
134+
{
135+
res[k] = v2;
136+
--j;
137+
}
138+
++k;
139+
}
140+
else
141+
{
142+
if (v1 >= v2)
143+
{
144+
res[k] = v1;
145+
++i;
146+
}
147+
else
148+
{
149+
res[k] = v2;
150+
--j;
151+
}
152+
--k;
153+
}
154+
}
155+
return res;
156+
}
157+
158+
int f(int a, int b, int c, int x) {
159+
return a * x * x + b * x + c;
160+
}
161+
};
162+
```
45163

164+
### **Go**
165+
166+
```go
167+
func sortTransformedArray(nums []int, a int, b int, c int) []int {
168+
n := len(nums)
169+
i, j, k := 0, n-1, 0
170+
if a >= 0 {
171+
k = n - 1
172+
}
173+
res := make([]int, n)
174+
for i <= j {
175+
v1, v2 := f(a, b, c, nums[i]), f(a, b, c, nums[j])
176+
if a < 0 {
177+
if v1 <= v2 {
178+
res[k] = v1
179+
i++
180+
} else {
181+
res[k] = v2
182+
j--
183+
}
184+
k++
185+
} else {
186+
if v1 >= v2 {
187+
res[k] = v1
188+
i++
189+
} else {
190+
res[k] = v2
191+
j--
192+
}
193+
k--
194+
}
195+
}
196+
return res
197+
}
198+
199+
func f(a, b, c, x int) int {
200+
return a*x*x + b*x + c
201+
}
46202
```
47203

48204
### **...**

solution/0300-0399/0360.Sort Transformed Array/README_EN.md

Lines changed: 150 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,169 @@
2626
<p>&nbsp;</p>
2727
<p><strong>Follow up:</strong> Could you solve it in <code>O(n)</code> time?</p>
2828

29-
3029
## Solutions
3130

3231
<!-- tabs:start -->
3332

3433
### **Python3**
3534

3635
```python
37-
36+
class Solution:
37+
def sortTransformedArray(self, nums: List[int], a: int, b: int, c: int) -> List[int]:
38+
def f(x):
39+
return a * x * x + b * x + c
40+
41+
n = len(nums)
42+
i, j, k = 0, n - 1, 0 if a < 0 else n - 1
43+
res = [0] * n
44+
while i <= j:
45+
v1, v2 = f(nums[i]), f(nums[j])
46+
if a < 0:
47+
if v1 <= v2:
48+
res[k] = v1
49+
i += 1
50+
else:
51+
res[k] = v2
52+
j -= 1
53+
k += 1
54+
else:
55+
if v1 >= v2:
56+
res[k] = v1
57+
i += 1
58+
else:
59+
res[k] = v2
60+
j -= 1
61+
k -= 1
62+
return res
3863
```
3964

4065
### **Java**
4166

4267
```java
68+
class Solution {
69+
public int[] sortTransformedArray(int[] nums, int a, int b, int c) {
70+
int n = nums.length;
71+
int i = 0, j = n - 1, k = a < 0 ? 0 : n - 1;
72+
int[] res = new int[n];
73+
while (i <= j) {
74+
int v1 = f(a, b, c, nums[i]), v2 = f(a, b, c, nums[j]);
75+
if (a < 0) {
76+
if (v1 <= v2) {
77+
res[k] = v1;
78+
++i;
79+
} else {
80+
res[k] = v2;
81+
--j;
82+
}
83+
++k;
84+
} else {
85+
if (v1 >= v2) {
86+
res[k] = v1;
87+
++i;
88+
} else {
89+
res[k] = v2;
90+
--j;
91+
}
92+
--k;
93+
}
94+
}
95+
return res;
96+
}
97+
98+
private int f(int a, int b, int c, int x) {
99+
return a * x * x + b * x + c;
100+
}
101+
}
102+
```
103+
104+
### **C++**
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
vector<int> sortTransformedArray(vector<int> &nums, int a, int b, int c) {
110+
int n = nums.size();
111+
int i = 0, j = n - 1, k = a < 0 ? 0 : n - 1;
112+
vector<int> res(n);
113+
while (i <= j)
114+
{
115+
int v1 = f(a, b, c, nums[i]), v2 = f(a, b, c, nums[j]);
116+
if (a < 0)
117+
{
118+
if (v1 <= v2)
119+
{
120+
res[k] = v1;
121+
++i;
122+
}
123+
else
124+
{
125+
res[k] = v2;
126+
--j;
127+
}
128+
++k;
129+
}
130+
else
131+
{
132+
if (v1 >= v2)
133+
{
134+
res[k] = v1;
135+
++i;
136+
}
137+
else
138+
{
139+
res[k] = v2;
140+
--j;
141+
}
142+
--k;
143+
}
144+
}
145+
return res;
146+
}
147+
148+
int f(int a, int b, int c, int x) {
149+
return a * x * x + b * x + c;
150+
}
151+
};
152+
```
43153

154+
### **Go**
155+
156+
```go
157+
func sortTransformedArray(nums []int, a int, b int, c int) []int {
158+
n := len(nums)
159+
i, j, k := 0, n-1, 0
160+
if a >= 0 {
161+
k = n - 1
162+
}
163+
res := make([]int, n)
164+
for i <= j {
165+
v1, v2 := f(a, b, c, nums[i]), f(a, b, c, nums[j])
166+
if a < 0 {
167+
if v1 <= v2 {
168+
res[k] = v1
169+
i++
170+
} else {
171+
res[k] = v2
172+
j--
173+
}
174+
k++
175+
} else {
176+
if v1 >= v2 {
177+
res[k] = v1
178+
i++
179+
} else {
180+
res[k] = v2
181+
j--
182+
}
183+
k--
184+
}
185+
}
186+
return res
187+
}
188+
189+
func f(a, b, c, x int) int {
190+
return a*x*x + b*x + c
191+
}
44192
```
45193

46194
### **...**

0 commit comments

Comments
 (0)