Advertisement
LikeRampage

C++ leetcode 330.Patching Array Chatgpt Unlimited

Apr 22nd, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.44 KB | None | 0 0
  1. #include <vector>
  2.  
  3. class Solution {
  4. public:
  5. int minPatches(std::vector<int>& nums, int n) {
  6. long miss = 1;
  7. int patches = 0;
  8. int i = 0;
  9.  
  10. while (miss <= n) {
  11. if (i < nums.size() && nums[i] <= miss) {
  12. miss += nums[i++];
  13. } else {
  14. miss += miss;
  15. patches++;
  16. }
  17. }
  18.  
  19. return patches;
  20. }
  21. };
Tags: C++
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement