Skip to content

Instantly share code, notes, and snippets.

@munguial
Created April 23, 2020 05:55
Show Gist options
  • Save munguial/26282719897570d738b1245e95beb50b to your computer and use it in GitHub Desktop.
Save munguial/26282719897570d738b1245e95beb50b to your computer and use it in GitHub Desktop.
Day 22 - Subarray Sum Equals K
// 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