Skip to content

Commit 59ecd68

Browse files
committed
Added DP questions
1 parent 556d68a commit 59ecd68

12 files changed

+521
-0
lines changed

Design/IteratorForCombination1286.cpp

Whitespace-only changes.

Design/IteratorForCombination1286.java

Whitespace-only changes.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// There are n points on a road you are driving your taxi on. The n points on the road are labeled from 1 to n in the direction you are going, and you want to drive from point 1 to point n to make money by picking up passengers. You cannot change the direction of the taxi.
2+
3+
// The passengers are represented by a 0-indexed 2D integer array rides, where rides[i] = [starti, endi, tipi] denotes the ith passenger requesting a ride from point starti to point endi who is willing to give a tipi dollar tip.
4+
5+
// For each passenger i you pick up, you earn endi - starti + tipi dollars. You may only drive at most one passenger at a time.
6+
7+
// Given n and rides, return the maximum number of dollars you can earn by picking up the passengers optimally.
8+
9+
// Note: You may drop off a passenger and pick up a different passenger at the same point.
10+
11+
12+
13+
// Example 1:
14+
15+
// Input: n = 5, rides = [[2,5,4],[1,5,1]]
16+
// Output: 7
17+
// Explanation: We can pick up passenger 0 to earn 5 - 2 + 4 = 7 dollars.
18+
// Example 2:
19+
20+
// Input: n = 20, rides = [[1,6,1],[3,10,2],[10,12,3],[11,12,2],[12,15,2],[13,18,1]]
21+
// Output: 20
22+
// Explanation: We will pick up the following passengers:
23+
// - Drive passenger 1 from point 3 to point 10 for a profit of 10 - 3 + 2 = 9 dollars.
24+
// - Drive passenger 2 from point 10 to point 12 for a profit of 12 - 10 + 3 = 5 dollars.
25+
// - Drive passenger 5 from point 13 to point 18 for a profit of 18 - 13 + 1 = 6 dollars.
26+
// We earn 9 + 5 + 6 = 20 dollars in total.
27+
28+
29+
// Constraints:
30+
31+
// 1 <= n <= 105
32+
// 1 <= rides.length <= 3 * 104
33+
// rides[i].length == 3
34+
// 1 <= starti < endi <= n
35+
// 1 <= tipi <= 105
36+
37+
#define pii pair<int, int>
38+
39+
class Solution {
40+
public:
41+
long long maxTaxiEarnings(int n, vector<vector<int>>& rides) {
42+
vector<vector<pii>> earn(n);
43+
for(auto &ride : rides){
44+
int start = ride[0], end = ride[1], tip = ride[2];
45+
earn[start].push_back({end, end - start + tip});
46+
}
47+
vector<long long> dp(n+1);
48+
for(int i = n-1; i >= 1; i--){
49+
for(auto& [end, tip] : earn[i])
50+
dp[i] = max(dp[i], dp[end] + tip);
51+
dp[i] = max(dp[i], dp[i+1]);
52+
}
53+
return dp[1];
54+
}
55+
};
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// There are n points on a road you are driving your taxi on. The n points on the road are labeled from 1 to n in the direction you are going, and you want to drive from point 1 to point n to make money by picking up passengers. You cannot change the direction of the taxi.
2+
3+
// The passengers are represented by a 0-indexed 2D integer array rides, where rides[i] = [starti, endi, tipi] denotes the ith passenger requesting a ride from point starti to point endi who is willing to give a tipi dollar tip.
4+
5+
// For each passenger i you pick up, you earn endi - starti + tipi dollars. You may only drive at most one passenger at a time.
6+
7+
// Given n and rides, return the maximum number of dollars you can earn by picking up the passengers optimally.
8+
9+
// Note: You may drop off a passenger and pick up a different passenger at the same point.
10+
11+
12+
13+
// Example 1:
14+
15+
// Input: n = 5, rides = [[2,5,4],[1,5,1]]
16+
// Output: 7
17+
// Explanation: We can pick up passenger 0 to earn 5 - 2 + 4 = 7 dollars.
18+
// Example 2:
19+
20+
// Input: n = 20, rides = [[1,6,1],[3,10,2],[10,12,3],[11,12,2],[12,15,2],[13,18,1]]
21+
// Output: 20
22+
// Explanation: We will pick up the following passengers:
23+
// - Drive passenger 1 from point 3 to point 10 for a profit of 10 - 3 + 2 = 9 dollars.
24+
// - Drive passenger 2 from point 10 to point 12 for a profit of 12 - 10 + 3 = 5 dollars.
25+
// - Drive passenger 5 from point 13 to point 18 for a profit of 18 - 13 + 1 = 6 dollars.
26+
// We earn 9 + 5 + 6 = 20 dollars in total.
27+
28+
29+
// Constraints:
30+
31+
// 1 <= n <= 105
32+
// 1 <= rides.length <= 3 * 104
33+
// rides[i].length == 3
34+
// 1 <= starti < endi <= n
35+
// 1 <= tipi <= 105
36+
37+
class Solution {
38+
public long maxTaxiEarnings(int n, int[][] rides) {
39+
List<int[]>[] arr = new List[n+1];
40+
for(int i = 1; i <= n; i++)
41+
arr[i] = new ArrayList<>();
42+
for(int[] ride : rides)
43+
arr[ride[0]].add(new int[]{ride[1], ride[1] - ride[0] + ride[2]});
44+
long[] dp = new long[n+1];
45+
for(int i = n-1; i >= 1; i--){
46+
for(int[] t : arr[i])
47+
dp[i] = Math.max(dp[i], dp[t[0]] + t[1]);
48+
dp[i] = Math.max(dp[i], dp[i+1]);
49+
}
50+
return dp[1];
51+
}
52+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Given a set of distinct positive integers nums, return the largest subset answer such that every pair
2+
// (answer[i], answer[j]) of elements in this subset satisfies:
3+
// answer[i] % answer[j] == 0, or
4+
// answer[j] % answer[i] == 0
5+
// If there are multiple solutions, return any of them.
6+
7+
8+
// Example 1:
9+
// Input: nums = [1,2,3]
10+
// Output: [1,2]
11+
// Explanation: [1,3] is also accepted.
12+
// Example 2:
13+
// Input: nums = [1,2,4,8]
14+
// Output: [1,2,4,8]
15+
16+
// Constraints:
17+
// 1 <= nums.length <= 1000
18+
// 1 <= nums[i] <= 2 * 109
19+
// All the integers in nums are unique.
20+
21+
class Solution {
22+
public:
23+
vector<int> largestDivisibleSubset(vector<int>& nums) {
24+
int n = nums.size(), maxlen = 0, maxind = -1;
25+
if(n == 1) return nums;
26+
vector<int> dp(n), pre(n);
27+
sort(nums.begin(), nums.end());
28+
for(int i = 0; i < n; i++){
29+
dp[i] = 1; pre[i] = -1;
30+
for(int j = i-1; j >= 0; j--){
31+
if(nums[i] % nums[j] == 0){
32+
if(dp[i] < 1 + dp[j]){
33+
dp[i] = 1 + dp[j];
34+
pre[i] = j;
35+
}
36+
}
37+
}
38+
if(dp[i] > maxlen){
39+
maxlen = dp[i];
40+
maxind = i;
41+
}
42+
}
43+
vector<int> res;
44+
while(maxind != -1){
45+
res.push_back(nums[maxind]);
46+
maxind = pre[maxind];
47+
}
48+
return res;
49+
}
50+
};
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Given a set of distinct positive integers nums, return the largest subset answer such that every pair (answer[i], answer[j]) of elements in this subset satisfies:
2+
3+
// answer[i] % answer[j] == 0, or
4+
// answer[j] % answer[i] == 0
5+
// If there are multiple solutions, return any of them.
6+
7+
8+
9+
// Example 1:
10+
11+
// Input: nums = [1,2,3]
12+
// Output: [1,2]
13+
// Explanation: [1,3] is also accepted.
14+
// Example 2:
15+
16+
// Input: nums = [1,2,4,8]
17+
// Output: [1,2,4,8]
18+
19+
20+
// Constraints:
21+
22+
// 1 <= nums.length <= 1000
23+
// 1 <= nums[i] <= 2 * 109
24+
// All the integers in nums are unique.
25+
26+
class Solution {
27+
public List<Integer> largestDivisibleSubset(int[] nums) {
28+
int n = nums.length, maxlen = 0, maxind = -1;
29+
int[] dp = new int[n];
30+
int[] pre = new int[n];
31+
Arrays.sort(nums);
32+
for(int i = 0; i < n; i++){
33+
dp[i] = 1;
34+
pre[i] = -1;
35+
for(int j = i-1; j >= 0; j--){
36+
if(nums[i] % nums[j] == 0){
37+
if(dp[i] < 1 + dp[j]){
38+
dp[i] = 1 + dp[j];
39+
pre[i] = j;
40+
}
41+
}
42+
}
43+
if(maxlen < dp[i]){
44+
maxlen = dp[i];
45+
maxind = i;
46+
}
47+
}
48+
49+
List<Integer> res = new ArrayList<>();
50+
while(maxind != -1){
51+
res.add(nums[maxind]);
52+
maxind = pre[maxind];
53+
}
54+
55+
return res;
56+
}
57+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// The beauty of a string is the difference in frequencies between the most frequent and least frequent characters.
2+
3+
// For example, the beauty of "abaacc" is 3 - 1 = 2.
4+
// Given a string s, return the sum of beauty of all of its substrings.
5+
6+
7+
8+
// Example 1:
9+
10+
// Input: s = "aabcb"
11+
// Output: 5
12+
// Explanation: The substrings with non-zero beauty are ["aab","aabc","aabcb","abcb","bcb"], each with beauty equal to 1.
13+
// Example 2:
14+
15+
// Input: s = "aabcbaa"
16+
// Output: 17
17+
18+
19+
// Constraints:
20+
21+
// 1 <= s.length <= 500
22+
// s consists of only lowercase English letters.
23+
24+
class Solution {
25+
public:
26+
int beautySum(string s) {
27+
int res = 0;
28+
for (auto i = 0; i < s.size(); ++i) {
29+
int cnt[26] = {}, max_f = 0, min_f = 0;
30+
for (auto j = i; j < s.size(); ++j) {
31+
int idx = s[j] - 'a';
32+
max_f = max(max_f, ++cnt[idx]);
33+
if (min_f >= cnt[idx] - 1) {
34+
min_f = cnt[idx];
35+
for (int k = 0; k < 26; ++k)
36+
min_f = min(min_f, cnt[k] == 0 ? INT_MAX : cnt[k]);
37+
}
38+
res += max_f - min_f;
39+
}
40+
}
41+
return res;
42+
}
43+
};
44+
45+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// The beauty of a string is the difference in frequencies between the most frequent and least frequent characters.
2+
3+
// For example, the beauty of "abaacc" is 3 - 1 = 2.
4+
// Given a string s, return the sum of beauty of all of its substrings.
5+
6+
7+
8+
// Example 1:
9+
10+
// Input: s = "aabcb"
11+
// Output: 5
12+
// Explanation: The substrings with non-zero beauty are ["aab","aabc","aabcb","abcb","bcb"], each with beauty equal to 1.
13+
// Example 2:
14+
15+
// Input: s = "aabcbaa"
16+
// Output: 17
17+
18+
19+
// Constraints:
20+
21+
// 1 <= s.length <= 500
22+
// s consists of only lowercase English letters.
23+
24+
class Solution {
25+
public int beautySum(String s) {
26+
int n = s.length(), ans = 0;
27+
for(int i = 0; i < n; i++){
28+
int[] count = new int[26];
29+
int maxi = 0, mini = 0;
30+
for(int j = i; j < n; j++){
31+
int idx = s.charAt(j) - 'a';
32+
maxi = Math.max(maxi, ++count[idx]);
33+
if(mini >= count[idx] - 1){
34+
mini = count[idx];
35+
for(int k = 0; k < 26; k++)
36+
mini = Math.min(mini, count[k] == 0 ? Integer.MAX_VALUE : count[k]);
37+
}
38+
ans += maxi - mini;
39+
}
40+
}
41+
return ans;
42+
}
43+
}

Math/CountGoodNumbers1922.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// A digit string is good if the digits (0-indexed) at even indices are even and the digits at odd indices are prime (2, 3, 5, or 7).
2+
3+
// For example, "2582" is good because the digits (2 and 8) at even positions are even and the digits (5 and 2) at odd positions are prime. However, "3245" is not good because 3 is at an even index but is not even.
4+
// Given an integer n, return the total number of good digit strings of length n. Since the answer may be large, return it modulo 109 + 7.
5+
6+
// A digit string is a string consisting of digits 0 through 9 that may contain leading zeros.
7+
8+
9+
10+
// Example 1:
11+
12+
// Input: n = 1
13+
// Output: 5
14+
// Explanation: The good numbers of length 1 are "0", "2", "4", "6", "8".
15+
// Example 2:
16+
17+
// Input: n = 4
18+
// Output: 400
19+
// Example 3:
20+
21+
// Input: n = 50
22+
// Output: 564908303
23+
24+
25+
// Constraints:
26+
27+
// 1 <= n <= 1015
28+
29+
#define ll long long
30+
int p = 1e9 + 7;
31+
class Solution {
32+
public:
33+
ll power(long long x, long long y){
34+
long long res = 1;
35+
x %= p;
36+
if(x == 0) return 0;
37+
while(y > 0){
38+
if(y & 1) res = (res*x) % p;
39+
y >>= 1;
40+
x = (x*x) % p;
41+
}
42+
return res;
43+
}
44+
45+
int countGoodNumbers(long long n) {
46+
ll count4s = n/2;
47+
ll count5s = n - (n/2);
48+
ll ans = ((power(4, count4s) % p * power(5, count5s)) % p);
49+
return (int) ans;
50+
}
51+
};

0 commit comments

Comments
 (0)