<commit_before>package leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * https://leetcode.com/problems/maximum-number-of-non-overlapping-subarrays-with-sum-equals-target/
 */
public class Problem1546 {
    public int maxNonOverlapping(int[] nums, int target) {
        return maxNonOverlapping(nums, target, 0, nums[0], new HashMap<>());
    }

    private static int maxNonOverlapping(int[] nums, int target, int index, int sum,
                                         Map<String, Integer> memo) {
        if (index == nums.length) {
            return 0;
        }
        String key = index + "," + sum;
        if (memo.containsKey(key)) {
            return memo.get(key);
        }
        int max = 0;
        int nextVal = index + 1 < nums.length ? nums[index + 1] : 0;
        if (sum == target) {
            int a = maxNonOverlapping(nums, target, index + 1, 0, memo) + 1;
            int b = maxNonOverlapping(nums, target, index + 1, nextVal, memo) + 1;
            max = Math.max(max, Math.max(a, b));
        } else {
            int a = maxNonOverlapping(nums, target, index + 1, 0, memo);
            int b = maxNonOverlapping(nums, target, index + 1, sum + nextVal, memo);
            max = Math.max(max, Math.max(a, b));
        }
        memo.put(key, max);
        return max;
    }

    public static void main(String[] args) {
        Problem1546 prob = new Problem1546();
        System.out.println(prob.maxNonOverlapping(new int[]{1,1,1,1,1}, 2)); // 2
        System.out.println(prob.maxNonOverlapping(new int[]{-1,3,5,1,4,2,-9}, 6)); // 2
        System.out.println(prob.maxNonOverlapping(new int[]{-2,6,6,3,5,4,1,2,8}, 10)); // 3
        System.out.println(prob.maxNonOverlapping(new int[]{0,0,0}, 0)); // 3
        System.out.println(prob.maxNonOverlapping(new int[]{1,1,1,1,1,3,0,8,24,5,3,23,98,5,38,5,2,45,4,5,9,9,17,5,2,25,9,23}, 9)); // 4
        System.out.println(prob.maxNonOverlapping(new int[]{4,5,9,9,5,2,9}, 9)); // 4
        System.out.println(prob.maxNonOverlapping(new int[]{1,1,5,2,4,5,1,9,9,9}, 9)); // 5
    }
}<commit_msg>Solve problem 1546<commit_after>package leetcode;

import java.util.HashSet;
import java.util.Set;

/**
 * https://leetcode.com/problems/maximum-number-of-non-overlapping-subarrays-with-sum-equals-target/
 */
public class Problem1546 {
    public int maxNonOverlapping(int[] nums, int target) {
        int answer = 0;
        int prefixSum = 0;
        Set<Integer> set = new HashSet<>();
        set.add(prefixSum);
        for (int i = 0; i < nums.length; i++) {
            prefixSum += nums[i];
            if (set.contains(prefixSum - target)) {
                answer++;
                set.clear();
            }
            set.add(prefixSum);
        }
        return answer;
    }
}
{'fraction_non_alphanumeric': 0.16223021582733813, 'fraction_numerical': 0.046762589928057555, 'mean_word_length': 2.298932384341637, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1069362', 'n_tokens_mistral': 1033, 'n_tokens_neox': 971, 'n_words': 195}