Created
April 23, 2020 05:55
-
-
Save munguial/26282719897570d738b1245e95beb50b to your computer and use it in GitHub Desktop.
Day 22 - Subarray Sum Equals K
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/531/week-4/3307/ | |
class Solution { | |
fun subarraySum(nums: IntArray, k: Int): Int { | |
val map = mutableMapOf<Int, Int>().withDefault { 0 } | |
map[0] = 1 | |
var currSum = 0 | |
var result = 0 | |
nums.forEach { | |
currSum += it | |
result += map.getValue(currSum - k) | |
map.put(currSum, map.getValue(currSum) + 1) | |
} | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment