Skip to content

Commit 83e1de5

Browse files
Solved Problems
1 parent 2627da3 commit 83e1de5

File tree

3 files changed

+109
-2
lines changed

3 files changed

+109
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ My accepted leetcode solutions to some of the common interview problems.
208208
- [Student Attendance Record II](problems/src/dynamic_programming/StudentAttendanceRecordII.java) (Hard)
209209
- [Out of Boundary Paths](problems/src/dynamic_programming/OutOfBoundaryPaths.java) (Medium)
210210
- [Remove Boxes](problems/src/dynamic_programming/RemoveBoxes.java) (Hard)
211+
- [Stickers to Spell Word](problems/src/dynamic_programming/StickersToSpellWord.java) (Hard)
211212

212213
#### [Greedy](problems/src/greedy)
213214

problems/src/dynamic_programming/RemoveBoxes.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
* 3, 4, 3, 1] (3*3=9 points) ----> [1, 3, 3, 3, 1] (1*1=1 points) ----> [1, 1] (3*3=9 points) ---->
1313
* [] (2*2=4 points) Note: The number of boxes n would not exceed 100.
1414
*
15-
* Solution O(N ^ 4) For each sub-array [l, r] make a dp cache and calculate maximum of [l, i][1] + [i + 1, r][1] or
16-
* maximum of [l + 1, i - 1][n] + [i, r][1] where boxes[l] == boxes[i] where n is the count of repetitions
15+
* <p>Solution O(N ^ 4) For each sub-array [l, r] make a dp cache and calculate maximum of [l, i][1]
16+
* + [i + 1, r][1] or maximum of [l + 1, i - 1][n] + [i, r][1] where boxes[l] == boxes[i] where n is
17+
* the count of repetitions
1718
*/
1819
public class RemoveBoxes {
1920

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package dynamic_programming;
2+
3+
/**
4+
* Created by gouthamvidyapradhan on 14/07/2019 We are given N different types of stickers. Each
5+
* sticker has a lowercase English word on it.
6+
*
7+
* <p>You would like to spell out the given target string by cutting individual letters from your
8+
* collection of stickers and rearranging them.
9+
*
10+
* <p>You can use each sticker more than once if you want, and you have infinite quantities of each
11+
* sticker.
12+
*
13+
* <p>What is the minimum number of stickers that you need to spell out the target? If the task is
14+
* impossible, return -1.
15+
*
16+
* <p>Example 1:
17+
*
18+
* <p>Input:
19+
*
20+
* <p>["with", "example", "science"], "thehat" Output:
21+
*
22+
* <p>3 Explanation:
23+
*
24+
* <p>We can use 2 "with" stickers, and 1 "example" sticker. After cutting and rearrange the letters
25+
* of those stickers, we can form the target "thehat". Also, this is the minimum number of stickers
26+
* necessary to form the target string. Example 2:
27+
*
28+
* <p>Input:
29+
*
30+
* <p>["notice", "possible"], "basicbasic" Output:
31+
*
32+
* <p>-1 Explanation:
33+
*
34+
* <p>We can't form the target "basicbasic" from cutting letters from the given stickers. Note:
35+
*
36+
* <p>stickers has length in the range [1, 50]. stickers consists of lowercase English words
37+
* (without apostrophes). target has length in the range [1, 15], and consists of lowercase English
38+
* letters. In all test cases, all words were chosen randomly from the 1000 most common US English
39+
* words, and the target was chosen as a concatenation of two random words. The time limit may be
40+
* more challenging than usual. It is expected that a 50 sticker test case can be solved within 35ms
41+
* on average.
42+
*
43+
* <p>Solution: O(2 ^ T x T x S) where T is the length of target and S is length of sticker array.
44+
* Each state is a combination of characters selected in the target sticker plus the total count of
45+
* stickers used. Cache the minimum count in each state and explore all the different possible
46+
* states.
47+
*/
48+
public class StickersToSpellWord {
49+
/**
50+
* Main method
51+
*
52+
* @param args
53+
*/
54+
public static void main(String[] args) {
55+
String[] stickers = {"bright", "neighbor", "capital"};
56+
57+
System.out.println(new StickersToSpellWord().minStickers(stickers, "originalchair"));
58+
}
59+
60+
private int destination = 0;
61+
private int min = Integer.MAX_VALUE;
62+
private int[][] DP;
63+
64+
public int minStickers(String[] stickers, String target) {
65+
for (int i = 0; i < target.length(); i++) {
66+
destination |= (1 << i);
67+
}
68+
DP = new int[destination][target.length() + 1];
69+
int answer = dp(stickers, target, 0, 0);
70+
return answer == Integer.MAX_VALUE ? -1 : answer;
71+
}
72+
73+
private int dp(String[] stickers, String target, int curr, int count) {
74+
if (curr == destination) {
75+
return count;
76+
} else {
77+
if (count > min) return Integer.MAX_VALUE;
78+
if (DP[curr][count] != 0) return DP[curr][count];
79+
DP[curr][count] = Integer.MAX_VALUE;
80+
for (String s : stickers) {
81+
int temp = 0;
82+
char[] arr = s.toCharArray();
83+
for (int i = 0, l = target.length(); i < l; i++) {
84+
if ((curr & (1 << i)) == 0) {
85+
char targetChar = target.charAt(i);
86+
for (int j = 0; j < arr.length; j++) {
87+
if (arr[j] == targetChar) {
88+
arr[j] = '0';
89+
temp |= (1 << i);
90+
break;
91+
}
92+
}
93+
}
94+
}
95+
if (temp > 0) {
96+
int child = (curr | temp);
97+
int retValue = dp(stickers, target, child, count + 1);
98+
DP[curr][count] = Math.min(DP[curr][count], retValue);
99+
min = Math.min(min, DP[curr][count]);
100+
}
101+
}
102+
return DP[curr][count];
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)