-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
101 additions
and
40 deletions.
There are no files selected for viewing
109 changes: 69 additions & 40 deletions
109
src/main/java/g1801_1900/s1815_maximum_number_of_groups_getting_fresh_donuts/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,89 @@ | ||
package g1801_1900.s1815_maximum_number_of_groups_getting_fresh_donuts; | ||
|
||
// #Hard #Array #Dynamic_Programming #Bit_Manipulation #Bitmask #Memoization | ||
// #2022_05_03_Time_7_ms_(86.67%)_Space_43.6_MB_(73.33%) | ||
// #2025_02_21_Time_2_ms_(100.00%)_Space_41.56_MB_(100.00%) | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Solution { | ||
private static final Map<String, Integer> MAP = new HashMap<>(); | ||
|
||
public int maxHappyGroups(int batchSize, int[] groups) { | ||
int[] count = new int[batchSize]; | ||
int res = 0; | ||
int remGroup = 0; | ||
for (int group : groups) { | ||
int g = group % batchSize; | ||
if (g == 0) { | ||
res++; | ||
} else if (count[batchSize - g] > 0) { | ||
remGroup--; | ||
res++; | ||
count[batchSize - g]--; | ||
} else { | ||
count[g]++; | ||
remGroup++; | ||
} | ||
if (batchSize == 1) { | ||
return groups.length; | ||
} | ||
int[] withSize = new int[batchSize]; | ||
for (int size : groups) { | ||
withSize[size % batchSize]++; | ||
} | ||
int fromZero = withSize[0]; | ||
withSize[0] = 0; | ||
int fromEnds = 0; | ||
for (int l = 1, r = batchSize - 1; l < r; l++, r--) { | ||
int usable = Math.min(withSize[l], withSize[r]); | ||
fromEnds += usable; | ||
withSize[l] -= usable; | ||
withSize[r] -= usable; | ||
} | ||
res += dfs(0, remGroup, count, batchSize); | ||
return res; | ||
int fromMid = 0; | ||
if (batchSize % 2 == 0) { | ||
fromMid = withSize[batchSize / 2] / 2; | ||
withSize[batchSize / 2] -= fromMid * 2; | ||
} | ||
return get(pruneEnd(withSize), batchSize, 0, new HashMap<>()) | ||
+ fromZero | ||
+ fromEnds | ||
+ fromMid; | ||
} | ||
|
||
private int dfs(int curr, int remain, int[] count, int batch) { | ||
if (remain == 0) { | ||
return 0; | ||
private int get(int[] ar, int batchSize, int rem, Map<Long, Integer> cache) { | ||
long hash = 0; | ||
for (int e : ar) { | ||
hash = hash * 69L + e; | ||
} | ||
int res = 0; | ||
String s = Arrays.toString(count); | ||
if (MAP.containsKey(s)) { | ||
return MAP.get(s); | ||
Integer fromCache = cache.get(hash); | ||
if (fromCache != null) { | ||
return fromCache; | ||
} | ||
if (curr == 0) { | ||
res++; | ||
curr = batch; | ||
if (zeroed(ar)) { | ||
cache.put(hash, 0); | ||
return 0; | ||
} | ||
int val = 0; | ||
for (int i = 1; i < count.length; i++) { | ||
if (count[i] == 0) { | ||
int max = 0; | ||
for (int i = 0; i < ar.length; i++) { | ||
if (ar[i] == 0) { | ||
continue; | ||
} | ||
count[i]--; | ||
val = Math.max(val, dfs((curr - i + batch) % batch, remain - 1, count, batch)); | ||
count[i]++; | ||
ar[i]--; | ||
int from = get(ar, batchSize, (rem + i) % batchSize, cache); | ||
if (from > max) { | ||
max = from; | ||
} | ||
ar[i]++; | ||
} | ||
int score = max + (rem == 0 ? 1 : 0); | ||
cache.put(hash, score); | ||
return score; | ||
} | ||
|
||
private int[] pruneEnd(int[] in) { | ||
int endingZeros = 0; | ||
for (int i = in.length - 1; i >= 0; i--) { | ||
if (in[i] != 0) { | ||
break; | ||
} | ||
endingZeros++; | ||
} | ||
int[] out = new int[in.length - endingZeros]; | ||
System.arraycopy(in, 0, out, 0, out.length); | ||
return out; | ||
} | ||
|
||
private boolean zeroed(int[] ar) { | ||
for (int e : ar) { | ||
if (e != 0) { | ||
return false; | ||
} | ||
} | ||
res += val; | ||
MAP.put(s, res); | ||
return res; | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters