Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a90d058

Browse files
feat: add solutions to lc problem: No.0360 (doocs#4737)
1 parent 0d3d402 commit a90d058

File tree

8 files changed

+449
-252
lines changed

8 files changed

+449
-252
lines changed

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

Lines changed: 154 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ tags:
5757

5858
<!-- solution:start -->
5959

60-
### 方法一
60+
### 方法一:数学 + 双指针
61+
62+
根据数学知识可知,二次函数的图像是一条抛物线,当 $a \gt 0$ 时,抛物线开口向上,顶点为最小值;当 $a \lt 0$ 时,抛物线开口向下,顶点为最大值。
63+
64+
由于数组 $\textit{nums}$ 已经排好序,我们可以使用双指针分别指向数组的两端,根据 $a$ 的正负决定从结果数组的头部还是尾部开始填充较大(或较小)的值。
65+
66+
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。
6167

6268
<!-- tabs:start -->
6369

@@ -68,31 +74,29 @@ class Solution:
6874
def sortTransformedArray(
6975
self, nums: List[int], a: int, b: int, c: int
7076
) -> List[int]:
71-
def f(x):
77+
def f(x: int) -> int:
7278
return a * x * x + b * x + c
7379

7480
n = len(nums)
75-
i, j, k = 0, n -1, 0if a <0else n - 1
76-
res = [0] * n
77-
while i <= j:
78-
v1, v2 = f(nums[i]), f(nums[j])
79-
if a < 0:
80-
if v1 <= v2:
81-
res[k] = v1
81+
i, j= 0, n - 1
82+
ans = [0] * n
83+
for k inrange(n):
84+
y1, y2 = f(nums[i]), f(nums[j])
85+
if a > 0:
86+
if y1 > y2:
87+
ans[n - k -1] = y1
8288
i += 1
8389
else:
84-
res[k] = v2
90+
ans[n - k -1] = y2
8591
j -= 1
86-
k += 1
8792
else:
88-
if v1 >= v2:
89-
res[k] = v1
90-
i += 1
91-
else:
92-
res[k] = v2
93+
if y1 > y2:
94+
ans[k] = y2
9395
j -= 1
94-
k -= 1
95-
return res
96+
else:
97+
ans[k] = y1
98+
i += 1
99+
return ans
96100
```
97101

98102
#### Java
@@ -101,35 +105,33 @@ class Solution:
101105
class Solution {
102106
public int[] sortTransformedArray(int[] nums, int a, int b, int c) {
103107
int n = nums.length;
104-
int i = 0, j = n - 1, k = a < 0 ? 0 : n - 1;
105-
int[] res = new int[n];
106-
while (i <= j) {
107-
int v1 = f(a, b, c, nums[i]), v2 = f(a, b, c, nums[j]);
108-
if (a < 0) {
109-
if (v1 <= v2) {
110-
res[k] = v1;
111-
++i;
108+
int[] ans = new int[n];
109+
int i = 0, j = n - 1;
110+
111+
IntUnaryOperator f = x -> a * x * x + b * x + c;
112+
113+
for (int k = 0; k < n; k++) {
114+
int y1 = f.applyAsInt(nums[i]);
115+
int y2 = f.applyAsInt(nums[j]);
116+
if (a > 0) {
117+
if (y1 > y2) {
118+
ans[n - k - 1] = y1;
119+
i++;
112120
} else {
113-
res[k] = v2;
114-
--j;
121+
ans[n - k -1] = y2;
122+
j--;
115123
}
116-
++k;
117124
} else {
118-
if (v1 >= v2) {
119-
res[k] = v1;
120-
++i;
125+
if (y1 > y2) {
126+
ans[k] = y2;
127+
j--;
121128
} else {
122-
res[k] = v2;
123-
--j;
129+
ans[k] = y1;
130+
i++;
124131
}
125-
--k;
126132
}
127133
}
128-
return res;
129-
}
130-
131-
private int f(int a, int b, int c, int x) {
132-
return a * x * x + b * x + c;
134+
return ans;
133135
}
134136
}
135137
```
@@ -141,35 +143,35 @@ class Solution {
141143
public:
142144
vector<int> sortTransformedArray(vector<int>& nums, int a, int b, int c) {
143145
int n = nums.size();
144-
int i = 0, j = n - 1, k = a < 0 ? 0 : n - 1;
145-
vector<int> res(n);
146-
while (i <= j) {
147-
int v1 = f(a, b, c, nums[i]), v2 = f(a, b, c, nums[j]);
148-
if (a < 0) {
149-
if (v1 <= v2) {
150-
res[k] = v1;
151-
++i;
146+
vector<int> ans(n);
147+
int i = 0, j = n - 1;
148+
149+
auto f = [&](int x) {
150+
return a * x * x + b * x + c;
151+
};
152+
153+
for (int k = 0; k < n; k++) {
154+
int y1 = f(nums[i]);
155+
int y2 = f(nums[j]);
156+
if (a > 0) {
157+
if (y1 > y2) {
158+
ans[n - k - 1] = y1;
159+
i++;
152160
} else {
153-
res[k] = v2;
154-
--j;
161+
ans[n - k - 1] = y2;
162+
j--;
155163
}
156-
++k;
157164
} else {
158-
if (v1 >= v2) {
159-
res[k] = v1;
160-
++i;
165+
if (y1 > y2) {
166+
ans[k] = y2;
167+
j--;
161168
} else {
162-
res[k] = v2;
163-
--j;
169+
ans[k] = y1;
170+
i++;
164171
}
165-
--k;
166172
}
167173
}
168-
return res;
169-
}
170-
171-
int f(int a, int b, int c, int x) {
172-
return a * x * x + b * x + c;
174+
return ans;
173175
}
174176
};
175177
```
@@ -178,42 +180,111 @@ public:
178180

179181
```go
180182
func sortTransformedArray(nums []int, a int, b int, c int) []int {
181-
n := len(nums)
182-
i, j, k := 0, n-1, 0
183-
if a >= 0 {
184-
k = n - 1
183+
f := func(x int) int {
184+
return a*x*x + b*x + c
185185
}
186-
res := make([]int, n)
187-
for i <= j {
188-
v1, v2 := f(a, b, c, nums[i]), f(a, b, c, nums[j])
189-
if a < 0 {
190-
if v1 <= v2 {
191-
res[k] = v1
186+
187+
n := len(nums)
188+
ans := make([]int, n)
189+
i, j := 0, n-1
190+
191+
for k := 0; k < n; k++ {
192+
y1, y2 := f(nums[i]), f(nums[j])
193+
if a > 0 {
194+
if y1 > y2 {
195+
ans[n-k-1] = y1
192196
i++
193197
} else {
194-
res[k] = v2
198+
ans[n-k-1] = y2
195199
j--
196200
}
197-
k++
198201
} else {
199-
if v1 >= v2 {
200-
res[k] = v1
201-
i++
202-
} else {
203-
res[k] = v2
202+
if y1 > y2 {
203+
ans[k] = y2
204204
j--
205+
} else {
206+
ans[k] = y1
207+
i++
205208
}
206-
k--
207209
}
208210
}
209-
return res
211+
return ans
210212
}
213+
```
211214

212-
func f(a, b, c, x int) int {
213-
return a*x*x + b*x + c
215+
#### TypeScript
216+
217+
```ts
218+
function sortTransformedArray(nums: number[], a: number, b: number, c: number): number[] {
219+
const f = (x: number): number => a * x * x + b * x + c;
220+
const n = nums.length;
221+
let [i, j] = [0, n - 1];
222+
const ans: number[] = Array(n);
223+
for (let k = 0; k < n; ++k) {
224+
const y1 = f(nums[i]);
225+
const y2 = f(nums[j]);
226+
if (a > 0) {
227+
if (y1 > y2) {
228+
ans[n - k - 1] = y1;
229+
++i;
230+
} else {
231+
ans[n - k - 1] = y2;
232+
--j;
233+
}
234+
} else {
235+
if (y1 > y2) {
236+
ans[k] = y2;
237+
--j;
238+
} else {
239+
ans[k] = y1;
240+
++i;
241+
}
242+
}
243+
}
244+
return ans;
214245
}
215246
```
216247

248+
#### JavaScript
249+
250+
```js
251+
/**
252+
* @param {number[]} nums
253+
* @param {number} a
254+
* @param {number} b
255+
* @param {number} c
256+
* @return {number[]}
257+
*/
258+
var sortTransformedArray = function (nums, a, b, c) {
259+
const f = x => a * x * x + b * x + c;
260+
const n = nums.length;
261+
let [i, j] = [0, n - 1];
262+
const ans = Array(n);
263+
for (let k = 0; k < n; ++k) {
264+
const y1 = f(nums[i]);
265+
const y2 = f(nums[j]);
266+
if (a > 0) {
267+
if (y1 > y2) {
268+
ans[n - k - 1] = y1;
269+
++i;
270+
} else {
271+
ans[n - k - 1] = y2;
272+
--j;
273+
}
274+
} else {
275+
if (y1 > y2) {
276+
ans[k] = y2;
277+
--j;
278+
} else {
279+
ans[k] = y1;
280+
++i;
281+
}
282+
}
283+
}
284+
return ans;
285+
};
286+
```
287+
217288
<!-- tabs:end -->
218289

219290
<!-- solution:end -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /