Skip to content

Commit

Permalink
[LeetCode] Add January 30.
Browse files Browse the repository at this point in the history
  • Loading branch information
pin3da authored Jan 30, 2021
1 parent bf93f67 commit 9e1aced
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions solved/LeetCode/Challenges/2020/January/30.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
fun minimumDeviation(nums: IntArray): Int {
val s = TreeSet<Int>()
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
}
}

0 comments on commit 9e1aced

Please sign in to comment.