From f0dd82e39d398c2203ba708d39656d220072b49c Mon Sep 17 00:00:00 2001 From: Manuel Pineda Date: Mon, 18 Jan 2021 14:04:52 +0100 Subject: [PATCH] [LeetCode] Add January 18. --- solved/LeetCode/Challenges/2020/January/18.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 solved/LeetCode/Challenges/2020/January/18.kt diff --git a/solved/LeetCode/Challenges/2020/January/18.kt b/solved/LeetCode/Challenges/2020/January/18.kt new file mode 100644 index 00000000..c0ca9630 --- /dev/null +++ b/solved/LeetCode/Challenges/2020/January/18.kt @@ -0,0 +1,16 @@ +class Solution { + fun maxOperations(nums: IntArray, k: Int): Int { + val freq = mutableMapOf() + var ans = 0 + for (i in nums) { + val target = k - i + if ((freq[target] ?: 0) > 0) { + freq.put(target, freq[target]!! -1) + ans++ + } else { + freq[i] = (freq[i] ?: 0) + 1 + } + } + return ans + } +}