You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given string A representative a positive integer which has N digits, remove any k digits of the number, the remaining digits are arranged according to the original order to become a new positive integer.
7
+
Given string A representative a positive integer which has N digits, remove any k digits of the number,
8
+
the remaining digits are arranged according to the original order to become a new positive integer.
3
9
4
10
Find the smallest integer after remove k digits.
5
11
@@ -13,6 +19,10 @@
13
19
Tags Expand
14
20
Greedy LintCode Copyright
15
21
22
+
*/
23
+
24
+
/*
25
+
16
26
Attempt2,Thoughts:
17
27
loop k times: each interation, find one digit to remove
18
28
Rules: want to remove whatever digit at A[i] that's A[i] > A[i+1].
@@ -21,14 +31,9 @@ Well... thinking straight (attempt2) seems much easier to understand and to code
21
31
22
32
Note:
23
33
remember to remove the prefixing 0's
24
-
*/
25
34
35
+
*/
26
36
publicclassSolution {
27
-
/**
28
-
*@param A: A positive integer which has N digits, A is a string.
29
-
*@param k: Remove k digits.
30
-
*@return: A string
31
-
*/
32
37
publicStringDeleteDigits(StringA, intk) {
33
38
if (A == null || A.length() == 0 || k == 0) {
34
39
returnA;
@@ -112,3 +117,5 @@ public static String DeleteDigits(String A, int k) {
Given a string S and a string T, count the number of distinct subsequences of T in S.
3
8
4
-
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not).
9
+
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none)
10
+
of the characters without disturbing the relative positions of the remaining characters.
11
+
(ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not).
5
12
6
13
Example
7
14
Given S = "rabbbit", T = "rabbit", return 3.
@@ -18,29 +25,29 @@ Do it in O(n2) time and O(n) memory.
18
25
Attempt2:
19
26
Use DP. Okay, first I had no idea how to start, but here is a reference: http://blog.csdn.net/abcbc/article/details/8978146
20
27
First of all, Map out the number of existance of T in S in a 2D map:
21
-
0 1 2 3 4 5 6 7
22
-
---------------
23
-
r a b b b i t
24
-
0| 1 1 1 1 1 1 1 1
25
-
1|r 0 1 1 1 1 1 1 1
26
-
2|a 0 0 1 1 1 1 1 1
27
-
3|b 0 0 0 1 2 3 3 3
28
-
4|b 0 0 0 0 1 3 3 3
29
-
5|i 0 0 0 0 0 0 3 3
30
-
6|t 0 0 0 0 0 0 0 3
28
+
0 1 2 3 4 5 6 7
29
+
---------------
30
+
r a b b b i t
31
+
0| 1 1 1 1 1 1 1 1
32
+
1|r 0 1 1 1 1 1 1 1
33
+
2|a 0 0 1 1 1 1 1 1
34
+
3|b 0 0 0 1 2 3 3 3
35
+
4|b 0 0 0 0 1 3 3 3
36
+
5|i 0 0 0 0 0 0 3 3
37
+
6|t 0 0 0 0 0 0 0 3
31
38
32
39
Use DP[T][S]. We realize:
33
40
1.DP[0][0] == 1; //Both null can be a match
34
41
2.DP[0][1 ~ S.length - 1] = 1;//First fow, when T=="", whatever S will have 1 subsequence: ""
35
42
3.DP[1 ~ T.length][0] = 0;// First column, when S=="", whatever T will not be subsequence of S == ""
36
43
4.When looking at each row and filling out the pixels, we realize when T exist in S[a~b], it will surely exist in S[a~b+1], taht is:
37
-
Step1: DP[i][j] is at least equal to DP[i][j - 1];//DP[i][j] is always based on DP[i][j-1], so DP[i][j] = DP[i][j+1] + something
38
-
Step2: So, what's that 'something' in step1? For example, look at T[3] == 'b' against S[0 ~ 3]:
39
-
S[0 ~ 3] has 1 'b' at S[3], and also, T[0~3] == S[0~3], that's a perfect match. SO DP[3][3] = 1
40
-
S[0 ~ 4] has 2 'b' at S[3] and S[4]. Now imagine we pick either S[3] or S[4] to genreate T[0~3] out of S[0~4]: we have 2 possibilities.D[3][4] = 2
41
-
Consider: D[i][j] means we picked S[j]; in our S[0 ~ 4] case, that means we picked S[4] but skipped S[3], though S[3] still counts towards another situation where we skipped S[4].
42
-
After all, we will count whatever that we skipped into our current DP[i][j], that is DP[i][j] += T[i - 1] == S[j - 1] ? DP[i - 1][j - 1] : 0;
43
-
Conclusion: while we for-looping through each row, if we find out S[j] and S[j - 1] both equals to T[i - 1], we want to make sure we count D[i - 1][j -1]'s previous records in!
44
+
Step1: DP[i][j] is at least equal to DP[i][j - 1];//DP[i][j] is always based on DP[i][j-1], so DP[i][j] = DP[i][j+1] + something
45
+
Step2: So, what's that 'something' in step1? For example, look at T[3] == 'b' against S[0 ~ 3]:
46
+
S[0 ~ 3] has 1 'b' at S[3], and also, T[0~3] == S[0~3], that's a perfect match. SO DP[3][3] = 1
47
+
S[0 ~ 4] has 2 'b' at S[3] and S[4]. Now imagine we pick either S[3] or S[4] to genreate T[0~3] out of S[0~4]: we have 2 possibilities.D[3][4] = 2
48
+
Consider: D[i][j] means we picked S[j]; in our S[0 ~ 4] case, that means we picked S[4] but skipped S[3], though S[3] still counts towards another situation where we skipped S[4].
49
+
After all, we will count whatever that we skipped into our current DP[i][j], that is DP[i][j] += T[i - 1] == S[j - 1] ? DP[i - 1][j - 1] : 0;
50
+
Conclusion: while we for-looping through each row, if we find out S[j] and S[j - 1] both equals to T[i - 1], we want to make sure we count D[i - 1][j -1]'s previous records in!
44
51
45
52
Note:
46
53
In double for loop, set i,j <= xxxx.length(), since we've increased the 2D array by 1 block on row and col.
@@ -53,23 +60,23 @@ public class Solution {
53
60
* @return: Count the number of distinct subsequences
@@ -107,4 +114,5 @@ public int numDistinct(String S, String T) {
107
114
However, time cost on this:
108
115
For example I have n missing chars from S.length == m. so I have (m + 1) places where i can insert the n chars. Then it's a mCn problem. This goes up to m!, too much. Not applicapable.
0 commit comments