[recommend for beginners]clean C++ implementation with detailed explanation
we run the code on an example to illustrate the ideas:\n  \n        [1, miss)   : the right miss is open field\n\n         nums=[1,5,10]      n=20\n\n        in
we run the code on an example to illustrate the ideas:\n  \n        [1, miss)   : the right miss is open field\n\n         nums=[1,5,10]      n=20\n\n        initialize state : miss=1   i=0   size=3\n\nmiss=1 :   i=0\n\n        if find [number<=1] in nums {  i++  }  else add 1 to nums   update    miss+=1         [1,2)\n        found 1,  i++, so added 1 to the nums \n  \nmiss=2 :  i=1\n\n        if find [number<=2] in nums  { i++ }   else add 2 to nums    update  miss+=2;        [1,4)\n        not found ,  so add 2 to the nums\n\nmiss=4 : i=1\n\n        if find [number<=4] in nums  { i++ }   else add 4 to nums    update  miss+=2;        [1,8)\n        not found ,  so add 4 to the nums\n       \nmiss=8: i=1\n\n        if find [number<=8] in nums  { i++ }   else add 8 to nums    update  miss+=2;        [1,13)\n        found 5 , so add 5 to the nums  \n\nmiss=13:i=2\n\n        if find [number<=13] in nums  { i++ }   else add 13 to nums    update  miss+=2;        [1,23)\n        found 10 , so add 10 to the nums \n\nmiss=23 \n\n        covers [1,20], done!\n\nreturn added=2\n\n\nCode:\n\n\n    class Solution {\n    public:\n        int minPatches(vector<int>& nums, int n) {\n            long miss=1, added=0, i=0;\n            while(miss<=n){\n                /*** update the miss to the (miss+nums[i]) **/\n                if(i<nums.size() && nums[i]<=miss){\n                    miss+=nums[i++];\n                }else{\n                /*** update the miss to the miss+miss **/\n                    miss+=miss;\n                    added++;\n                }\n            }\n            return added;\n        }\n    };
[]