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
classSolution {
publicintnumberOfGoodSubarraySplits(int[] nums) {
intn = nums.length;
finalintMOD = (int)(1e9) + 7;
longans = 1;
inti = 0;
while (i < n && nums[i] == 0) {
i++;
}
if (i == n) {
return0;
}
intl = i + 1, r = i + 1;
while (r < n) {
if (nums[r] == 1) {
ans *= (r - l + 1);
ans %= MOD;
l = r + 1;
}
r++;
}
return (int)ans;
}
}
The text was updated successfully, but these errors were encountered:
2750. Ways to Split Array Into Good Subarrays
2750. Ways to Split Array Into Good Subarrays
类似问题:
930. Binary Subarrays With Sum
1、DP
这道题我又是思维定式直接想到用动态规划来做。
虽然这不是正解,但是可以硬着头皮做下去的。难点在于如何优化,那最多耗时是O(nlgn),所以最后用二分解决了问题。
2、 Greedy / DP / Math
然而,其实不难发现一点(也能从上面窥探出来),其实我们可以只需要记住0...1的个数,然后相乘就行了。
也可以这么想,对于这个问题,可以分成多个子问题,也就是要求解[0,1,...,n - 1]上的分割数组的方式,分割成0...1模式(可能0个0)的多个子字符串,然后解决这些子问题。
The text was updated successfully, but these errors were encountered: