Skip to content

Commit e08f8dc

Browse files
authored
feat: add solutions to lc problems: No.2574~2576 (doocs#900)
1 parent 34c3429 commit e08f8dc

File tree

9 files changed

+132
-0
lines changed

9 files changed

+132
-0
lines changed

solution/2500-2599/2574.Left and Right Sum Differences/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,22 @@ func abs(x int) int {
143143
}
144144
```
145145

146+
### **TypeScript**
147+
148+
```ts
149+
function leftRigthDifference(nums: number[]): number[] {
150+
let left = 0,
151+
right = nums.reduce((a, b) => a + b);
152+
const ans: number[] = [];
153+
for (const x of nums) {
154+
right -= x;
155+
ans.push(Math.abs(left - right));
156+
left += x;
157+
}
158+
return ans;
159+
}
160+
```
161+
146162
### **...**
147163

148164
```

solution/2500-2599/2574.Left and Right Sum Differences/README_EN.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,22 @@ func abs(x int) int {
125125
}
126126
```
127127

128+
### **TypeScript**
129+
130+
```ts
131+
function leftRigthDifference(nums: number[]): number[] {
132+
let left = 0,
133+
right = nums.reduce((a, b) => a + b);
134+
const ans: number[] = [];
135+
for (const x of nums) {
136+
right -= x;
137+
ans.push(Math.abs(left - right));
138+
left += x;
139+
}
140+
return ans;
141+
}
142+
```
143+
128144
### **...**
129145

130146
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function leftRigthDifference(nums: number[]): number[] {
2+
let left = 0,
3+
right = nums.reduce((a, b) => a + b);
4+
const ans: number[] = [];
5+
for (const x of nums) {
6+
right -= x;
7+
ans.push(Math.abs(left - right));
8+
left += x;
9+
}
10+
return ans;
11+
}

solution/2500-2599/2575.Find the Divisibility Array of a String/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ func divisibilityArray(word string, m int) (ans []int) {
128128
}
129129
```
130130

131+
### **TypeScript**
132+
133+
```ts
134+
function divisibilityArray(word: string, m: number): number[] {
135+
const ans: number[] = [];
136+
let x = 0;
137+
for (const c of word) {
138+
x = (x * 10 + Number(c)) % m;
139+
ans.push(x === 0 ? 1 : 0);
140+
}
141+
return ans;
142+
}
143+
```
144+
131145
### **...**
132146

133147
```

solution/2500-2599/2575.Find the Divisibility Array of a String/README_EN.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,20 @@ func divisibilityArray(word string, m int) (ans []int) {
112112
}
113113
```
114114

115+
### **TypeScript**
116+
117+
```ts
118+
function divisibilityArray(word: string, m: number): number[] {
119+
const ans: number[] = [];
120+
let x = 0;
121+
for (const c of word) {
122+
x = (x * 10 + Number(c)) % m;
123+
ans.push(x === 0 ? 1 : 0);
124+
}
125+
return ans;
126+
}
127+
```
128+
115129
### **...**
116130

117131
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function divisibilityArray(word: string, m: number): number[] {
2+
const ans: number[] = [];
3+
let x = 0;
4+
for (const c of word) {
5+
x = (x * 10 + Number(c)) % m;
6+
ans.push(x === 0 ? 1 : 0);
7+
}
8+
return ans;
9+
}

solution/2500-2599/2576.Find the Maximum Number of Marked Indices/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,25 @@ func maxNumOfMarkedIndices(nums []int) (ans int) {
149149
}
150150
```
151151

152+
### **TypeScript**
153+
154+
```ts
155+
function maxNumOfMarkedIndices(nums: number[]): number {
156+
nums.sort((a, b) => a - b);
157+
const n = nums.length;
158+
let ans = 0;
159+
for (let i = 0, j = Math.floor((n + 1) / 2); j < n; ++i, ++j) {
160+
while (j < n && nums[i] * 2 > nums[j]) {
161+
++j;
162+
}
163+
if (j < n) {
164+
ans += 2;
165+
}
166+
}
167+
return ans;
168+
}
169+
```
170+
152171
### **...**
153172

154173
```

solution/2500-2599/2576.Find the Maximum Number of Marked Indices/README_EN.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,25 @@ func maxNumOfMarkedIndices(nums []int) (ans int) {
143143
}
144144
```
145145

146+
### **TypeScript**
147+
148+
```ts
149+
function maxNumOfMarkedIndices(nums: number[]): number {
150+
nums.sort((a, b) => a - b);
151+
const n = nums.length;
152+
let ans = 0;
153+
for (let i = 0, j = Math.floor((n + 1) / 2); j < n; ++i, ++j) {
154+
while (j < n && nums[i] * 2 > nums[j]) {
155+
++j;
156+
}
157+
if (j < n) {
158+
ans += 2;
159+
}
160+
}
161+
return ans;
162+
}
163+
```
164+
146165
### **...**
147166

148167
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function maxNumOfMarkedIndices(nums: number[]): number {
2+
nums.sort((a, b) => a - b);
3+
const n = nums.length;
4+
let ans = 0;
5+
for (let i = 0, j = Math.floor((n + 1) / 2); j < n; ++i, ++j) {
6+
while (j < n && nums[i] * 2 > nums[j]) {
7+
++j;
8+
}
9+
if (j < n) {
10+
ans += 2;
11+
}
12+
}
13+
return ans;
14+
}

0 commit comments

Comments
 (0)