JAVA:
public final int maxNonOverlapping(int[] nums, int target) { int re = 0; int point = 0; while (point < nums.length) { int sum = 0; Set<Integer> preSums = new HashSet<Integer>(); preSums.add(0); while (point < nums.length) { sum += nums[point]; preSums.add(sum); if (preSums.contains(sum - target)) { re++; break; } else { point++; } } point++; } return re; }
JS:
var maxNonOverlapping = function (nums, target) { let re = 0; let point = 0; while (point < nums.length) { let sums = new Set(); sums.add(0); let sum = 0; while (point < nums.length) { sum += nums[point]; if (sums.has(sum - target)) { re++; break; } else { sums.add(sum); point++; } } point++; } return re; };
Leetcode 1546 和为目标值的最大数目不重叠非空子数组数目 贪心前缀和
原文:https://www.cnblogs.com/niuyourou/p/13562591.html