Skip to content

Commit dbfd945

Browse files
[N-0] add 691
1 parent c0080e5 commit dbfd945

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Your ideas/fixes/algorithms are more than welcome!
3636
|694|[Number of Distinct Islands](https://leetcode.com/problems/number-of-distinct-islands/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_694.java) | O(m*n) | O(1) | Medium | DFS
3737
|693|[Binary Number with Alternating Bits](https://leetcode.com/problems/binary-number-with-alternating-bits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_693.java) | O(n) | O(1) | Easy |
3838
|692|[Top K Frequent Words](https://leetcode.com/problems/top-k-frequent-words/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_692.java) | O(nlogk) | O(n) | Medium |
39+
|691|[Stickers to Spell Word](https://leetcode.com/problems/stickers-to-spell-word/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_691.java) | O(?) | O(?) | Hard | DP
3940
|690|[Employee Importance](https://leetcode.com/problems/employee-importance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_690.java) | O(n) | O(h) | Easy | DFS
4041
|689|[Maximum Sum of 3 Non-Overlapping Subarrays](https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_689.java) | O(n) | O(n) | Hard | DP
4142
|688|[Knight Probability in Chessboard](https://leetcode.com/problems/knight-probability-in-chessboard/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_688.java) | O(n^2) | O(n^2) | Medium | DP
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* 691. Stickers to Spell Word
8+
*
9+
* We are given N different types of stickers. Each sticker has a lowercase English word on it.
10+
* You would like to spell out the given target string by cutting individual letters from your collection of stickers and rearranging them.
11+
* You can use each sticker more than once if you want, and you have infinite quantities of each sticker.
12+
* What is the minimum number of stickers that you need to spell out the target? If the task is impossible, return -1.
13+
14+
Example 1:
15+
Input:
16+
["with", "example", "science"], "thehat"
17+
Output:
18+
3
19+
20+
Explanation:
21+
We can use 2 "with" stickers, and 1 "example" sticker.
22+
After cutting and rearrange the letters of those stickers, we can form the target "thehat".
23+
Also, this is the minimum number of stickers necessary to form the target string.
24+
25+
Example 2:
26+
Input:
27+
["notice", "possible"], "basicbasic"
28+
Output:
29+
-1
30+
31+
Explanation:
32+
We can't form the target "basicbasic" from cutting letters from the given stickers.
33+
34+
Note:
35+
stickers has length in the range [1, 50].
36+
stickers consists of lowercase English words (without apostrophes).
37+
target has length in the range [1, 15], and consists of lowercase English letters.
38+
In all test cases, all words were chosen randomly from the 1000 most common US English words, and the target was chosen as a concatenation of two random words.
39+
The time limit may be more challenging than usual. It is expected that a 50 sticker test case can be solved within 35ms on average.
40+
*/
41+
public class _691 {
42+
public static class Solution1 {
43+
/**
44+
* credit: https://discuss.leetcode.com/topic/106273/c-java-python-dp-memoization-with-optimization-29-ms-c/2
45+
*/
46+
public int minStickers(String[] stickers, String target) {
47+
int m = stickers.length;
48+
int[][] mp = new int[m][26];
49+
Map<String, Integer> dp = new HashMap<>();
50+
for (int i = 0; i < m; i++) {
51+
for (char c : stickers[i].toCharArray()) {
52+
mp[i][c - 'a']++;
53+
}
54+
}
55+
dp.put("", 0);
56+
return helper(dp, mp, target);
57+
}
58+
59+
private int helper(Map<String, Integer> dp, int[][] mp, String target) {
60+
if (dp.containsKey(target)) {
61+
return dp.get(target);
62+
}
63+
int ans = Integer.MAX_VALUE;
64+
int n = mp.length;
65+
int[] tar = new int[26];
66+
for (char c : target.toCharArray()) {
67+
tar[c - 'a']++;
68+
}
69+
// try every sticker
70+
for (int i = 0; i < n; i++) {
71+
// optimization
72+
if (mp[i][target.charAt(0) - 'a'] == 0) {
73+
continue;
74+
}
75+
StringBuilder sb = new StringBuilder();
76+
// apply a sticker on every character a-z
77+
for (int j = 0; j < 26; j++) {
78+
if (tar[j] > 0) {
79+
for (int k = 0; k < Math.max(0, tar[j] - mp[i][j]); k++) {
80+
sb.append((char) ('a' + j));
81+
}
82+
}
83+
}
84+
String s = sb.toString();
85+
int tmp = helper(dp, mp, s);
86+
if (tmp != -1) {
87+
ans = Math.min(ans, 1 + tmp);
88+
}
89+
}
90+
dp.put(target, ans == Integer.MAX_VALUE ? -1 : ans);
91+
return dp.get(target);
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy