Skip to content

Commit fda2d09

Browse files
authored
feat: add solutions to lc problems: No.2578,2579,2582,2583 (doocs#914)
* No.2578.Split With Minimum Sum * No.2579.Count Total Number of Colored Cells * No.2582.Pass the Pillow * No.2583.Kth Largest Sum in a Binary Tree
1 parent e84ac4d commit fda2d09

File tree

12 files changed

+352
-0
lines changed

12 files changed

+352
-0
lines changed

solution/2500-2599/2578.Split With Minimum Sum/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,40 @@ func splitNum(num int) int {
216216
}
217217
```
218218

219+
### **TypeScript**
220+
221+
```ts
222+
function splitNum(num: number): number {
223+
const cnt = new Array(10).fill(0);
224+
let n = 0;
225+
for (; num > 0; num = Math.floor(num / 10)) {
226+
++cnt[num % 10];
227+
++n;
228+
}
229+
const ans = new Array(2).fill(0);
230+
for (let i = 0, j = 0; i < n; ++i) {
231+
while (cnt[j] === 0) {
232+
++j;
233+
}
234+
--cnt[j];
235+
ans[i & 1] = ans[i & 1] * 10 + j;
236+
}
237+
return ans[0] + ans[1];
238+
}
239+
```
240+
241+
```ts
242+
function splitNum(num: number): number {
243+
const s: string[] = String(num).split('');
244+
s.sort();
245+
const ans: number[] = new Array(2).fill(0);
246+
for (let i = 0; i < s.length; ++i) {
247+
ans[i & 1] = ans[i & 1] * 10 + Number(s[i]);
248+
}
249+
return ans[0] + ans[1];
250+
}
251+
```
252+
219253
### **...**
220254

221255
```

solution/2500-2599/2578.Split With Minimum Sum/README_EN.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,40 @@ func splitNum(num int) int {
192192
}
193193
```
194194

195+
### **TypeScript**
196+
197+
```ts
198+
function splitNum(num: number): number {
199+
const cnt = new Array(10).fill(0);
200+
let n = 0;
201+
for (; num > 0; num = Math.floor(num / 10)) {
202+
++cnt[num % 10];
203+
++n;
204+
}
205+
const ans = new Array(2).fill(0);
206+
for (let i = 0, j = 0; i < n; ++i) {
207+
while (cnt[j] === 0) {
208+
++j;
209+
}
210+
--cnt[j];
211+
ans[i & 1] = ans[i & 1] * 10 + j;
212+
}
213+
return ans[0] + ans[1];
214+
}
215+
```
216+
217+
```ts
218+
function splitNum(num: number): number {
219+
const s: string[] = String(num).split('');
220+
s.sort();
221+
const ans: number[] = new Array(2).fill(0);
222+
for (let i = 0; i < s.length; ++i) {
223+
ans[i & 1] = ans[i & 1] * 10 + Number(s[i]);
224+
}
225+
return ans[0] + ans[1];
226+
}
227+
```
228+
195229
### **...**
196230

197231
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function splitNum(num: number): number {
2+
const cnt = new Array(10).fill(0);
3+
let n = 0;
4+
for (; num > 0; num = Math.floor(num / 10)) {
5+
++cnt[num % 10];
6+
++n;
7+
}
8+
const ans = new Array(2).fill(0);
9+
for (let i = 0, j = 0; i < n; ++i) {
10+
while (cnt[j] === 0) {
11+
++j;
12+
}
13+
--cnt[j];
14+
ans[i & 1] = ans[i & 1] * 10 + j;
15+
}
16+
return ans[0] + ans[1];
17+
}

solution/2500-2599/2579.Count Total Number of Colored Cells/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ func coloredCells(n int) int64 {
9494
}
9595
```
9696

97+
### **TypeScript**
98+
99+
```ts
100+
function coloredCells(n: number): number {
101+
return 2 * n * (n - 1) + 1;
102+
}
103+
```
104+
97105
### **...**
98106

99107
```

solution/2500-2599/2579.Count Total Number of Colored Cells/README_EN.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ func coloredCells(n int) int64 {
8080
}
8181
```
8282

83+
### **TypeScript**
84+
85+
```ts
86+
function coloredCells(n: number): number {
87+
return 2 * n * (n - 1) + 1;
88+
}
89+
```
90+
8391
### **...**
8492

8593
```
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function coloredCells(n: number): number {
2+
return 2 * n * (n - 1) + 1;
3+
}

solution/2500-2599/2582.Pass the Pillow/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,30 @@ func passThePillow(n int, time int) int {
173173
}
174174
```
175175

176+
### **TypeScript**
177+
178+
```ts
179+
function passThePillow(n: number, time: number): number {
180+
let ans = 1,
181+
k = 1;
182+
while (time-- > 0) {
183+
ans += k;
184+
if (ans === 1 || ans === n) {
185+
k *= -1;
186+
}
187+
}
188+
return ans;
189+
}
190+
```
191+
192+
```ts
193+
function passThePillow(n: number, time: number): number {
194+
const k = time / (n - 1);
195+
const mod = time % (n - 1);
196+
return (k & 1) == 1 ? n - mod : mod + 1;
197+
}
198+
```
199+
176200
### **...**
177201

178202
```

solution/2500-2599/2582.Pass the Pillow/README_EN.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,30 @@ func passThePillow(n int, time int) int {
143143
}
144144
```
145145

146+
### **TypeScript**
147+
148+
```ts
149+
function passThePillow(n: number, time: number): number {
150+
let ans = 1,
151+
k = 1;
152+
while (time-- > 0) {
153+
ans += k;
154+
if (ans === 1 || ans === n) {
155+
k *= -1;
156+
}
157+
}
158+
return ans;
159+
}
160+
```
161+
162+
```ts
163+
function passThePillow(n: number, time: number): number {
164+
const k = time / (n - 1);
165+
const mod = time % (n - 1);
166+
return (k & 1) == 1 ? n - mod : mod + 1;
167+
}
168+
```
169+
146170
### **...**
147171

148172
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function passThePillow(n: number, time: number): number {
2+
const k = time / (n - 1);
3+
const mod = time % (n - 1);
4+
return (k & 1) == 1 ? n - mod : mod + 1;
5+
}

solution/2500-2599/2583.Kth Largest Sum in a Binary Tree/README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,85 @@ func kthLargestLevelSum(root *TreeNode, k int) int64 {
361361
}
362362
```
363363

364+
### **TypeScript**
365+
366+
```ts
367+
/**
368+
* Definition for a binary tree node.
369+
* class TreeNode {
370+
* val: number
371+
* left: TreeNode | null
372+
* right: TreeNode | null
373+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
374+
* this.val = (val===undefined ? 0 : val)
375+
* this.left = (left===undefined ? null : left)
376+
* this.right = (right===undefined ? null : right)
377+
* }
378+
* }
379+
*/
380+
381+
function kthLargestLevelSum(root: TreeNode | null, k: number): number {
382+
const arr: number[] = [];
383+
const q = [root];
384+
while (q.length) {
385+
let t = 0;
386+
for (let n = q.length; n > 0; --n) {
387+
root = q.shift();
388+
t += root.val;
389+
if (root.left) {
390+
q.push(root.left);
391+
}
392+
if (root.right) {
393+
q.push(root.right);
394+
}
395+
}
396+
arr.push(t);
397+
}
398+
if (arr.length < k) {
399+
return -1;
400+
}
401+
arr.sort((a, b) => b - a);
402+
return arr[k - 1];
403+
}
404+
```
405+
406+
```ts
407+
/**
408+
* Definition for a binary tree node.
409+
* class TreeNode {
410+
* val: number
411+
* left: TreeNode | null
412+
* right: TreeNode | null
413+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
414+
* this.val = (val===undefined ? 0 : val)
415+
* this.left = (left===undefined ? null : left)
416+
* this.right = (right===undefined ? null : right)
417+
* }
418+
* }
419+
*/
420+
421+
function kthLargestLevelSum(root: TreeNode | null, k: number): number {
422+
const dfs = (root: TreeNode, d: number) => {
423+
if (!root) {
424+
return;
425+
}
426+
if (arr.length <= d) {
427+
arr.push(0);
428+
}
429+
arr[d] += root.val;
430+
dfs(root.left, d + 1);
431+
dfs(root.right, d + 1);
432+
};
433+
const arr: number[] = [];
434+
dfs(root, 0);
435+
if (arr.length < k) {
436+
return -1;
437+
}
438+
arr.sort((a, b) => b - a);
439+
return arr[k - 1];
440+
}
441+
```
442+
364443
### **...**
365444

366445
```

0 commit comments

Comments
 (0)