Shawn's Blog

  • Home
  • Tags
  • Categories
  • Archives
  • Search
  • Table of Contents
  • Overview

Shawn

若有恒,何必三更眠五更起;最无益,莫过一日曝十日寒。
392 posts
13 categories
114 tags

LeetCode 330. Patching Array

Posted on 2020-12-29 Edited on 2024-11-24 In LeetCode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
int minPatches(vector<int>& nums, int n) {
int patches = 0;
long long x = 1;
int length = nums.size(), index = 0;
while (x <= n) {
if (index < length && nums[index] <= x) {
x += nums[index];
index++;
} else {
x <<= 1;
patches++;
}
}
return patches;
}
};
# Greedy
LeetCode 188. Best Time to Buy and Sell Stock IV
LeetCode 1046. Last Stone Weight
© 2024 Shawn
Powered by Hexo & NexT.Mist
0%