【Leetcode】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.

思路:

考虑   1,2 可以表示出1~3

1,2,4 可以表示出1~7

1,2,4,8,..k 可以表示出1~2*k-1

这是因为{1,2,4,8}这些数 转化为二进制{1,10,100,1000},把它们组合相加就相当于能表示出1~1111 之间的数

所以对于给定的n,想要表示出1~n之间所有的数,集合中只要有1、2、4~(n+1)/2就可以了。

那么如果集合如果给定了不是2的幂次方的数是不是就没用了呢? 当然也是有用的。把这个数d添加到集合里 当前可以表示的范围也就扩大为cur+d了

要注意此时必须cur>=d否则当前集合不一定能表示出1~cur+d。如果cur<d,此时需要添加数让范围扩大,具体可以添加,比当前最高位还多一位的数就可以了,比如当前最高能表示出的数是7(111),我们只需要添加一个8(1000),就能让表示范围扩大一倍。

算法

public int minPatches(int[] nums, int n) {
    long cur = 1;
    int count = 1,i=0;
    while(cur<=n){
        if(i<nums.length&&nums[i]<=cur){
            cur+=nums[i];
            i++;
        }else{
            count++;
            cur= cur*2;
        }
    }
    return count;
}  
时间: 2024-08-14 01:45:35

【Leetcode】Patching Array的相关文章

【LeetCode】Rotate Array

Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note: Try to come up as many solutions as you can, there are at least 3 different ways to

【LeetCode】9.Array and String — Longest Common Prefix 最长共同前缀

Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". Example 1: Input: ["flower","flow","flight"] Output: "fl" Exa

【LeetCode】11.Array and String —Array Partition I 数组分区

Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible. Example 1: Input: [1,4,3,2] Output: 4 Explan

【LeetCode】10.Array and String —Reverse String 字符数组逆置

Write a function that reverses a string. The input string is given as an array of characters char[]. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. You may assume all the

【LeetCode】4.Array and String — Diagonal Traverse 对角线遍历

Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image. Example: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,4,7,5,3,6,8,9] Explanation: Note: The total

【LeetCode】贪心 greedy(共38题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [44]Wildcard Matching [45]Jump Game II (2018年11月28日,算法群衍生题) 题目背景和 55 一样的,问我能到达最后一个index的话,最少走几步. 题解: [55]Jump Game (2018年11月27日,算法群) 给了一个数组nums,nums[i] = k 代表站在第 i 个位置的情况下, 我最多能往前走 k 个单

【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

【Leetcode】Median of Two Sorted Array II

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 非常经典的一个问题,如果直接查找中位数,即偶数约定向下取整,奇数就是严格的中位数,这个题目直接可以比较每个数组的mid来计算得到. 但是这个问题的需求更改为如果为偶数,则中

【leetcode】905. Sort Array By Parity

题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数组头部,否则追加到尾部. 代码如下: class Solution(object): def sortArrayByParity(self, A): """ :type A: List[int] :rtype: List[int] """ res =