Skip to content

Commit df2216e

Browse files
authored
feat: add solutions to lc problems: No.2591~2592, No.2595~2598 (doocs#940)
1 parent 216886d commit df2216e

File tree

18 files changed

+313
-0
lines changed

18 files changed

+313
-0
lines changed

solution/2500-2599/2591.Distribute Money to Maximum Children/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,23 @@ func distMoney(money int, children int) int {
144144
}
145145
```
146146

147+
### **TypeScript**
148+
149+
```ts
150+
function distMoney(money: number, children: number): number {
151+
if (money < children) {
152+
return -1;
153+
}
154+
if (money > 8 * children) {
155+
return children - 1;
156+
}
157+
if (money === 8 * children - 4) {
158+
return children - 2;
159+
}
160+
return Math.floor((money - children) / 7);
161+
}
162+
```
163+
147164
### **...**
148165

149166
```

solution/2500-2599/2591.Distribute Money to Maximum Children/README_EN.md

+18
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,24 @@ func distMoney(money int, children int) int {
124124
}
125125
```
126126

127+
128+
### **TypeScript**
129+
130+
```ts
131+
function distMoney(money: number, children: number): number {
132+
if (money < children) {
133+
return -1;
134+
}
135+
if (money > 8 * children) {
136+
return children - 1;
137+
}
138+
if (money === 8 * children - 4) {
139+
return children - 2;
140+
}
141+
return Math.floor((money - children) / 7);
142+
}
143+
```
144+
127145
### **...**
128146

129147
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function distMoney(money: number, children: number): number {
2+
if (money < children) {
3+
return -1;
4+
}
5+
if (money > 8 * children) {
6+
return children - 1;
7+
}
8+
if (money === 8 * children - 4) {
9+
return children - 2;
10+
}
11+
return Math.floor((money - children) / 7);
12+
}

solution/2500-2599/2592.Maximize Greatness of an Array/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,21 @@ func maximizeGreatness(nums []int) int {
118118
}
119119
```
120120

121+
### **TypeScript**
122+
123+
```ts
124+
function maximizeGreatness(nums: number[]): number {
125+
nums.sort((a, b) => a - b);
126+
let i = 0;
127+
for (const x of nums) {
128+
if (x > nums[i]) {
129+
i += 1;
130+
}
131+
}
132+
return i;
133+
}
134+
```
135+
121136
### **...**
122137

123138
```

solution/2500-2599/2592.Maximize Greatness of an Array/README_EN.md

+15
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,21 @@ func maximizeGreatness(nums []int) int {
100100
}
101101
```
102102

103+
### **TypeScript**
104+
105+
```ts
106+
function maximizeGreatness(nums: number[]): number {
107+
nums.sort((a, b) => a - b);
108+
let i = 0;
109+
for (const x of nums) {
110+
if (x > nums[i]) {
111+
i += 1;
112+
}
113+
}
114+
return i;
115+
}
116+
```
117+
103118
### **...**
104119

105120
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function maximizeGreatness(nums: number[]): number {
2+
nums.sort((a, b) => a - b);
3+
let i = 0;
4+
for (const x of nums) {
5+
if (x > nums[i]) {
6+
i += 1;
7+
}
8+
}
9+
return i;
10+
}

solution/2500-2599/2595.Number of Even and Odd Bits/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ func evenOddBit(n int) []int {
113113
}
114114
```
115115

116+
### **TypeScript**
117+
118+
```ts
119+
function evenOddBit(n: number): number[] {
120+
const ans = new Array(2).fill(0);
121+
for (let i = 0; n > 0; n >>= 1, i ^= 1) {
122+
ans[i] += n & 1;
123+
}
124+
return ans;
125+
}
126+
```
127+
116128
### **...**
117129

118130
```

solution/2500-2599/2595.Number of Even and Odd Bits/README_EN.md

+12
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ func evenOddBit(n int) []int {
9999
}
100100
```
101101

102+
### **TypeScript**
103+
104+
```ts
105+
function evenOddBit(n: number): number[] {
106+
const ans = new Array(2).fill(0);
107+
for (let i = 0; n > 0; n >>= 1, i ^= 1) {
108+
ans[i] += n & 1;
109+
}
110+
return ans;
111+
}
112+
```
113+
102114
### **...**
103115

104116
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function evenOddBit(n: number): number[] {
2+
const ans = new Array(2).fill(0);
3+
for (let i = 0; n > 0; n >>= 1, i ^= 1) {
4+
ans[i] += n & 1;
5+
}
6+
return ans;
7+
}

solution/2500-2599/2596.Check Knight Tour Configuration/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,34 @@ func abs(x int) int {
176176
}
177177
```
178178

179+
### **TypeScript**
180+
181+
```ts
182+
function checkValidGrid(grid: number[][]): boolean {
183+
if (grid[0][0] !== 0) {
184+
return false;
185+
}
186+
const n = grid.length;
187+
const pos = Array.from(new Array(n * n), () => new Array(2).fill(0));
188+
for (let i = 0; i < n; ++i) {
189+
for (let j = 0; j < n; ++j) {
190+
pos[grid[i][j]] = [i, j];
191+
}
192+
}
193+
for (let i = 1; i < n * n; ++i) {
194+
const p1 = pos[i - 1];
195+
const p2 = pos[i];
196+
const dx = Math.abs(p1[0] - p2[0]);
197+
const dy = Math.abs(p1[1] - p2[1]);
198+
const ok = (dx === 1 && dy === 2) || (dx === 2 && dy === 1);
199+
if (!ok) {
200+
return false;
201+
}
202+
}
203+
return true;
204+
}
205+
```
206+
179207
### **...**
180208

181209
```

solution/2500-2599/2596.Check Knight Tour Configuration/README_EN.md

+28
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,34 @@ func abs(x int) int {
159159
}
160160
```
161161

162+
### **TypeScript**
163+
164+
```ts
165+
function checkValidGrid(grid: number[][]): boolean {
166+
if (grid[0][0] !== 0) {
167+
return false;
168+
}
169+
const n = grid.length;
170+
const pos = Array.from(new Array(n * n), () => new Array(2).fill(0));
171+
for (let i = 0; i < n; ++i) {
172+
for (let j = 0; j < n; ++j) {
173+
pos[grid[i][j]] = [i, j];
174+
}
175+
}
176+
for (let i = 1; i < n * n; ++i) {
177+
const p1 = pos[i - 1];
178+
const p2 = pos[i];
179+
const dx = Math.abs(p1[0] - p2[0]);
180+
const dy = Math.abs(p1[1] - p2[1]);
181+
const ok = (dx === 1 && dy === 2) || (dx === 2 && dy === 1);
182+
if (!ok) {
183+
return false;
184+
}
185+
}
186+
return true;
187+
}
188+
```
189+
162190
### **...**
163191

164192
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function checkValidGrid(grid: number[][]): boolean {
2+
if (grid[0][0] !== 0) {
3+
return false;
4+
}
5+
const n = grid.length;
6+
const pos = Array.from(new Array(n * n), () => new Array(2).fill(0));
7+
for (let i = 0; i < n; ++i) {
8+
for (let j = 0; j < n; ++j) {
9+
pos[grid[i][j]] = [i, j];
10+
}
11+
}
12+
for (let i = 1; i < n * n; ++i) {
13+
const p1 = pos[i - 1];
14+
const p2 = pos[i];
15+
const dx = Math.abs(p1[0] - p2[0]);
16+
const dy = Math.abs(p1[1] - p2[1]);
17+
const ok = (dx === 1 && dy === 2) || (dx === 2 && dy === 1);
18+
if (!ok) {
19+
return false;
20+
}
21+
}
22+
return true;
23+
}

solution/2500-2599/2597.The Number of Beautiful Subsets/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,32 @@ func beautifulSubsets(nums []int, k int) int {
176176
}
177177
```
178178

179+
### **TypeScript**
180+
181+
```ts
182+
function beautifulSubsets(nums: number[], k: number): number {
183+
let ans: number = -1;
184+
const cnt: number[] = new Array(1010).fill(0);
185+
const n: number = nums.length;
186+
const dfs = (i: number) => {
187+
if (i >= n) {
188+
++ans;
189+
return;
190+
}
191+
dfs(i + 1);
192+
const ok1: boolean = nums[i] + k >= 1010 || cnt[nums[i] + k] === 0;
193+
const ok2: boolean = nums[i] - k < 0 || cnt[nums[i] - k] === 0;
194+
if (ok1 && ok2) {
195+
++cnt[nums[i]];
196+
dfs(i + 1);
197+
--cnt[nums[i]];
198+
}
199+
};
200+
dfs(0);
201+
return ans;
202+
}
203+
```
204+
179205
### **...**
180206

181207
```

solution/2500-2599/2597.The Number of Beautiful Subsets/README_EN.md

+26
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,32 @@ func beautifulSubsets(nums []int, k int) int {
155155
}
156156
```
157157

158+
### **TypeScript**
159+
160+
```ts
161+
function beautifulSubsets(nums: number[], k: number): number {
162+
let ans: number = -1;
163+
const cnt: number[] = new Array(1010).fill(0);
164+
const n: number = nums.length;
165+
const dfs = (i: number) => {
166+
if (i >= n) {
167+
++ans;
168+
return;
169+
}
170+
dfs(i + 1);
171+
const ok1: boolean = nums[i] + k >= 1010 || cnt[nums[i] + k] === 0;
172+
const ok2: boolean = nums[i] - k < 0 || cnt[nums[i] - k] === 0;
173+
if (ok1 && ok2) {
174+
++cnt[nums[i]];
175+
dfs(i + 1);
176+
--cnt[nums[i]];
177+
}
178+
};
179+
dfs(0);
180+
return ans;
181+
}
182+
```
183+
158184
### **...**
159185

160186
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function beautifulSubsets(nums: number[], k: number): number {
2+
let ans: number = -1;
3+
const cnt: number[] = new Array(1010).fill(0);
4+
const n: number = nums.length;
5+
const dfs = (i: number) => {
6+
if (i >= n) {
7+
++ans;
8+
return;
9+
}
10+
dfs(i + 1);
11+
const ok1: boolean = nums[i] + k >= 1010 || cnt[nums[i] + k] === 0;
12+
const ok2: boolean = nums[i] - k < 0 || cnt[nums[i] - k] === 0;
13+
if (ok1 && ok2) {
14+
++cnt[nums[i]];
15+
dfs(i + 1);
16+
--cnt[nums[i]];
17+
}
18+
};
19+
dfs(0);
20+
return ans;
21+
}

solution/2500-2599/2598.Smallest Missing Non-negative Integer After Operations/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,22 @@ func findSmallestInteger(nums []int, value int) int {
138138
}
139139
```
140140

141+
### **TypeScript**
142+
143+
```ts
144+
function findSmallestInteger(nums: number[], value: number): number {
145+
const cnt: number[] = new Array(value).fill(0);
146+
for (const x of nums) {
147+
++cnt[((x % value) + value) % value];
148+
}
149+
for (let i = 0; ; ++i) {
150+
if (cnt[i % value]-- === 0) {
151+
return i;
152+
}
153+
}
154+
}
155+
```
156+
141157
### **...**
142158

143159
```

solution/2500-2599/2598.Smallest Missing Non-negative Integer After Operations/README_EN.md

+16
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,22 @@ func findSmallestInteger(nums []int, value int) int {
122122
}
123123
```
124124

125+
### **TypeScript**
126+
127+
```ts
128+
function findSmallestInteger(nums: number[], value: number): number {
129+
const cnt: number[] = new Array(value).fill(0);
130+
for (const x of nums) {
131+
++cnt[((x % value) + value) % value];
132+
}
133+
for (let i = 0; ; ++i) {
134+
if (cnt[i % value]-- === 0) {
135+
return i;
136+
}
137+
}
138+
}
139+
```
140+
125141
### **...**
126142

127143
```

0 commit comments

Comments
 (0)