From d6a0fe2d6de90804a75cb694e4a5de246ec407f7 Mon Sep 17 00:00:00 2001 From: thinkasany <480968828@qq.com> Date: Sun, 5 Mar 2023 21:14:28 +0800 Subject: [PATCH] feat: add solutions to lc problems: No.2578,2579,2582,2583 --- .../2578.Split With Minimum Sum/README.md | 34 ++++++++ .../2578.Split With Minimum Sum/README_EN.md | 34 ++++++++ .../2578.Split With Minimum Sum/Solution.ts | 17 ++++ .../README.md | 8 ++ .../README_EN.md | 8 ++ .../Solution.ts | 3 + .../2500-2599/2582.Pass the Pillow/README.md | 24 ++++++ .../2582.Pass the Pillow/README_EN.md | 24 ++++++ .../2582.Pass the Pillow/Solution.ts | 5 ++ .../README.md | 79 +++++++++++++++++++ .../README_EN.md | 79 +++++++++++++++++++ .../Solution.ts | 37 +++++++++ 12 files changed, 352 insertions(+) create mode 100644 solution/2500-2599/2578.Split With Minimum Sum/Solution.ts create mode 100644 solution/2500-2599/2579.Count Total Number of Colored Cells/Solution.ts create mode 100644 solution/2500-2599/2582.Pass the Pillow/Solution.ts create mode 100644 solution/2500-2599/2583.Kth Largest Sum in a Binary Tree/Solution.ts diff --git a/solution/2500-2599/2578.Split With Minimum Sum/README.md b/solution/2500-2599/2578.Split With Minimum Sum/README.md index 5911a025d9fc5..f4eb25bebc7dd 100644 --- a/solution/2500-2599/2578.Split With Minimum Sum/README.md +++ b/solution/2500-2599/2578.Split With Minimum Sum/README.md @@ -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]; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2578.Split With Minimum Sum/README_EN.md b/solution/2500-2599/2578.Split With Minimum Sum/README_EN.md index ad861fde5a8cf..ebab205062ca5 100644 --- a/solution/2500-2599/2578.Split With Minimum Sum/README_EN.md +++ b/solution/2500-2599/2578.Split With Minimum Sum/README_EN.md @@ -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]; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2578.Split With Minimum Sum/Solution.ts b/solution/2500-2599/2578.Split With Minimum Sum/Solution.ts new file mode 100644 index 0000000000000..fc711d2325718 --- /dev/null +++ b/solution/2500-2599/2578.Split With Minimum Sum/Solution.ts @@ -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]; +} diff --git a/solution/2500-2599/2579.Count Total Number of Colored Cells/README.md b/solution/2500-2599/2579.Count Total Number of Colored Cells/README.md index f615362ab9f20..98cc310cb4e76 100644 --- a/solution/2500-2599/2579.Count Total Number of Colored Cells/README.md +++ b/solution/2500-2599/2579.Count Total Number of Colored Cells/README.md @@ -94,6 +94,14 @@ func coloredCells(n int) int64 { } ``` +### **TypeScript** + +```ts +function coloredCells(n: number): number { + return 2 * n * (n - 1) + 1; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2579.Count Total Number of Colored Cells/README_EN.md b/solution/2500-2599/2579.Count Total Number of Colored Cells/README_EN.md index 03a3a979d5132..0eed84b82b931 100644 --- a/solution/2500-2599/2579.Count Total Number of Colored Cells/README_EN.md +++ b/solution/2500-2599/2579.Count Total Number of Colored Cells/README_EN.md @@ -80,6 +80,14 @@ func coloredCells(n int) int64 { } ``` +### **TypeScript** + +```ts +function coloredCells(n: number): number { + return 2 * n * (n - 1) + 1; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2579.Count Total Number of Colored Cells/Solution.ts b/solution/2500-2599/2579.Count Total Number of Colored Cells/Solution.ts new file mode 100644 index 0000000000000..2a143925d8fa3 --- /dev/null +++ b/solution/2500-2599/2579.Count Total Number of Colored Cells/Solution.ts @@ -0,0 +1,3 @@ +function coloredCells(n: number): number { + return 2 * n * (n - 1) + 1; +} diff --git a/solution/2500-2599/2582.Pass the Pillow/README.md b/solution/2500-2599/2582.Pass the Pillow/README.md index 0413710e3d95b..ae21c45520c50 100644 --- a/solution/2500-2599/2582.Pass the Pillow/README.md +++ b/solution/2500-2599/2582.Pass the Pillow/README.md @@ -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; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2582.Pass the Pillow/README_EN.md b/solution/2500-2599/2582.Pass the Pillow/README_EN.md index f84fb5b5cfd9e..4c227dcab509b 100644 --- a/solution/2500-2599/2582.Pass the Pillow/README_EN.md +++ b/solution/2500-2599/2582.Pass the Pillow/README_EN.md @@ -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; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2582.Pass the Pillow/Solution.ts b/solution/2500-2599/2582.Pass the Pillow/Solution.ts new file mode 100644 index 0000000000000..4af6cb1ba16ff --- /dev/null +++ b/solution/2500-2599/2582.Pass the Pillow/Solution.ts @@ -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; +} diff --git a/solution/2500-2599/2583.Kth Largest Sum in a Binary Tree/README.md b/solution/2500-2599/2583.Kth Largest Sum in a Binary Tree/README.md index 3a2681f720424..e349ec1bc2e7f 100644 --- a/solution/2500-2599/2583.Kth Largest Sum in a Binary Tree/README.md +++ b/solution/2500-2599/2583.Kth Largest Sum in a Binary Tree/README.md @@ -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]; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2583.Kth Largest Sum in a Binary Tree/README_EN.md b/solution/2500-2599/2583.Kth Largest Sum in a Binary Tree/README_EN.md index fa1c33f58b644..ce0dfcdfc82f3 100644 --- a/solution/2500-2599/2583.Kth Largest Sum in a Binary Tree/README_EN.md +++ b/solution/2500-2599/2583.Kth Largest Sum in a Binary Tree/README_EN.md @@ -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]; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2583.Kth Largest Sum in a Binary Tree/Solution.ts b/solution/2500-2599/2583.Kth Largest Sum in a Binary Tree/Solution.ts new file mode 100644 index 0000000000000..e7fa62f82fd91 --- /dev/null +++ b/solution/2500-2599/2583.Kth Largest Sum in a Binary Tree/Solution.ts @@ -0,0 +1,37 @@ +/** + * 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]; +}