From 31091f58a20485f7fc4d2778b71f852c7578cc48 Mon Sep 17 00:00:00 2001 From: thinkasany <480968828@qq.com> Date: Sun, 19 Mar 2023 14:18:05 +0800 Subject: [PATCH] feat: add solutions to lc problems: No.2591~2592, No.2595~2598 --- .../README.md | 17 +++++++++++ .../README_EN.md | 18 ++++++++++++ .../Solution.ts | 12 ++++++++ .../README.md | 15 ++++++++++ .../README_EN.md | 15 ++++++++++ .../Solution.ts | 10 +++++++ .../README.md | 12 ++++++++ .../README_EN.md | 12 ++++++++ .../Solution.ts | 7 +++++ .../README.md | 28 +++++++++++++++++++ .../README_EN.md | 28 +++++++++++++++++++ .../Solution.ts | 23 +++++++++++++++ .../README.md | 26 +++++++++++++++++ .../README_EN.md | 26 +++++++++++++++++ .../Solution.ts | 21 ++++++++++++++ .../README.md | 16 +++++++++++ .../README_EN.md | 16 +++++++++++ .../Solution.ts | 11 ++++++++ 18 files changed, 313 insertions(+) create mode 100644 solution/2500-2599/2591.Distribute Money to Maximum Children/Solution.ts create mode 100644 solution/2500-2599/2592.Maximize Greatness of an Array/Solution.ts create mode 100644 solution/2500-2599/2595.Number of Even and Odd Bits/Solution.ts create mode 100644 solution/2500-2599/2596.Check Knight Tour Configuration/Solution.ts create mode 100644 solution/2500-2599/2597.The Number of Beautiful Subsets/Solution.ts create mode 100644 solution/2500-2599/2598.Smallest Missing Non-negative Integer After Operations/Solution.ts diff --git a/solution/2500-2599/2591.Distribute Money to Maximum Children/README.md b/solution/2500-2599/2591.Distribute Money to Maximum Children/README.md index 4bfec3dcb0ba4..40f12086d5625 100644 --- a/solution/2500-2599/2591.Distribute Money to Maximum Children/README.md +++ b/solution/2500-2599/2591.Distribute Money to Maximum Children/README.md @@ -144,6 +144,23 @@ func distMoney(money int, children int) int { } ``` +### **TypeScript** + +```ts +function distMoney(money: number, children: number): number { + if (money < children) { + return -1; + } + if (money > 8 * children) { + return children - 1; + } + if (money === 8 * children - 4) { + return children - 2; + } + return Math.floor((money - children) / 7); +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2591.Distribute Money to Maximum Children/README_EN.md b/solution/2500-2599/2591.Distribute Money to Maximum Children/README_EN.md index ce54c20dfdff7..6bf65e668d8ad 100644 --- a/solution/2500-2599/2591.Distribute Money to Maximum Children/README_EN.md +++ b/solution/2500-2599/2591.Distribute Money to Maximum Children/README_EN.md @@ -124,6 +124,24 @@ func distMoney(money int, children int) int { } ``` + +### **TypeScript** + +```ts +function distMoney(money: number, children: number): number { + if (money < children) { + return -1; + } + if (money > 8 * children) { + return children - 1; + } + if (money === 8 * children - 4) { + return children - 2; + } + return Math.floor((money - children) / 7); +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2591.Distribute Money to Maximum Children/Solution.ts b/solution/2500-2599/2591.Distribute Money to Maximum Children/Solution.ts new file mode 100644 index 0000000000000..2473941c6ce5b --- /dev/null +++ b/solution/2500-2599/2591.Distribute Money to Maximum Children/Solution.ts @@ -0,0 +1,12 @@ +function distMoney(money: number, children: number): number { + if (money < children) { + return -1; + } + if (money > 8 * children) { + return children - 1; + } + if (money === 8 * children - 4) { + return children - 2; + } + return Math.floor((money - children) / 7); +} diff --git a/solution/2500-2599/2592.Maximize Greatness of an Array/README.md b/solution/2500-2599/2592.Maximize Greatness of an Array/README.md index dabbd99ac271f..a74ba9376e593 100644 --- a/solution/2500-2599/2592.Maximize Greatness of an Array/README.md +++ b/solution/2500-2599/2592.Maximize Greatness of an Array/README.md @@ -118,6 +118,21 @@ func maximizeGreatness(nums []int) int { } ``` +### **TypeScript** + +```ts +function maximizeGreatness(nums: number[]): number { + nums.sort((a, b) => a - b); + let i = 0; + for (const x of nums) { + if (x > nums[i]) { + i += 1; + } + } + return i; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2592.Maximize Greatness of an Array/README_EN.md b/solution/2500-2599/2592.Maximize Greatness of an Array/README_EN.md index 937a09a3e4297..0bc96dc906f5e 100644 --- a/solution/2500-2599/2592.Maximize Greatness of an Array/README_EN.md +++ b/solution/2500-2599/2592.Maximize Greatness of an Array/README_EN.md @@ -100,6 +100,21 @@ func maximizeGreatness(nums []int) int { } ``` +### **TypeScript** + +```ts +function maximizeGreatness(nums: number[]): number { + nums.sort((a, b) => a - b); + let i = 0; + for (const x of nums) { + if (x > nums[i]) { + i += 1; + } + } + return i; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2592.Maximize Greatness of an Array/Solution.ts b/solution/2500-2599/2592.Maximize Greatness of an Array/Solution.ts new file mode 100644 index 0000000000000..e0785eca4aed9 --- /dev/null +++ b/solution/2500-2599/2592.Maximize Greatness of an Array/Solution.ts @@ -0,0 +1,10 @@ +function maximizeGreatness(nums: number[]): number { + nums.sort((a, b) => a - b); + let i = 0; + for (const x of nums) { + if (x > nums[i]) { + i += 1; + } + } + return i; +} diff --git a/solution/2500-2599/2595.Number of Even and Odd Bits/README.md b/solution/2500-2599/2595.Number of Even and Odd Bits/README.md index ca70e62c5b362..cbfc900775479 100644 --- a/solution/2500-2599/2595.Number of Even and Odd Bits/README.md +++ b/solution/2500-2599/2595.Number of Even and Odd Bits/README.md @@ -113,6 +113,18 @@ func evenOddBit(n int) []int { } ``` +### **TypeScript** + +```ts +function evenOddBit(n: number): number[] { + const ans = new Array(2).fill(0); + for (let i = 0; n > 0; n >>= 1, i ^= 1) { + ans[i] += n & 1; + } + return ans; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2595.Number of Even and Odd Bits/README_EN.md b/solution/2500-2599/2595.Number of Even and Odd Bits/README_EN.md index 2eeb167c136d4..fc9cd5289254f 100644 --- a/solution/2500-2599/2595.Number of Even and Odd Bits/README_EN.md +++ b/solution/2500-2599/2595.Number of Even and Odd Bits/README_EN.md @@ -99,6 +99,18 @@ func evenOddBit(n int) []int { } ``` +### **TypeScript** + +```ts +function evenOddBit(n: number): number[] { + const ans = new Array(2).fill(0); + for (let i = 0; n > 0; n >>= 1, i ^= 1) { + ans[i] += n & 1; + } + return ans; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2595.Number of Even and Odd Bits/Solution.ts b/solution/2500-2599/2595.Number of Even and Odd Bits/Solution.ts new file mode 100644 index 0000000000000..dd201acad879e --- /dev/null +++ b/solution/2500-2599/2595.Number of Even and Odd Bits/Solution.ts @@ -0,0 +1,7 @@ +function evenOddBit(n: number): number[] { + const ans = new Array(2).fill(0); + for (let i = 0; n > 0; n >>= 1, i ^= 1) { + ans[i] += n & 1; + } + return ans; +} diff --git a/solution/2500-2599/2596.Check Knight Tour Configuration/README.md b/solution/2500-2599/2596.Check Knight Tour Configuration/README.md index 094d11029d74a..d9152970743cd 100644 --- a/solution/2500-2599/2596.Check Knight Tour Configuration/README.md +++ b/solution/2500-2599/2596.Check Knight Tour Configuration/README.md @@ -176,6 +176,34 @@ func abs(x int) int { } ``` +### **TypeScript** + +```ts +function checkValidGrid(grid: number[][]): boolean { + if (grid[0][0] !== 0) { + return false; + } + const n = grid.length; + const pos = Array.from(new Array(n * n), () => new Array(2).fill(0)); + for (let i = 0; i < n; ++i) { + for (let j = 0; j < n; ++j) { + pos[grid[i][j]] = [i, j]; + } + } + for (let i = 1; i < n * n; ++i) { + const p1 = pos[i - 1]; + const p2 = pos[i]; + const dx = Math.abs(p1[0] - p2[0]); + const dy = Math.abs(p1[1] - p2[1]); + const ok = (dx === 1 && dy === 2) || (dx === 2 && dy === 1); + if (!ok) { + return false; + } + } + return true; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2596.Check Knight Tour Configuration/README_EN.md b/solution/2500-2599/2596.Check Knight Tour Configuration/README_EN.md index d82c5ad3a9cfb..e852ed2940333 100644 --- a/solution/2500-2599/2596.Check Knight Tour Configuration/README_EN.md +++ b/solution/2500-2599/2596.Check Knight Tour Configuration/README_EN.md @@ -159,6 +159,34 @@ func abs(x int) int { } ``` +### **TypeScript** + +```ts +function checkValidGrid(grid: number[][]): boolean { + if (grid[0][0] !== 0) { + return false; + } + const n = grid.length; + const pos = Array.from(new Array(n * n), () => new Array(2).fill(0)); + for (let i = 0; i < n; ++i) { + for (let j = 0; j < n; ++j) { + pos[grid[i][j]] = [i, j]; + } + } + for (let i = 1; i < n * n; ++i) { + const p1 = pos[i - 1]; + const p2 = pos[i]; + const dx = Math.abs(p1[0] - p2[0]); + const dy = Math.abs(p1[1] - p2[1]); + const ok = (dx === 1 && dy === 2) || (dx === 2 && dy === 1); + if (!ok) { + return false; + } + } + return true; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2596.Check Knight Tour Configuration/Solution.ts b/solution/2500-2599/2596.Check Knight Tour Configuration/Solution.ts new file mode 100644 index 0000000000000..137d744339a98 --- /dev/null +++ b/solution/2500-2599/2596.Check Knight Tour Configuration/Solution.ts @@ -0,0 +1,23 @@ +function checkValidGrid(grid: number[][]): boolean { + if (grid[0][0] !== 0) { + return false; + } + const n = grid.length; + const pos = Array.from(new Array(n * n), () => new Array(2).fill(0)); + for (let i = 0; i < n; ++i) { + for (let j = 0; j < n; ++j) { + pos[grid[i][j]] = [i, j]; + } + } + for (let i = 1; i < n * n; ++i) { + const p1 = pos[i - 1]; + const p2 = pos[i]; + const dx = Math.abs(p1[0] - p2[0]); + const dy = Math.abs(p1[1] - p2[1]); + const ok = (dx === 1 && dy === 2) || (dx === 2 && dy === 1); + if (!ok) { + return false; + } + } + return true; +} diff --git a/solution/2500-2599/2597.The Number of Beautiful Subsets/README.md b/solution/2500-2599/2597.The Number of Beautiful Subsets/README.md index f6e1ddb1c3752..f4651d3084f7c 100644 --- a/solution/2500-2599/2597.The Number of Beautiful Subsets/README.md +++ b/solution/2500-2599/2597.The Number of Beautiful Subsets/README.md @@ -176,6 +176,32 @@ func beautifulSubsets(nums []int, k int) int { } ``` +### **TypeScript** + +```ts +function beautifulSubsets(nums: number[], k: number): number { + let ans: number = -1; + const cnt: number[] = new Array(1010).fill(0); + const n: number = nums.length; + const dfs = (i: number) => { + if (i >= n) { + ++ans; + return; + } + dfs(i + 1); + const ok1: boolean = nums[i] + k >= 1010 || cnt[nums[i] + k] === 0; + const ok2: boolean = nums[i] - k < 0 || cnt[nums[i] - k] === 0; + if (ok1 && ok2) { + ++cnt[nums[i]]; + dfs(i + 1); + --cnt[nums[i]]; + } + }; + dfs(0); + return ans; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2597.The Number of Beautiful Subsets/README_EN.md b/solution/2500-2599/2597.The Number of Beautiful Subsets/README_EN.md index 38851d981a7e6..7e3d83c98e526 100644 --- a/solution/2500-2599/2597.The Number of Beautiful Subsets/README_EN.md +++ b/solution/2500-2599/2597.The Number of Beautiful Subsets/README_EN.md @@ -155,6 +155,32 @@ func beautifulSubsets(nums []int, k int) int { } ``` +### **TypeScript** + +```ts +function beautifulSubsets(nums: number[], k: number): number { + let ans: number = -1; + const cnt: number[] = new Array(1010).fill(0); + const n: number = nums.length; + const dfs = (i: number) => { + if (i >= n) { + ++ans; + return; + } + dfs(i + 1); + const ok1: boolean = nums[i] + k >= 1010 || cnt[nums[i] + k] === 0; + const ok2: boolean = nums[i] - k < 0 || cnt[nums[i] - k] === 0; + if (ok1 && ok2) { + ++cnt[nums[i]]; + dfs(i + 1); + --cnt[nums[i]]; + } + }; + dfs(0); + return ans; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2597.The Number of Beautiful Subsets/Solution.ts b/solution/2500-2599/2597.The Number of Beautiful Subsets/Solution.ts new file mode 100644 index 0000000000000..00b242b2c709f --- /dev/null +++ b/solution/2500-2599/2597.The Number of Beautiful Subsets/Solution.ts @@ -0,0 +1,21 @@ +function beautifulSubsets(nums: number[], k: number): number { + let ans: number = -1; + const cnt: number[] = new Array(1010).fill(0); + const n: number = nums.length; + const dfs = (i: number) => { + if (i >= n) { + ++ans; + return; + } + dfs(i + 1); + const ok1: boolean = nums[i] + k >= 1010 || cnt[nums[i] + k] === 0; + const ok2: boolean = nums[i] - k < 0 || cnt[nums[i] - k] === 0; + if (ok1 && ok2) { + ++cnt[nums[i]]; + dfs(i + 1); + --cnt[nums[i]]; + } + }; + dfs(0); + return ans; +} diff --git a/solution/2500-2599/2598.Smallest Missing Non-negative Integer After Operations/README.md b/solution/2500-2599/2598.Smallest Missing Non-negative Integer After Operations/README.md index a4c88f2b91d22..31ace9297d626 100644 --- a/solution/2500-2599/2598.Smallest Missing Non-negative Integer After Operations/README.md +++ b/solution/2500-2599/2598.Smallest Missing Non-negative Integer After Operations/README.md @@ -138,6 +138,22 @@ func findSmallestInteger(nums []int, value int) int { } ``` +### **TypeScript** + +```ts +function findSmallestInteger(nums: number[], value: number): number { + const cnt: number[] = new Array(value).fill(0); + for (const x of nums) { + ++cnt[((x % value) + value) % value]; + } + for (let i = 0; ; ++i) { + if (cnt[i % value]-- === 0) { + return i; + } + } +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2598.Smallest Missing Non-negative Integer After Operations/README_EN.md b/solution/2500-2599/2598.Smallest Missing Non-negative Integer After Operations/README_EN.md index 83ba49e729cfc..718863b65dd83 100644 --- a/solution/2500-2599/2598.Smallest Missing Non-negative Integer After Operations/README_EN.md +++ b/solution/2500-2599/2598.Smallest Missing Non-negative Integer After Operations/README_EN.md @@ -122,6 +122,22 @@ func findSmallestInteger(nums []int, value int) int { } ``` +### **TypeScript** + +```ts +function findSmallestInteger(nums: number[], value: number): number { + const cnt: number[] = new Array(value).fill(0); + for (const x of nums) { + ++cnt[((x % value) + value) % value]; + } + for (let i = 0; ; ++i) { + if (cnt[i % value]-- === 0) { + return i; + } + } +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2598.Smallest Missing Non-negative Integer After Operations/Solution.ts b/solution/2500-2599/2598.Smallest Missing Non-negative Integer After Operations/Solution.ts new file mode 100644 index 0000000000000..40b0ce6df3a61 --- /dev/null +++ b/solution/2500-2599/2598.Smallest Missing Non-negative Integer After Operations/Solution.ts @@ -0,0 +1,11 @@ +function findSmallestInteger(nums: number[], value: number): number { + const cnt: number[] = new Array(value).fill(0); + for (const x of nums) { + ++cnt[((x % value) + value) % value]; + } + for (let i = 0; ; ++i) { + if (cnt[i % value]-- === 0) { + return i; + } + } +}