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 96855b9

Browse files
add java solutions of 913 ~ 915
1 parent 1cdcaf1 commit 96855b9

File tree

4 files changed

+214
-0
lines changed

4 files changed

+214
-0
lines changed

‎README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,10 @@ You can also ask for problem solving ideas and discuss in GitHub issues directly
803803
|909|[Snakes and Ladders](https://leetcode.com/problems/snakes-and-ladders/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_909.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
804804
|910|[Smallest Range II](https://leetcode.com/problems/smallest-range-ii/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_910.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
805805
|911|[Online Election](https://leetcode.com/problems/online-election/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_911.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
806+
|913|[Cat and Mouse](https://leetcode.com/problems/cat-and-mouse/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_913.java) & Python |![hard](https://github.com/guobinhit/myleetcode/blob/master/images/hard.png)| Others
807+
|914|[X of a Kind in a Deck of Cards](https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_914.java) & Python |![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Others
808+
|915|[Partition Array into Disjoint Intervals ](https://leetcode.com/problems/partition-array-into-disjoint-intervals/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_915.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
809+
806810

807811

808812

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.hit.basmath.learn.others;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 913. Cat and Mouse
7+
* <p>
8+
* A game on an undirected graph is played by two players, Mouse and Cat, who alternate turns.
9+
* <p>
10+
* The graph is given as follows: graph[a] is a list of all nodes b such that ab is an edge of the graph.
11+
* <p>
12+
* Mouse starts at node 1 and goes first, Cat starts at node 2 and goes second, and there is a Hole at node 0.
13+
* <p>
14+
* During each player's turn, they must travel along one edge of the graph that meets where they are. For example, if the Mouse is at node 1, it must travel to any node in graph[1].
15+
* <p>
16+
* Additionally, it is not allowed for the Cat to travel to the Hole (node 0.)
17+
* <p>
18+
* Then, the game can end in 3 ways:
19+
* <p>
20+
* If ever the Cat occupies the same node as the Mouse, the Cat wins.
21+
* If ever the Mouse reaches the Hole, the Mouse wins.
22+
* If ever a position is repeated (ie. the players are in the same position as a previous turn, and it is the same player's turn to move), the game is a draw.
23+
* Given a graph, and assuming both players play optimally, return 1 if the game is won by Mouse, 2 if the game is won by Cat, and 0 if the game is a draw.
24+
* <p>
25+
* Example 1:
26+
* <p>
27+
* Input: [[2,5],[3],[0,4,5],[1,4,5],[2,3],[0,2,3]]
28+
* Output: 0
29+
* Explanation:
30+
* 4---3---1
31+
* | |
32+
* 2---5
33+
* \ /
34+
* 0
35+
* <p>
36+
* Note:
37+
* <p>
38+
* 3 <= graph.length <= 50
39+
* It is guaranteed that graph[1] is non-empty.
40+
* It is guaranteed that graph[2] contains a non-zero element.
41+
*/
42+
public class _913 {
43+
public int catMouseGame(int[][] graph) {
44+
int n = graph.length;
45+
int[][][] f = new int[n + n][n][n];
46+
fill(f, -1);
47+
48+
return find(graph, f, 0, 1, 2);
49+
}
50+
51+
private int find(int[][] graph, int[][][] f, int t, int x /* mouse */, int y /* cat */) {
52+
if (t == graph.length * 2) return 0; // draw
53+
if (x == 0) return 1; // mouse wins
54+
if (x == y) return 2; // cat wins
55+
if (f[t][x][y] != -1) return f[t][x][y];
56+
57+
if (t % 2 == 0) {
58+
// mouse moves
59+
// for the next position that mouse gonna move to,
60+
// if all possibilities lead to cat-win, then it is
61+
// surely the cat will win.
62+
// otherwise, if any 1 possibility leads to mouse-win,
63+
// the mouse should move to that position and then the
64+
// mouse will win.
65+
// otherwise, it is a draw.
66+
boolean cat_win = true;
67+
for (int next : graph[x]) {
68+
int r = find(graph, f, t + 1, next, y);
69+
if (r == 1) {
70+
return f[t][x][y] = 1;
71+
}
72+
if (r == 0) {
73+
cat_win = false;
74+
}
75+
}
76+
if (cat_win) return f[t][x][y] = 2;
77+
else return f[t][x][y] = 0;
78+
} else {
79+
// cat moves
80+
// the analysis is similar as above.
81+
boolean mouse_win = true;
82+
for (int next : graph[y]) {
83+
if (next == 0) continue; // cat cannot go to hole
84+
int r = find(graph, f, t + 1, x, next);
85+
if (r == 2) {
86+
return f[t][x][y] = 2;
87+
}
88+
if (r == 0) {
89+
mouse_win = false;
90+
}
91+
}
92+
if (mouse_win) return f[t][x][y] = 1;
93+
else return f[t][x][y] = 0;
94+
}
95+
}
96+
97+
private void fill(int[][][] f, int val) {
98+
for (int[][] ints : f) {
99+
for (int[] anInt : ints) {
100+
Arrays.fill(anInt, val);
101+
}
102+
}
103+
}
104+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.hit.basmath.learn.others;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* 914. X of a Kind in a Deck of Cards
8+
* <p>
9+
* In a deck of cards, each card has an integer written on it.
10+
* <p>
11+
* Return true if and only if you can choose X >= 2 such that it is possible to split the entire deck into 1 or more groups of cards, where:
12+
* <p>
13+
* Each group has exactly X cards.
14+
* All the cards in each group have the same integer.
15+
* <p>
16+
* Example 1:
17+
* <p>
18+
* Input: [1,2,3,4,4,3,2,1]
19+
* Output: true
20+
* Explanation: Possible partition [1,1],[2,2],[3,3],[4,4]
21+
* <p>
22+
* Example 2:
23+
* <p>
24+
* Input: [1,1,1,2,2,2,3,3]
25+
* Output: false
26+
* Explanation: No possible partition.
27+
* <p>
28+
* Example 3:
29+
* <p>
30+
* Input: [1]
31+
* Output: false
32+
* Explanation: No possible partition.
33+
* <p>
34+
* Example 4:
35+
* <p>
36+
* Input: [1,1]
37+
* Output: true
38+
* Explanation: Possible partition [1,1]
39+
* <p>
40+
* Example 5:
41+
* <p>
42+
* Input: [1,1,2,2,2,2]
43+
* Output: true
44+
* Explanation: Possible partition [1,1],[2,2],[2,2]
45+
* <p>
46+
* Note:
47+
* <p>
48+
* 1 <= deck.length <= 10000
49+
* 0 <= deck[i] < 10000
50+
*/
51+
public class _914 {
52+
public boolean hasGroupsSizeX(int[] deck) {
53+
Map<Integer, Integer> count = new HashMap<>();
54+
int res = 0;
55+
for (int i : deck) count.put(i, count.getOrDefault(i, 0) + 1);
56+
for (int i : count.values()) res = gcd(i, res);
57+
return res > 1;
58+
}
59+
60+
private int gcd(int a, int b) {
61+
return b > 0 ? gcd(b, a % b) : a;
62+
}
63+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.hit.basmath.learn.others;
2+
3+
/**
4+
* 915. Partition Array into Disjoint Intervals
5+
* <p>
6+
* Given an array A, partition it into two (contiguous) subarrays left and right so that:
7+
* <p>
8+
* Every element in left is less than or equal to every element in right.
9+
* left and right are non-empty.
10+
* left has the smallest possible size.
11+
* Return the length of left after such a partitioning. It is guaranteed that such a partitioning exists.
12+
* <p>
13+
* Example 1:
14+
* <p>
15+
* Input: [5,0,3,8,6]
16+
* Output: 3
17+
* Explanation: left = [5,0,3], right = [8,6]
18+
* <p>
19+
* Example 2:
20+
* <p>
21+
* Input: [1,1,1,0,6,12]
22+
* Output: 4
23+
* Explanation: left = [1,1,1,0], right = [6,12]
24+
* <p>
25+
* Note:
26+
* <p>
27+
* 2 <= A.length <= 30000
28+
* 0 <= A[i] <= 10^6
29+
* It is guaranteed there is at least one way to partition A as described.
30+
*/
31+
public class _915 {
32+
public int partitionDisjoint(int[] a) {
33+
int localMax = a[0], partitionIdx = 0, max = localMax;
34+
for (int i = 1; i < a.length; i++)
35+
if (localMax > a[i]) {
36+
localMax = max;
37+
partitionIdx = i;
38+
} else {
39+
max = Math.max(max, a[i]);
40+
}
41+
return partitionIdx + 1;
42+
}
43+
}

0 commit comments

Comments
(0)

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