Return the maximum number of non-emptynon-overlapping subarrays such that the sum of values in each subarray is equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Use a hashmap index to record the last index when a given prefix sum occurs. dp[i] := max # of non-overlapping subarrays of nums[0~i], nums[i] is not required to be included. dp[i+1] = max(dp[i], // skip nums[i] dp[index[sum – target] + 1] + 1) // use nums[i] to form a new subarray ans = dp[n]
Time complexity: O(n) Space complexity: O(n)
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution{
public:
intmaxNonOverlapping(vector<int>&nums,inttarget){
constintn=nums.size();
vector<int>dp(n+1,0);// ans at nums[i];
unordered_map<int,int>index;// {prefix sum -> last_index}
You are given two sorted arrays of distinct integers nums1 and nums2.
A validpath is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Since numbers are strictly increasing, we can always traverse the smaller one using two pointers. Traversing ([2,4,5,8,10], [4,6,8,10]) will be like [2, 4/4, 5, 6, 8, 10/10] It two nodes have the same value, we have two choices and pick the larger one, then both move nodes one step forward. Otherwise, the smaller node moves one step forward. dp1[i] := max path sum ends with nums1[i-1] dp2[j] := max path sum ends with nums2[j-1] if nums[i -1] == nums[j – 1]: dp1[i] = dp2[j] = max(dp[i-1], dp[j-1]) + nums[i -1] i += 1, j += 1 else if nums[i – 1] < nums[j – 1]: dp[i] = dp[i-1] + nums[i -1] i += 1 else if nums[j – 1] < nums[i – 1]: dp[j] = dp[j-1] + nums[j -1] j += 1 return max(dp1[-1], dp2[-1])
Time complexity: O(n) Space complexity: O(n) -> O(1)
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
classSolution{
public:
intmaxSum(vector<int>&nums1,vector<int>&nums2){
constexprintkMod=1e9+7;
constintn1=nums1.size();
constintn2=nums2.size();
vector<long>dp1(n1+1);// max path sum ends with nums1[i-1]
vector<long>dp2(n2+1);// max path sum ends with nums2[j-1]
Run-length encoding is a string compression method that works by replacing consecutive identical characters (repeated 2 or more times) with the concatenation of the character and the number marking the count of the characters (length of the run). For example, to compress the string "aabccc" we replace "aa" by "a2" and replace "ccc" by "c3". Thus the compressed string becomes "a2bc3".
Notice that in this problem, we are not adding '1' after single characters.
Given a string s and an integer k. You need to delete at mostk characters from s such that the run-length encoded version of s has minimum length.
Find the minimum length of the run-length encoded version of s after deleting at most k characters.
Example 1:
Input: s = "aaabcccd", k = 2
Output: 4
Explanation: Compressing s without deleting anything will give us "a3bc3d" of length 6. Deleting any of the characters 'a' or 'c' would at most decrease the length of the compressed string to 5, for instance delete 2 'a' then we will have s = "abcccd" which compressed is abc3d. Therefore, the optimal way is to delete 'b' and 'd', then the compressed version of s will be "a3c3" of length 4.
Example 2:
Input: s = "aabbaa", k = 2
Output: 2
Explanation: If we delete both 'b' characters, the resulting compressed string would be "a4" of length 2.
Example 3:
Input: s = "aaaaaaaaaaa", k = 0
Output: 3
Explanation: Since k is zero, we cannot delete anything. The compressed string is "a11" of length 3.
Given an array of integers arr. Return the number of sub-arrays with odd sum.
As the answer may grow large, the answer must be computed modulo 10^9 + 7.
Example 1:
Input: arr = [1,3,5]
Output: 4
Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]]
All sub-arrays sum are [1,4,9,3,8,5].
Odd sums are [1,9,3,5] so the answer is 4.
Example 2:
Input: arr = [2,4,6]
Output: 0
Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]]
All sub-arrays sum are [2,6,12,4,10,6].
All sub-arrays have even sum and the answer is 0.
Example 3:
Input: arr = [1,2,3,4,5,6,7]
Output: 16
Example 4:
Input: arr = [100,100,99,99]
Output: 4
Example 5:
Input: arr = [7]
Output: 1
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 100
Solution: DP
We would like to know how many subarrays end with arr[i] have odd or even sums.
dp[i][0] := # end with arr[i] has even sum dp[i][1] := # end with arr[i] has even sum
if arr[i] is even:
dp[i][0]=dp[i-1][0] + 1, dp[i][1]=dp[i-1][1]
else:
dp[i][1]=dp[i-1][0], dp[i][0]=dp[i-1][0] + 1
ans = sum(dp[i][1])
Time complexity: O(n) Space complexity: O(n) -> O(1)
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
classSolution{
public:
intnumOfSubarrays(vector<int>&arr){
constintn=arr.size();
constexprintkMod=1e9+7;
// dp[i][0] # of subarrays ends with a[i-1] have even sums.
// dp[i][1] # of subarrays ends with a[i-1] have odd sums.