Skip to content

Commit ff7d03b

Browse files
committed
260423
1 parent 1330b34 commit ff7d03b

File tree

4 files changed

+262
-1
lines changed

4 files changed

+262
-1
lines changed

src/Main.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import leetcode.editor.en.Q1014.BestSightseeingPair;
22
import leetcode.editor.en.Q1051.HeightChecker;
3+
import leetcode.editor.en.Q115.DistinctSubsequences;
4+
import leetcode.editor.en.Q1218.LongestArithmeticSubsequenceOfGivenDifference;
35
import leetcode.editor.en.Q122.BestTimeToBuyAndSellStockIi;
46
import leetcode.editor.en.Q1416.RestoreTheArray;
57
import leetcode.editor.en.Q152.MaximumProductSubarray;
@@ -35,7 +37,7 @@
3537
public class Main {
3638
public static void main(String[] args) throws IOException {
3739

38-
System.out.println(new MaximumLengthOfPairChain().findLongestChain(toIntMatrix("[[-6,9],[1,6],[8,10],[-1,4],[-6,-2],[-9,8],[-5,3],[0,3]]")));
40+
System.out.println(new LongestArithmeticSubsequenceOfGivenDifference().longestSubsequence(toIntArray("[7,-2,8,10,6,18,9,-8,-5,18,13,-6,-17,-1,-6,-9,9,9]\n"), 1));
3941

4042
}
4143

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package leetcode.editor.en.Q115;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//Given two strings s and t, return the number of distinct subsequences of s
8+
//which equals t.
9+
//
10+
// The test cases are generated so that the answer fits on a 32-bit signed
11+
//integer.
12+
//
13+
//
14+
// Example 1:
15+
//
16+
//
17+
//Input: s = "rabbbit", t = "rabbit"
18+
//Output: 3
19+
//Explanation:
20+
//As shown below, there are 3 ways you can generate "rabbit" from s.
21+
//rabbbit
22+
//rabbbit
23+
//rabbbit
24+
//
25+
//
26+
// Example 2:
27+
//
28+
//
29+
//Input: s = "babgbag", t = "bag"
30+
//Output: 5
31+
//Explanation:
32+
//As shown below, there are 5 ways you can generate "bag" from s.
33+
//babgbag
34+
//babgbag
35+
//babgbag
36+
//babgbag
37+
//babgbag
38+
//
39+
//
40+
// Constraints:
41+
//
42+
//
43+
// 1 <= s.length, t.length <= 1000
44+
// s and t consist of English letters.
45+
//
46+
//
47+
// 👍 5254 👎 200
48+
49+
50+
//leetcode submit region begin(Prohibit modification and deletion)
51+
class Solution {
52+
Integer[][] cache;
53+
54+
public int numDistinct(String s, String t) {
55+
int ans = 0;
56+
cache = new Integer[s.length() + 1][t.length() + 1];
57+
ans += numDistinct(0, 0, s, t);
58+
59+
return ans;
60+
}
61+
62+
public int numDistinct(int i, int j, String s, String t) {
63+
int ans = 0;
64+
if (j == t.length()) {
65+
return ++ans;
66+
}
67+
if (i == s.length()) {
68+
return ans;
69+
}
70+
if (cache[i][j] != null) {
71+
return cache[i][j];
72+
}
73+
char c1 = s.charAt(i);
74+
char c2 = t.charAt(j);
75+
76+
if (c1 == c2) {
77+
ans += numDistinct(i + 1, j + 1, s, t);
78+
}
79+
ans += numDistinct(i + 1, j, s, t);
80+
81+
cache[i][j] = ans;
82+
return ans;
83+
84+
85+
}
86+
}
87+
//leetcode submit region end(Prohibit modification and deletion)
88+
89+
90+
public class DistinctSubsequences extends Solution {
91+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package leetcode.editor.en.Q1218;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//Given an integer array arr and an integer difference, return the length of
8+
//the longest subsequence in arr which is an arithmetic sequence such that the
9+
//difference between adjacent elements in the subsequence equals difference.
10+
//
11+
// A subsequence is a sequence that can be derived from arr by deleting some or
12+
//no elements without changing the order of the remaining elements.
13+
//
14+
//
15+
// Example 1:
16+
//
17+
//
18+
//Input: arr = [1,2,3,4], difference = 1
19+
//Output: 4
20+
//Explanation: The longest arithmetic subsequence is [1,2,3,4].
21+
//
22+
// Example 2:
23+
//
24+
//
25+
//Input: arr = [1,3,5,7], difference = 1
26+
//Output: 1
27+
//Explanation: The longest arithmetic subsequence is any single element.
28+
//
29+
//
30+
// Example 3:
31+
//
32+
//
33+
//Input: arr = [1,5,7,8,5,3,4,2,1], difference = -2
34+
//Output: 4
35+
//Explanation: The longest arithmetic subsequence is [7,5,3,1].
36+
//
37+
//
38+
//
39+
// Constraints:
40+
//
41+
//
42+
// 1 <= arr.length <= 10⁵
43+
// -10⁴ <= arr[i], difference <= 10⁴
44+
//
45+
//
46+
// 👍 1403 👎 48
47+
48+
49+
//leetcode submit region begin(Prohibit modification and deletion)
50+
class Solution {
51+
Integer[] cache;
52+
HashMap<Integer, TreeSet<Integer>> numsMap = new HashMap<>();
53+
54+
public int longestSubsequence(int[] arr, int difference) {
55+
int ans = 0;
56+
cache = new Integer[arr.length];
57+
for (int i = 0; i < arr.length; i++) {
58+
numsMap.computeIfAbsent(arr[i], (v) -> new TreeSet<>()).add(i);
59+
}
60+
61+
for (int i = 0; i < arr.length; i++) {
62+
ans = Math.max(longestSubsequence(i, arr, difference), ans);
63+
}
64+
return ans;
65+
}
66+
67+
68+
public int longestSubsequence(int i, int[] arr, int difference) {
69+
int ans = 1;
70+
if (i == arr.length) {
71+
return ans;
72+
}
73+
if (cache[i] != null) {
74+
return cache[i];
75+
}
76+
77+
int num = arr[i];
78+
79+
int target = difference + num;
80+
81+
if (numsMap.containsKey(target)) {
82+
TreeSet<Integer> idxes = numsMap.get(target);
83+
Integer higher = idxes.higher(i);
84+
if (higher != null) {
85+
SortedSet<Integer> next = idxes.subSet(higher, true, idxes.last(), true);
86+
for (int j : next) {
87+
if (j > i) {
88+
ans = Math.max(longestSubsequence(j, arr, difference) + 1, ans);
89+
}
90+
}
91+
}
92+
}
93+
94+
cache[i] = ans;
95+
return ans;
96+
97+
98+
}
99+
}
100+
//leetcode submit region end(Prohibit modification and deletion)
101+
102+
103+
public class LongestArithmeticSubsequenceOfGivenDifference extends Solution {
104+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package leetcode.editor.en.Q258;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//Given an integer num, repeatedly add all its digits until the result has only
8+
//one digit, and return it.
9+
//
10+
//
11+
// Example 1:
12+
//
13+
//
14+
//Input: num = 38
15+
//Output: 2
16+
//Explanation: The process is
17+
//38 --> 3 + 8 --> 11
18+
//11 --> 1 + 1 --> 2
19+
//Since 2 has only one digit, return it.
20+
//
21+
//
22+
// Example 2:
23+
//
24+
//
25+
//Input: num = 0
26+
//Output: 0
27+
//
28+
//
29+
//
30+
// Constraints:
31+
//
32+
//
33+
// 0 <= num <= 2³¹ - 1
34+
//
35+
//
36+
//
37+
// Follow up: Could you do it without any loop/recursion in O(1) runtime?
38+
//
39+
// 👍 3619 👎 1816
40+
41+
42+
//leetcode submit region begin(Prohibit modification and deletion)
43+
class Solution {
44+
public int addDigits(int num) {
45+
return addDigits(String.valueOf(num));
46+
}
47+
48+
49+
public int addDigits(String s) {
50+
if (s.length() == 1) {
51+
return Integer.parseInt(s);
52+
}
53+
int ans = 0;
54+
for (int i = 0; i < s.length(); i++) {
55+
ans += Character.getNumericValue(s.charAt(i));
56+
}
57+
return addDigits(String.valueOf(ans));
58+
}
59+
}
60+
//leetcode submit region end(Prohibit modification and deletion)
61+
62+
63+
public class AddDigits extends Solution {
64+
}

0 commit comments

Comments
 (0)