[LeetCode] 330. Patching Array 数组补丁
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such that any number in range [1, n]
inclusive can be formed by the sum of some elements in the array. Return the minimum number of patches required.
Example 1:
nums = [1, 3]
, n = 6
Return 1
.
Combinations of nums are [1], [3], [1,3]
, which form possible sums of: 1, 3, 4
.
Now if we add/patch 2
to nums, the combinations are: [1], [2], [3], [1,3], [2,3], [1,2,3]
.
Possible sums are 1, 2, 3, 4, 5, 6
, which now covers the range [1, 6]
.
So we only need 1
patch.
Example 2:
nums = [1, 5, 10]
, n = 20
Return 2
.
The two patches can be [2, 4]
.
Example 3:
nums = [1, 2, 2]
, n = 5
Return 0
.
Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.
Java:
public int minPatches(int[] nums, int n) {
long miss = 1;
int count = 0;
int i = 0; while(miss <= n){
if(i<nums.length && nums[i] <= miss){
miss = miss + nums[i];
i++;
}else{
miss += miss;
count++;
}
} return count;
}
Python:
class Solution(object):
def minPatches(self, nums, n):
"""
:type nums: List[int]
:type n: int
:rtype: int
"""
patch, miss, i = 0, 1, 0
while miss <= n:
if i < len(nums) and nums[i] <= miss:
miss += nums[i]
i += 1
else:
miss += miss
patch += 1 return patch
C++:
class Solution {
public:
int minPatches(vector<int>& nums, int n) {
long miss = 1, res = 0, i = 0;
while (miss <= n) {
if (i < nums.size() && nums[i] <= miss) {
miss += nums[i++];
} else {
miss += miss;
++res;
}
}
return res;
}
};
C++:
class Solution {
public:
int minPatches(vector<int>& nums, int n) {
long miss = 1, k = nums.size(), i = 0;
while (miss <= n) {
if (i >= nums.size() || nums[i] > miss) {
nums.insert(nums.begin() + i, miss);
}
miss += nums[i++];
}
return nums.size() - k;
}
};
All LeetCode Questions List 题目汇总
[LeetCode] 330. Patching Array 数组补丁的更多相关文章
- [LeetCode] Shuffle an Array 数组洗牌
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- 330. Patching Array
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...
- 【LeetCode】1095. 山脉数组中查找目标值 Find in Mountain Array
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetco ...
- LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree
LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...
- 了解PHP中的Array数组和foreach
1. 了解数组 PHP 中的数组实际上是一个有序映射.映射是一种把 values 关联到 keys 的类型.详细的解释可参见:PHP.net中的Array数组 . 2.例子:一般的数组 这里,我 ...
- JavaScript的json和Array及Array数组的使用方法
1.关于json JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集.也可以称为数据集和数组类似,能够存数据! //Ar ...
- iOS -Swift 3.0 -Array(数组与可变数组相关属性及用法)
// // ViewController.swift // Swift-Array // // Created by luorende on 16/9/12. // Copyright © 2016年 ...
- 多动手试试,其实List类型的变量在页面上取到的值可以直接赋值给一个js的Array数组变量
多动手试试,其实List类型的变量在页面上取到的值可以直接赋值给一个js的Array数组变量,并且数组变量可以直接取到每一个元素var array1 = '<%=yearList =>'; ...
- c++中的array数组和vector数组
我觉得实验一下会记得比较牢,话不多直接上代码. 下面是array数组,感觉用的不多. //cpp 风格数组 array #include <iostream> #include <a ...
随机推荐
- 洛谷 P1443 马的遍历题解
题目链接:https://www.luogu.org/problem/P1443 题目描述 有一个n*m的棋盘(1<n,m<=400),在某个点上有一个马,要求你计算出马到达棋盘上任意一个 ...
- .Net Core控制台生成exe能独立运行
.Net Core控制台生成exe能独立运行,依赖文件都单独生成在一个publish文件夹里 方式一:强烈推荐,能独立运行,依赖DLL也会生成出来,支持无安装环境也能到处运行 按win+R输入cmd在 ...
- NodeJS 多版本管理(NVM)
前言 现在前端各种框架更新较快,对 Node 的依赖也不一样,Node 的过版本管理也很有必要. NVM(Node Version Manager),是一个 Node 的版本管理工具. 官方的 NVM ...
- idea拉取最新代码弹窗(Ctrl + T)
在此设置
- 《团队作业第三、第四周》五阿哥团队作业--Scrum 冲刺阶段--Day1--领航
<团队作业第三.第四周>五阿哥团队作业--Scrum 冲刺阶段--Day1--领航 各个成员在 Alpha 阶段认领的任务 在团队合作时任务也会动态分配,最终以实际为主,上述具有参考价值. ...
- python的整数相除
在python2中: 10/4=2 在python3中: 10/4=2.5 10//4=2
- freemarker使用shiro标签(spring boot)
freemarker使用shiro标签(spring boot) 2018年07月03日 14:20:37 niu_sayok 阅读数:348更多 个人分类: freeMarkerShiro 首先 ...
- inertia 服务端驱动的spa 开发框架
inertia 可以让我们开发server 驱动的单页面应用开发,从目前的github代码来看,代码量并不多,相关的文档也还比较少 introducing-inertia-js 这个连接值得看下 参考 ...
- koa koa-static 静态资源中间件
koa-static介绍 在网络请求中,请求往往分成两种类型,一种是静态资源,直接从服务器的文件存储中读取,一种是动态资源,一般需要先从数据库获取数据,然后经过一定的处理,最后返回给客户端. koa- ...
- 牛顿插值法(c++)
X Y 0.40 0.41075 0.55 0.57815 0.65 0.69675 0.80 0.88811 0.90 1.02652 1.05 1.25382 #include using nam ...