Skip to content

feat: add solutions to lc problems: No.2578,2579,2582,2583 #914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions solution/2500-2599/2578.Split With Minimum Sum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,40 @@ func splitNum(num int) int {
}
```

### **TypeScript**

```ts
function splitNum(num: number): number {
const cnt = new Array(10).fill(0);
let n = 0;
for (; num > 0; num = Math.floor(num / 10)) {
++cnt[num % 10];
++n;
}
const ans = new Array(2).fill(0);
for (let i = 0, j = 0; i < n; ++i) {
while (cnt[j] === 0) {
++j;
}
--cnt[j];
ans[i & 1] = ans[i & 1] * 10 + j;
}
return ans[0] + ans[1];
}
```

```ts
function splitNum(num: number): number {
const s: string[] = String(num).split('');
s.sort();
const ans: number[] = new Array(2).fill(0);
for (let i = 0; i < s.length; ++i) {
ans[i & 1] = ans[i & 1] * 10 + Number(s[i]);
}
return ans[0] + ans[1];
}
```

### **...**

```
Expand Down
34 changes: 34 additions & 0 deletions solution/2500-2599/2578.Split With Minimum Sum/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,40 @@ func splitNum(num int) int {
}
```

### **TypeScript**

```ts
function splitNum(num: number): number {
const cnt = new Array(10).fill(0);
let n = 0;
for (; num > 0; num = Math.floor(num / 10)) {
++cnt[num % 10];
++n;
}
const ans = new Array(2).fill(0);
for (let i = 0, j = 0; i < n; ++i) {
while (cnt[j] === 0) {
++j;
}
--cnt[j];
ans[i & 1] = ans[i & 1] * 10 + j;
}
return ans[0] + ans[1];
}
```

```ts
function splitNum(num: number): number {
const s: string[] = String(num).split('');
s.sort();
const ans: number[] = new Array(2).fill(0);
for (let i = 0; i < s.length; ++i) {
ans[i & 1] = ans[i & 1] * 10 + Number(s[i]);
}
return ans[0] + ans[1];
}
```

### **...**

```
Expand Down
17 changes: 17 additions & 0 deletions solution/2500-2599/2578.Split With Minimum Sum/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function splitNum(num: number): number {
const cnt = new Array(10).fill(0);
let n = 0;
for (; num > 0; num = Math.floor(num / 10)) {
++cnt[num % 10];
++n;
}
const ans = new Array(2).fill(0);
for (let i = 0, j = 0; i < n; ++i) {
while (cnt[j] === 0) {
++j;
}
--cnt[j];
ans[i & 1] = ans[i & 1] * 10 + j;
}
return ans[0] + ans[1];
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ func coloredCells(n int) int64 {
}
```

### **TypeScript**

```ts
function coloredCells(n: number): number {
return 2 * n * (n - 1) + 1;
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ func coloredCells(n int) int64 {
}
```

### **TypeScript**

```ts
function coloredCells(n: number): number {
return 2 * n * (n - 1) + 1;
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function coloredCells(n: number): number {
return 2 * n * (n - 1) + 1;
}
24 changes: 24 additions & 0 deletions solution/2500-2599/2582.Pass the Pillow/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,30 @@ func passThePillow(n int, time int) int {
}
```

### **TypeScript**

```ts
function passThePillow(n: number, time: number): number {
let ans = 1,
k = 1;
while (time-- > 0) {
ans += k;
if (ans === 1 || ans === n) {
k *= -1;
}
}
return ans;
}
```

```ts
function passThePillow(n: number, time: number): number {
const k = time / (n - 1);
const mod = time % (n - 1);
return (k & 1) == 1 ? n - mod : mod + 1;
}
```

### **...**

```
Expand Down
24 changes: 24 additions & 0 deletions solution/2500-2599/2582.Pass the Pillow/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,30 @@ func passThePillow(n int, time int) int {
}
```

### **TypeScript**

```ts
function passThePillow(n: number, time: number): number {
let ans = 1,
k = 1;
while (time-- > 0) {
ans += k;
if (ans === 1 || ans === n) {
k *= -1;
}
}
return ans;
}
```

```ts
function passThePillow(n: number, time: number): number {
const k = time / (n - 1);
const mod = time % (n - 1);
return (k & 1) == 1 ? n - mod : mod + 1;
}
```

### **...**

```
Expand Down
5 changes: 5 additions & 0 deletions solution/2500-2599/2582.Pass the Pillow/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function passThePillow(n: number, time: number): number {
const k = time / (n - 1);
const mod = time % (n - 1);
return (k & 1) == 1 ? n - mod : mod + 1;
}
79 changes: 79 additions & 0 deletions solution/2500-2599/2583.Kth Largest Sum in a Binary Tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,85 @@ func kthLargestLevelSum(root *TreeNode, k int) int64 {
}
```

### **TypeScript**

```ts
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

function kthLargestLevelSum(root: TreeNode | null, k: number): number {
const arr: number[] = [];
const q = [root];
while (q.length) {
let t = 0;
for (let n = q.length; n > 0; --n) {
root = q.shift();
t += root.val;
if (root.left) {
q.push(root.left);
}
if (root.right) {
q.push(root.right);
}
}
arr.push(t);
}
if (arr.length < k) {
return -1;
}
arr.sort((a, b) => b - a);
return arr[k - 1];
}
```

```ts
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

function kthLargestLevelSum(root: TreeNode | null, k: number): number {
const dfs = (root: TreeNode, d: number) => {
if (!root) {
return;
}
if (arr.length <= d) {
arr.push(0);
}
arr[d] += root.val;
dfs(root.left, d + 1);
dfs(root.right, d + 1);
};
const arr: number[] = [];
dfs(root, 0);
if (arr.length < k) {
return -1;
}
arr.sort((a, b) => b - a);
return arr[k - 1];
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,85 @@ func kthLargestLevelSum(root *TreeNode, k int) int64 {
}
```

### **TypeScript**

```ts
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

function kthLargestLevelSum(root: TreeNode | null, k: number): number {
const arr: number[] = [];
const q = [root];
while (q.length) {
let t = 0;
for (let n = q.length; n > 0; --n) {
root = q.shift();
t += root.val;
if (root.left) {
q.push(root.left);
}
if (root.right) {
q.push(root.right);
}
}
arr.push(t);
}
if (arr.length < k) {
return -1;
}
arr.sort((a, b) => b - a);
return arr[k - 1];
}
```

```ts
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

function kthLargestLevelSum(root: TreeNode | null, k: number): number {
const dfs = (root: TreeNode, d: number) => {
if (!root) {
return;
}
if (arr.length <= d) {
arr.push(0);
}
arr[d] += root.val;
dfs(root.left, d + 1);
dfs(root.right, d + 1);
};
const arr: number[] = [];
dfs(root, 0);
if (arr.length < k) {
return -1;
}
arr.sort((a, b) => b - a);
return arr[k - 1];
}
```

### **...**

```
Expand Down
Loading