[Kotlin]O[n] This is not hard problem, just some calculation
Intuition\nNaive thought\n[1] covers range 1-> 1\n[1, 2] covers range 1-> 3\n[1, 2, 4] covers range 1-> 7\n[1, 2, 4, 8] covers range 1-> 15\n\n\nNaive thought\n
# Intuition\nNaive thought\n[1] covers range 1-> 1\n[1, 2] covers range 1-> 3\n[1, 2, 4] covers range 1-> 7\n[1, 2, 4, 8] covers range 1-> 15\n\n\nNaive thought\n[1] covers range 1-> 1\n[1, 2] covers range 1-> 3\n**[1, 2, 2]** covers range 1-> 5\n[1, 2, 2, 3] covers range 1-> 8\n\n# Code\n```\nclass Solution {\n    fun minPatches(nums: IntArray, n: Int): Int {\n            var total = 0L\n            var count = 0L\n            var i = 0\n            while (total < n){\n                val expectNext = total + 1\n                if (i < nums.size){\n                    if (nums[i] <= expectNext){\n                        total += nums[i]\n                        i++\n                    } else{\n                        count ++\n                        total += expectNext\n                    }\n                } else{\n                    count ++\n                    total += expectNext\n                }\n            }\n\n            return count.toInt()\n        }\n}\n```
['Kotlin']