Sharing my 8ms C++ solution


  • 1
    T
    class Solution {
    public:
        int minPatches(vector<int>& nums, int n) {
            int i = 0, N = nums.size();
            int result=0;
            long long int element=1;
            while(element<=n)
            {
                if(i>=N || nums[i]>element)
                {
                    result++;
                    element = element*2;
                }
                else
                {
                    element = element+nums[i];
                    i++;
                }
            }
            
            return result;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.