0330.cpp (358B)
1 class Solution { 2 public: 3 int minPatches(const vector<int> &nums, const int n) const { 4 long long sum = 0; 5 int res = 0, i = 0; 6 7 while (sum < n) { 8 if (i < size(nums) && nums[i] <= sum + 1) 9 sum += nums[i++]; 10 else 11 sum += sum + 1, res++; 12 } 13 14 return res; 15 } 16 };