Skip to content

Commit c68ad3a

Browse files
author
Sherali Obidov
committed
rabbits in the forest
1 parent 757469c commit c68ad3a

3 files changed

Lines changed: 132 additions & 1 deletion

File tree

‎src/google/EscapePods.java‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package google;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Why Did you create this class? what does it do?
7+
*/
8+
@SuppressWarnings("Duplicates")
9+
public class EscapePods {
10+
public static void main(String[] args) {
11+
System.out.println(answer(new int[] { 0, 1 }, new int[] { 4, 5 }, new int[][] {
12+
{ 0, 0, 4, 6, 0, 0 },
13+
{ 0, 0, 5, 2, 0, 0 },
14+
{ 0, 0, 0, 0, 4, 4 },
15+
{ 0, 0, 0, 0, 6, 6 },
16+
{ 0, 0, 0, 0, 0, 0 },
17+
{ 0, 0, 0, 0, 0, 0 }
18+
}));
19+
System.out.println(answer(new int[] { 0 }, new int[] { 3 }, new int[][] {
20+
{ 0, 7, 0, 0 },
21+
{ 0, 0, 6, 0 },
22+
{ 0, 0, 0, 8 },
23+
{ 9, 0, 0, 0 },
24+
}));
25+
}
26+
27+
public static int answer(int[] entrances, int[] exits, int[][] path) {
28+
if (entrances == null || entrances.length == 0 || exits == null || exits.length == 0)
29+
return 0;
30+
if (path == null || path.length == 0)
31+
return 0;
32+
int res = 0;
33+
int[] rooms = new int[50];
34+
Queue<Integer> q = new LinkedList<>();
35+
Set<Integer> setE = new HashSet<>();
36+
for (int i = 0; i < entrances.length; i++) {
37+
rooms[entrances[i]] = -1;
38+
q.add(entrances[i]);
39+
setE.add(entrances[i]);
40+
}
41+
for (int exit : exits) {
42+
setE.add(exit);
43+
}
44+
45+
while (!q.isEmpty()) {
46+
int room = q.remove();
47+
setE.remove(room);
48+
int[] nei = path[room];
49+
for (int i = 0; i < nei.length; i++) {
50+
if (nei[i] == 0)
51+
continue;
52+
int val = nei[i];
53+
if (rooms[room] != -1) {
54+
val = Math.min(val, rooms[room]);
55+
rooms[room] -= val;
56+
}
57+
rooms[i] += val;
58+
if (setE.add(i)) {
59+
q.add(i);
60+
}
61+
}
62+
}
63+
64+
for (int i = 0; i < exits.length; i++) {
65+
res += rooms[exits[i]];
66+
}
67+
68+
return res;
69+
}
70+
71+
}

‎src/problems/Medium.txt‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2194,4 +2194,29 @@ Note:
21942194
The length of both num1 and num2 is < 110.
21952195
Both num1 and num2 contains only digits 0-9.
21962196
Both num1 and num2 does not contain any leading zero.
2197-
You must not use any built-in BigInteger library or convert the inputs to integer directly.
2197+
You must not use any built-in BigInteger library or convert the inputs to integer directly.
2198+
2199+
166) Rabbits in Forest
2200+
In a forest, each rabbit has some color. Some subset of rabbits (possibly all of them) tell you how many other rabbits have the same color as them. Those answers are placed in an array.
2201+
2202+
Return the minimum number of rabbits that could be in the forest.
2203+
2204+
Examples:
2205+
Input: answers = [1, 1, 2]
2206+
Output: 5
2207+
Explanation:
2208+
The two rabbits that answered "1" could both be the same color, say red.
2209+
The rabbit than answered "2" can't be red or the answers would be inconsistent.
2210+
Say the rabbit that answered "2" was blue.
2211+
Then there should be 2 other blue rabbits in the forest that didn't answer into the array.
2212+
The smallest possible number of rabbits in the forest is therefore 5: 3 that answered plus 2 that didn't.
2213+
2214+
Input: answers = [10, 10, 10]
2215+
Output: 11
2216+
2217+
Input: answers = []
2218+
Output: 0
2219+
Note:
2220+
2221+
answers will have length at most 1000.
2222+
Each answers[i] will be an integer in the range [0, 999].
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package problems.medium;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Why Did you create this class? what does it do?
8+
*/
9+
public class RabbitsinForest {
10+
11+
public int numRabbits(int[] a) {
12+
if (a == null || a.length == 0)
13+
return 0;
14+
Map<Integer, Integer> map = new HashMap<>();
15+
for (int i = 0; i < a.length; i++) {
16+
int v = map.getOrDefault(a[i], 0);
17+
map.put(a[i], ++v);
18+
}
19+
int res = 0;
20+
for (Integer key : map.keySet()) {
21+
int v = map.get(key);
22+
if (key == 0) {
23+
res += v;
24+
continue;
25+
}
26+
if (key + 1 < v) {
27+
key++;
28+
res += key * (Math.ceil(v * 1.0 / key));
29+
} else {
30+
res += key + 1;
31+
}
32+
}
33+
return res;
34+
}
35+
}

0 commit comments

Comments
 (0)