Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1944b38

Browse files
feat: add solutions to lc problem: No.1601 (doocs#2543)
No.1601.Maximum Number of Achievable Transfer Requests
1 parent 5812f34 commit 1944b38

File tree

4 files changed

+151
-4
lines changed

4 files changed

+151
-4
lines changed

‎solution/1600-1699/1601.Maximum Number of Achievable Transfer Requests/README.md‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ function maximumRequests(n: number, requests: number[][]): number {
201201
const m = requests.length;
202202
let ans = 0;
203203
const check = (mask: number): boolean => {
204-
const cnt = newArray(n).fill(0);
204+
const cnt = Array(n).fill(0);
205205
for (let i = 0; i < m; ++i) {
206206
if ((mask >> i) & 1) {
207207
const [f, t] = requests[i];
@@ -269,6 +269,54 @@ function bitCount(i) {
269269
}
270270
```
271271

272+
```cs
273+
public class Solution {
274+
private int m;
275+
private int n;
276+
private int[][] requests;
277+
278+
public int MaximumRequests(int n, int[][] requests) {
279+
m = requests.Length;
280+
this.n = n;
281+
this.requests = requests;
282+
int ans = 0;
283+
for (int mask = 0; mask < (1 << m); ++mask) {
284+
int cnt = CountBits(mask);
285+
if (ans < cnt && Check(mask)) {
286+
ans = cnt;
287+
}
288+
}
289+
return ans;
290+
}
291+
292+
private bool Check(int mask) {
293+
int[] cnt = new int[n];
294+
for (int i = 0; i < m; ++i) {
295+
if (((mask >> i) & 1) == 1) {
296+
int f = requests[i][0], t = requests[i][1];
297+
--cnt[f];
298+
++cnt[t];
299+
}
300+
}
301+
foreach (int v in cnt) {
302+
if (v != 0) {
303+
return false;
304+
}
305+
}
306+
return true;
307+
}
308+
309+
private int CountBits(int n) {
310+
int count = 0;
311+
while (n > 0) {
312+
n -= n & -n;
313+
++count;
314+
}
315+
return count;
316+
}
317+
}
318+
```
319+
272320
<!-- tabs:end -->
273321

274322
<!-- end -->

‎solution/1600-1699/1601.Maximum Number of Achievable Transfer Requests/README_EN.md‎

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ We can achieve all the requests. </pre>
6060

6161
## Solutions
6262

63-
### Solution 1
63+
### Solution 1: Binary Enumeration
64+
65+
We note that the length of the room change request list does not exceed 16ドル$. Therefore, we can use the method of binary enumeration to enumerate all room change request lists. Specifically, we can use a binary number of length 16ドル$ to represent a room change request list, where the $i$-th bit being 1ドル$ means the $i$-th room change request is selected, and 0ドル$ means the $i$-th room change request is not selected.
66+
67+
We enumerate all binary numbers in the range of $[1, 2^{m}),ドル for each binary number $mask,ドル we first calculate how many 1ドル$s are in its binary representation, denoted as $cnt$. If $cnt$ is larger than the current answer $ans,ドル then we judge whether $mask$ is a feasible room change request list. If it is, then we update the answer $ans$ with $cnt$. To judge whether $mask$ is a feasible room change request list, we only need to check whether the net inflow of each room is 0ドル$.
68+
69+
The time complexity is $O(2^m \times (m + n)),ドル and the space complexity is $O(n),ドル where $m$ and $n$ are the lengths of the room change request list and the number of rooms, respectively.
6470

6571
<!-- tabs:start -->
6672

@@ -190,7 +196,7 @@ function maximumRequests(n: number, requests: number[][]): number {
190196
const m = requests.length;
191197
let ans = 0;
192198
const check = (mask: number): boolean => {
193-
const cnt = newArray(n).fill(0);
199+
const cnt = Array(n).fill(0);
194200
for (let i = 0; i < m; ++i) {
195201
if ((mask >> i) & 1) {
196202
const [f, t] = requests[i];
@@ -258,6 +264,54 @@ function bitCount(i) {
258264
}
259265
```
260266

267+
```cs
268+
public class Solution {
269+
private int m;
270+
private int n;
271+
private int[][] requests;
272+
273+
public int MaximumRequests(int n, int[][] requests) {
274+
m = requests.Length;
275+
this.n = n;
276+
this.requests = requests;
277+
int ans = 0;
278+
for (int mask = 0; mask < (1 << m); ++mask) {
279+
int cnt = CountBits(mask);
280+
if (ans < cnt && Check(mask)) {
281+
ans = cnt;
282+
}
283+
}
284+
return ans;
285+
}
286+
287+
private bool Check(int mask) {
288+
int[] cnt = new int[n];
289+
for (int i = 0; i < m; ++i) {
290+
if (((mask >> i) & 1) == 1) {
291+
int f = requests[i][0], t = requests[i][1];
292+
--cnt[f];
293+
++cnt[t];
294+
}
295+
}
296+
foreach (int v in cnt) {
297+
if (v != 0) {
298+
return false;
299+
}
300+
}
301+
return true;
302+
}
303+
304+
private int CountBits(int n) {
305+
int count = 0;
306+
while (n > 0) {
307+
n -= n & -n;
308+
++count;
309+
}
310+
return count;
311+
}
312+
}
313+
```
314+
261315
<!-- tabs:end -->
262316

263317
<!-- end -->
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
public class Solution {
2+
private int m;
3+
private int n;
4+
private int[][] requests;
5+
6+
public int MaximumRequests(int n, int[][] requests) {
7+
m = requests.Length;
8+
this.n = n;
9+
this.requests = requests;
10+
int ans = 0;
11+
for (int mask = 0; mask < (1 << m); ++mask) {
12+
int cnt = CountBits(mask);
13+
if (ans < cnt && Check(mask)) {
14+
ans = cnt;
15+
}
16+
}
17+
return ans;
18+
}
19+
20+
private bool Check(int mask) {
21+
int[] cnt = new int[n];
22+
for (int i = 0; i < m; ++i) {
23+
if (((mask >> i) & 1) == 1) {
24+
int f = requests[i][0], t = requests[i][1];
25+
--cnt[f];
26+
++cnt[t];
27+
}
28+
}
29+
foreach (int v in cnt) {
30+
if (v != 0) {
31+
return false;
32+
}
33+
}
34+
return true;
35+
}
36+
37+
private int CountBits(int n) {
38+
int count = 0;
39+
while (n > 0) {
40+
n -= n & -n;
41+
++count;
42+
}
43+
return count;
44+
}
45+
}

‎solution/1600-1699/1601.Maximum Number of Achievable Transfer Requests/Solution.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ function maximumRequests(n: number, requests: number[][]): number {
22
const m = requests.length;
33
let ans = 0;
44
const check = (mask: number): boolean => {
5-
const cnt = newArray(n).fill(0);
5+
const cnt = Array(n).fill(0);
66
for (let i = 0; i < m; ++i) {
77
if ((mask >> i) & 1) {
88
const [f, t] = requests[i];

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /