From 9e1aced4efcd86e95b64d2b646981d95fa338f97 Mon Sep 17 00:00:00 2001 From: Manuel Pineda Date: Sat, 30 Jan 2021 12:10:18 +0100 Subject: [PATCH] [LeetCode] Add January 30. --- solved/LeetCode/Challenges/2020/January/30.kt | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 solved/LeetCode/Challenges/2020/January/30.kt diff --git a/solved/LeetCode/Challenges/2020/January/30.kt b/solved/LeetCode/Challenges/2020/January/30.kt new file mode 100644 index 00000000..933c938f --- /dev/null +++ b/solved/LeetCode/Challenges/2020/January/30.kt @@ -0,0 +1,30 @@ +class Solution { + fun minimumDeviation(nums: IntArray): Int { + val s = TreeSet() + for (i in nums) { + s.add(i) + } + + var best = s.last() - s.first() + + while (isOdd(s.first())) { + s.add(s.pollFirst() * 2) + best = Math.min(best, s.last() - s.first()) + } + + while (isEven(s.last())) { + s.add(s.pollLast() / 2) + best = Math.min(best, s.last() - s.first()) + } + + return best + } + + private fun isEven(x: Int): Boolean { + return (x % 2) == 0 + } + + private fun isOdd(x: Int): Boolean { + return (x % 2) == 1 + } +}