Google 2016面试题
来源:互联网 发布:mysql 分组 字段累加 编辑:程序博客网 时间:2024/05/18 00:49
1、数组补丁
- 给出一个从小到大排好序的整数数组nums和一个整数n,在数组中添加若干个补丁(元素)使得[1,n]的区间内的所有数都可以表示成nums中若干个数的和。返回最少需要添加的补丁个数。
Example 1:
nums = [1, 3], n = 6
返回1,表示至少需要添加1个数{2},才可以表示1到6之间所有数。
Example 2:
nums = [1, 5, 10], n = 20
返回2,表示至少需要添加两个数{2,4},才可以表示1到20之间所有数。
public class Solution { public int minPatches(int[] nums, int n) { long sum = 0; int ans = 0; int index = 0; if (nums.length > 0 && nums[0] == 1) { sum = 1; index = 1; } while (sum < n) { while (index < nums.length && nums[index] <= sum) { sum += nums[index]; index++; } if (sum < n) { if (index < nums.length && nums[index] == sum + 1) index++; else { ans++; } sum = 2 * sum + 1; } } return ans; }}
0 0
- Google 2016面试题
- Google 2016 面试题 | 数组补丁
- Google 2016 面试题1 | 数组补丁
- Google 面试题讨论
- 小小Google面试题
- google面试题
- 面试题2-- Google
- google面试题
- google面试题
- Google 的面试题
- Google面试题
- 一道Google面试题
- google 面试题 分享
- google面试题
- GOOGLE面试题
- Google面试题
- Google面试题
- google面试题
- Easy 1题 Two Sum
- mysql悲观锁总结和实践
- HDU 5868 Different Circle Permutation(dp+polya+矩阵快速幂+欧拉函数)
- IO技术(五)流的使用规律和键盘读入数据
- 《Java编程思想第四版》笔记---16章 数组
- Google 2016面试题
- 虚拟机NAT方式解决主机PING不同虚拟机的问题
- MongoDB在CentOS下的装配、启动和配置
- 国内三大云数据库测试对比
- 使用DELIMITER创建存储过程的用法
- 计算机内存储整数的总结
- HDU1541-Stars
- 求解二叉树第K层节点数目
- bzoj 2728: [HNOI2012]与非