diff --git a/kt/maximum-sum-of-distinct-subarrays-with-length-k.kt b/kt/maximum-sum-of-distinct-subarrays-with-length-k.kt new file mode 100644 index 0000000..b037a03 --- /dev/null +++ b/kt/maximum-sum-of-distinct-subarrays-with-length-k.kt @@ -0,0 +1,33 @@ +class Solution { + fun maximumSubarraySum( + nums: IntArray, + k: Int, + ): Long { + var maximum = 0L + + var (l, r) = 0 to 0 + var runningSum = 0 + + val seen = mutableMapOf() + while (r < nums.size) { + val x = nums[r] + + val lastSeen = seen.getOrElse(x) { -1 } + while (l <= lastSeen || r - l + 1 > k) { + runningSum -= nums[l] + l += 1 + } + + seen.put(x, r) + runningSum += nums[r] + + if (end - begin + 1 == k) { + maximum = listOf(maximum, runningSum).max() + } + + r += 1 + } + + return maximum + } +}