Skip to content

feat: add solutions to lc problem: No.1601 #2543

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
Apr 7, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ function maximumRequests(n: number, requests: number[][]): number {
const m = requests.length;
let ans = 0;
const check = (mask: number): boolean => {
const cnt = new Array(n).fill(0);
const cnt = Array(n).fill(0);
for (let i = 0; i < m; ++i) {
if ((mask >> i) & 1) {
const [f, t] = requests[i];
Expand Down Expand Up @@ -269,6 +269,54 @@ function bitCount(i) {
}
```

```cs
public class Solution {
private int m;
private int n;
private int[][] requests;

public int MaximumRequests(int n, int[][] requests) {
m = requests.Length;
this.n = n;
this.requests = requests;
int ans = 0;
for (int mask = 0; mask < (1 << m); ++mask) {
int cnt = CountBits(mask);
if (ans < cnt && Check(mask)) {
ans = cnt;
}
}
return ans;
}

private bool Check(int mask) {
int[] cnt = new int[n];
for (int i = 0; i < m; ++i) {
if (((mask >> i) & 1) == 1) {
int f = requests[i][0], t = requests[i][1];
--cnt[f];
++cnt[t];
}
}
foreach (int v in cnt) {
if (v != 0) {
return false;
}
}
return true;
}

private int CountBits(int n) {
int count = 0;
while (n > 0) {
n -= n & -n;
++count;
}
return count;
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ We can achieve all the requests. </pre>

## Solutions

### Solution 1
### Solution 1: Binary Enumeration

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.

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$.

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.

<!-- tabs:start -->

Expand Down Expand Up @@ -190,7 +196,7 @@ function maximumRequests(n: number, requests: number[][]): number {
const m = requests.length;
let ans = 0;
const check = (mask: number): boolean => {
const cnt = new Array(n).fill(0);
const cnt = Array(n).fill(0);
for (let i = 0; i < m; ++i) {
if ((mask >> i) & 1) {
const [f, t] = requests[i];
Expand Down Expand Up @@ -258,6 +264,54 @@ function bitCount(i) {
}
```

```cs
public class Solution {
private int m;
private int n;
private int[][] requests;

public int MaximumRequests(int n, int[][] requests) {
m = requests.Length;
this.n = n;
this.requests = requests;
int ans = 0;
for (int mask = 0; mask < (1 << m); ++mask) {
int cnt = CountBits(mask);
if (ans < cnt && Check(mask)) {
ans = cnt;
}
}
return ans;
}

private bool Check(int mask) {
int[] cnt = new int[n];
for (int i = 0; i < m; ++i) {
if (((mask >> i) & 1) == 1) {
int f = requests[i][0], t = requests[i][1];
--cnt[f];
++cnt[t];
}
}
foreach (int v in cnt) {
if (v != 0) {
return false;
}
}
return true;
}

private int CountBits(int n) {
int count = 0;
while (n > 0) {
n -= n & -n;
++count;
}
return count;
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
public class Solution {
private int m;
private int n;
private int[][] requests;

public int MaximumRequests(int n, int[][] requests) {
m = requests.Length;
this.n = n;
this.requests = requests;
int ans = 0;
for (int mask = 0; mask < (1 << m); ++mask) {
int cnt = CountBits(mask);
if (ans < cnt && Check(mask)) {
ans = cnt;
}
}
return ans;
}

private bool Check(int mask) {
int[] cnt = new int[n];
for (int i = 0; i < m; ++i) {
if (((mask >> i) & 1) == 1) {
int f = requests[i][0], t = requests[i][1];
--cnt[f];
++cnt[t];
}
}
foreach (int v in cnt) {
if (v != 0) {
return false;
}
}
return true;
}

private int CountBits(int n) {
int count = 0;
while (n > 0) {
n -= n & -n;
++count;
}
return count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ function maximumRequests(n: number, requests: number[][]): number {
const m = requests.length;
let ans = 0;
const check = (mask: number): boolean => {
const cnt = new Array(n).fill(0);
const cnt = Array(n).fill(0);
for (let i = 0; i < m; ++i) {
if ((mask >> i) & 1) {
const [f, t] = requests[i];
Expand Down