💯✅🔥Easy Java ,Python3 ,C++ Solution|| 0 ms ||≧◠‿◠≦✌
Intuition\n Describe your first thoughts on how to solve this problem. \nThe idea behind this solution is to keep track of the largest number that can be repres
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe idea behind this solution is to keep track of the largest number that can be represented using the given numbers and the patches added so far. We start with the first number in the nums array, and if it is greater than the current largest number plus 1, we add a patch to cover the gap. Otherwise, we add the current number to the largest number that can be represented\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Initialize patch to keep track of the largest number that can be represented, count to keep track of the number of patches added, and index to keep track of the current index in the nums array.\n- While patch is less than n, do the following:\n- If index is less than the length of nums and patch + 1 is less than or equal to nums[index], add nums[index] to patch and increment index.\n- Otherwise, add patch + 1 to patch and increment count.\nReturn count.\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\n\nclass Solution {\n    public int minPatches(int[] nums, int n) {\n        long patch = 0;\n        int count = 0;\n        int index = 0;\n        while (patch < n) {\n            if (index < nums.length && patch + 1 >= nums[index]) {\n                patch += nums[index];\n                index++;\n            } else {\n                patch += (patch + 1);\n                count++;\n            }\n        }\n\n        return count;\n    }\n}\n\n```\n```python3 []\nclass Solution:\n    def minPatches(self, nums: List[int], n: int) -> int:\n        patch = 0\n        count = 0\n        index = 0\n        while patch < n:\n            if index < len(nums) and patch + 1 >= nums[index]:\n                patch += nums[index]\n                index += 1\n            else:\n                patch += (patch + 1)\n                count += 1\n        return count\n```\n```C++ []\nclass Solution {\npublic:\n    int minPatches(std::vector<int>& nums, int n) {\n    long long patch = 0;\n    int count = 0;\n    int index = 0;\n    while (patch < n) {\n        if (index < nums.size() && patch + 1 >= nums[index]) {\n            patch += nums[index];\n            index++;\n        } else {\n            patch += (patch + 1);\n            count++;\n        }\n    }\n\n    return count;\n}\n};\n```\n\n\n\n![upvote.png](https://assets.leetcode.com/users/images/c9a1652d-4fab-4a78-a2e6-4fb7f106a5fb_1718499527.1176505.png)\n\n\n
['Array', 'C++', 'Java', 'Python3']