Skip to content

Commit e70c655

Browse files
committed
feat: add ts solutions to lc problems: No.2543,2547
* No.2543.Check if Point Is Reachable * No.2547.Minimum Cost to Split an Array
1 parent 0872b7d commit e70c655

File tree

6 files changed

+125
-0
lines changed

6 files changed

+125
-0
lines changed

solution/2500-2599/2543.Check if Point Is Reachable/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@ func gcd(a, b int) int {
117117
}
118118
```
119119

120+
### **TypeScript**
121+
122+
```ts
123+
function isReachable(targetX: number, targetY: number): boolean {
124+
const x = gcd(targetX, targetY);
125+
return (x & (x - 1)) === 0;
126+
}
127+
128+
function gcd(a: number, b: number): number {
129+
return b == 0 ? a : gcd(b, a % b);
130+
}
131+
```
132+
120133
### **...**
121134

122135
```

solution/2500-2599/2543.Check if Point Is Reachable/README_EN.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ func gcd(a, b int) int {
9797
}
9898
```
9999

100+
### **TypeScript**
101+
102+
```ts
103+
function isReachable(targetX: number, targetY: number): boolean {
104+
const x = gcd(targetX, targetY);
105+
return (x & (x - 1)) === 0;
106+
}
107+
108+
function gcd(a: number, b: number): number {
109+
return b == 0 ? a : gcd(b, a % b);
110+
}
111+
```
112+
100113
### **...**
101114

102115
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function isReachable(targetX: number, targetY: number): boolean {
2+
const x = gcd(targetX, targetY);
3+
return (x & (x - 1)) === 0;
4+
}
5+
6+
function gcd(a: number, b: number): number {
7+
return b == 0 ? a : gcd(b, a % b);
8+
}

solution/2500-2599/2547.Minimum Cost to Split an Array/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,38 @@ func min(a, b int) int {
236236
}
237237
```
238238

239+
### **TypeScript**
240+
241+
```ts
242+
function minCost(nums: number[], k: number): number {
243+
const n = nums.length;
244+
const f = new Array(n).fill(0);
245+
const dfs = (i: number) => {
246+
if (i >= n) {
247+
return 0;
248+
}
249+
if (f[i]) {
250+
return f[i];
251+
}
252+
const cnt = new Array(n).fill(0);
253+
let one = 0;
254+
let ans = 1 << 30;
255+
for (let j = i; j < n; ++j) {
256+
const x = ++cnt[nums[j]];
257+
if (x == 1) {
258+
++one;
259+
} else if (x == 2) {
260+
--one;
261+
}
262+
ans = Math.min(ans, k + j - i + 1 - one + dfs(j + 1));
263+
}
264+
f[i] = ans;
265+
return f[i];
266+
};
267+
return dfs(0);
268+
}
269+
```
270+
239271
### **...**
240272

241273
```

solution/2500-2599/2547.Minimum Cost to Split an Array/README_EN.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,38 @@ func min(a, b int) int {
220220
}
221221
```
222222

223+
### **TypeScript**
224+
225+
```ts
226+
function minCost(nums: number[], k: number): number {
227+
const n = nums.length;
228+
const f = new Array(n).fill(0);
229+
const dfs = (i: number) => {
230+
if (i >= n) {
231+
return 0;
232+
}
233+
if (f[i]) {
234+
return f[i];
235+
}
236+
const cnt = new Array(n).fill(0);
237+
let one = 0;
238+
let ans = 1 << 30;
239+
for (let j = i; j < n; ++j) {
240+
const x = ++cnt[nums[j]];
241+
if (x == 1) {
242+
++one;
243+
} else if (x == 2) {
244+
--one;
245+
}
246+
ans = Math.min(ans, k + j - i + 1 - one + dfs(j + 1));
247+
}
248+
f[i] = ans;
249+
return f[i];
250+
};
251+
return dfs(0);
252+
}
253+
```
254+
223255
### **...**
224256

225257
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function minCost(nums: number[], k: number): number {
2+
const n = nums.length;
3+
const f = new Array(n).fill(0);
4+
const dfs = (i: number) => {
5+
if (i >= n) {
6+
return 0;
7+
}
8+
if (f[i]) {
9+
return f[i];
10+
}
11+
const cnt = new Array(n).fill(0);
12+
let one = 0;
13+
let ans = 1 << 30;
14+
for (let j = i; j < n; ++j) {
15+
const x = ++cnt[nums[j]];
16+
if (x == 1) {
17+
++one;
18+
} else if (x == 2) {
19+
--one;
20+
}
21+
ans = Math.min(ans, k + j - i + 1 - one + dfs(j + 1));
22+
}
23+
f[i] = ans;
24+
return f[i];
25+
};
26+
return dfs(0);
27+
}

0 commit comments

Comments
 (0)