iT邦幫忙

2024 iThome 鐵人賽

DAY 22
0
自我挑戰組

Leetcode 解題之旅:逐日攻克系列 第 22

每日一LeetCode(22)

  • 分享至 

  • xImage
  •  

330. Patching Array

題目敘述:

Given a sorted integer array nums and an integer n, add/patch elements to the array such that any number in the range [1, n] inclusive can be formed by the sum of some elements in the array.

Return the minimum number of patches required.

class Solution {
public:
   int minPatches(vector<int>& nums, int n) {
       long long miss = 1;
       int result = 0;
       size_t i = 0;

       while (miss <= n) {
           if (i < nums.size() && nums[i] <= miss) {
               miss += nums[i];
               i++;
           } else {
               miss += miss;
               result++;
           }
       }

       return result;
   }
};

上一篇
每日一LeetCode(21)
下一篇
每日一LeetCode(23)
系列文
Leetcode 解題之旅:逐日攻克30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言