Skip to content

Commit a5619c8

Browse files
add 1349
1 parent 7abfd3a commit a5619c8

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1349|[Maximum Students Taking Exam](https://leetcode.com/problems/maximum-students-taking-exam/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1349.java) | |Hard|Dynamic Programming|
1112
|1348|[Tweet Counts Per Frequency](https://leetcode.com/problems/tweet-counts-per-frequency/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1348.java) | |Medium|Design|
1213
|1347|[Minimum Number of Steps to Make Two Strings Anagram](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1347.java) | |Easy|String|
1314
|1346|[Check If N and Its Double Exist](https://leetcode.com/problems/check-if-n-and-its-double-exist/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1346.java) | |Easy|Array|
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 1349. Maximum Students Taking Exam
7+
*
8+
* Given a m * n matrix seats that represent seats distributions in a classroom.
9+
* If a seat is broken, it is denoted by '#' character otherwise it is denoted by a '.' character.
10+
* Students can see the answers of those sitting next to the left, right, upper left and upper right, but he cannot see the answers of the student sitting directly in front or behind him. Return the maximum number of students that can take the exam together without any cheating being possible..
11+
* Students must be placed in seats in good condition.
12+
*
13+
* Example 1:
14+
* Input: seats = [["#",".","#","#",".","#"],
15+
* [".","#","#","#","#","."],
16+
* ["#",".","#","#",".","#"]]
17+
* Output: 4
18+
* Explanation: Teacher can place 4 students in available seats so they don't cheat on the exam.
19+
*
20+
* Example 2:
21+
* Input: seats = [[".","#"],
22+
* ["#","#"],
23+
* ["#","."],
24+
* ["#","#"],
25+
* [".","#"]]
26+
* Output: 3
27+
* Explanation: Place all students in available seats.
28+
*
29+
* Example 3:
30+
* Input: seats = [["#",".",".",".","#"],
31+
* [".","#",".","#","."],
32+
* [".",".","#",".","."],
33+
* [".","#",".","#","."],
34+
* ["#",".",".",".","#"]]
35+
* Output: 10
36+
* Explanation: Place students in available seats in column 1, 3 and 5.
37+
*
38+
* Constraints:
39+
* seats contains only characters '.' and'#'.
40+
* m == seats.length
41+
* n == seats[i].length
42+
* 1 <= m <= 8
43+
* 1 <= n <= 8
44+
* */
45+
public class _1349 {
46+
public static class Solution1 {
47+
/**credit: https://leetcode.com/problems/maximum-students-taking-exam/discuss/503686/A-simple-tutorial-on-this-bitmasking-problem*/
48+
public int maxStudents(char[][] seats) {
49+
int m = seats.length;
50+
int n = seats[0].length;
51+
int[] validRows = new int[m];
52+
for (int i = 0; i < m; i++) {
53+
for (int j = 0; j < n; j++) {
54+
validRows[i] = (validRows[i] << 1) + (seats[i][j] == '.' ? 1 : 0);
55+
}
56+
}
57+
int stateSize = 1 << n; // There are 2^n states for n columns in binary format
58+
int[][] dp = new int[m][stateSize];
59+
for (int i = 0; i < m; i++) {
60+
Arrays.fill(dp[i], -1);
61+
}
62+
int ans = 0;
63+
for (int i = 0; i < m; i++) {
64+
for (int j = 0; j < stateSize; j++) {
65+
if (((j & validRows[i]) == j) && ((j & (j << 1)) == 0)) {
66+
if (i == 0) {
67+
dp[i][j] = Integer.bitCount(j);
68+
} else {
69+
for (int k = 0; k < stateSize; k++) {
70+
if (((k << 1) & j) == 0 && ((j << 1) & k) == 0 && dp[i - 1][k] != -1) {
71+
dp[i][j] = Math.max(dp[i][j], dp[i - 1][k] + Integer.bitCount(j));
72+
}
73+
}
74+
}
75+
ans = Math.max(ans, dp[i][j]);
76+
}
77+
}
78+
}
79+
return ans;
80+
}
81+
}
82+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1349;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1349Test {
10+
private static _1349.Solution1 solution1;
11+
private static char[][] seats;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1349.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
seats = new char[][]{
21+
{'#', '.', '#', '#', '.', '#'},
22+
{'.', '#', '#', '#', '#', '.'},
23+
{'#', '.', '#', '#', '.', '#'}
24+
};
25+
assertEquals(4, solution1.maxStudents(seats));
26+
}
27+
28+
}

0 commit comments

Comments
 (0)