C# O(n)


  • 0
    H
    public int MaxSubArrayLen(int[] nums, int k) {
            Dictionary<int, int> map = new Dictionary<int, int>();
            int s = 0, max = 0;
            map[0] = -1;
            for(int i = 0; i < nums.Length; i++) {
                s += nums[i];
                if (!map.ContainsKey(s)) map[s] = i;
                if (map.ContainsKey(s - k)) {
                    max = Math.Max(max, i - map[s - k]);
                }
            }
            
            return max;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.