Skip to content

Commit 47c8d77

Browse files
committed
feat: add ts solution to lc problem: No.1144
No.1144.Decrease Elements To Make Array Zigzag
1 parent 8fec0e4 commit 47c8d77

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

solution/1100-1199/1144.Decrease Elements To Make Array Zigzag/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,28 @@ public class Solution {
180180
}
181181
```
182182

183+
### **TypeScript**
184+
185+
```ts
186+
function movesToMakeZigzag(nums: number[]): number {
187+
const ans: number[] = Array(2).fill(0);
188+
const n = nums.length;
189+
for (let i = 0; i < 2; ++i) {
190+
for (let j = i; j < n; j += 2) {
191+
let d = 0;
192+
if (j > 0) {
193+
d = Math.max(d, nums[j] - nums[j - 1] + 1);
194+
}
195+
if (j < n - 1) {
196+
d = Math.max(d, nums[j] - nums[j + 1] + 1);
197+
}
198+
ans[i] += d;
199+
}
200+
}
201+
return Math.min(...ans);
202+
}
203+
```
204+
183205
### **...**
184206

185207
```

solution/1100-1199/1144.Decrease Elements To Make Array Zigzag/README_EN.md

+22
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,28 @@ public class Solution {
166166
}
167167
```
168168

169+
### **TypeScript**
170+
171+
```ts
172+
function movesToMakeZigzag(nums: number[]): number {
173+
const ans: number[] = Array(2).fill(0);
174+
const n = nums.length;
175+
for (let i = 0; i < 2; ++i) {
176+
for (let j = i; j < n; j += 2) {
177+
let d = 0;
178+
if (j > 0) {
179+
d = Math.max(d, nums[j] - nums[j - 1] + 1);
180+
}
181+
if (j < n - 1) {
182+
d = Math.max(d, nums[j] - nums[j + 1] + 1);
183+
}
184+
ans[i] += d;
185+
}
186+
}
187+
return Math.min(...ans);
188+
}
189+
```
190+
169191
### **...**
170192

171193
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function movesToMakeZigzag(nums: number[]): number {
2+
const ans: number[] = Array(2).fill(0);
3+
const n = nums.length;
4+
for (let i = 0; i < 2; ++i) {
5+
for (let j = i; j < n; j += 2) {
6+
let d = 0;
7+
if (j > 0) {
8+
d = Math.max(d, nums[j] - nums[j - 1] + 1);
9+
}
10+
if (j < n - 1) {
11+
d = Math.max(d, nums[j] - nums[j + 1] + 1);
12+
}
13+
ans[i] += d;
14+
}
15+
}
16+
return Math.min(...ans);
17+
}

0 commit comments

Comments
 (0)