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.

二. 题目分析

题目的大意是,给定一个数组nums和一个数n,求添加最少的数使得区间[1, n]中的每个数都可以由数组nums中元素累加组成。

由于数组已经排序,因此基本的思路是,定义一个整形变量currSum,假设数组nums目前可以累加的数的范围为[1, currSum),如果此时向数组中添加一个元素kk <= currSum)可以将累加的数的范围扩充为[1, currSum + k),而题目中要求向数组nums添加元素的次数最少,因此当且仅当k = currSum时,累加数的范围可以取到上限[1, 2 * currSum)。在遍历数组nums时,有以下两种操作:

  1. 当数组中有小于等于currSum的元素nums[index]时,则利用数组中的元素,同时currSum += nums[index]
  2. 若没有,则添加新元素currSum入数组,此时范围扩展至最大,同时令currSum <<= 1

三. 示例代码

#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
    int minPatches(vector<int>& nums, int n) {
        int result = 0, index = 0;
        // currSum标记了当前数组nums可累加到的最大范围[1, currSum)
        for (int currSum = 1; currSum <= n; )
        {
            // 数组内元素小于currSum时,可累加的sum上限增加为
            // currSum + nums[index],然后数组索引值加1
            if (index < nums.size() && nums[index] <= currSum)
                currSum += nums[index++];
            else
            {
                currSum <<= 1; // 添入元素后,可累加的sum范围加倍
                ++result;
            }
        }
        return result;
    }
};

四. 小结

这种方法并不容易马上想到,对于这一类型的问题,特别需要在纸面上多列举例子,慢慢从中找出规律。

时间: 2025-01-12 06:17:34

leetcode笔记:Patching Array的相关文章

[LeetCode][JavaScript]Patching Array

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 re

leetcode 330. Patching Array

传送门 330. Patching Array My Submissions QuestionEditorial Solution Total Accepted: 8597 Total Submissions: 29516 Difficulty: Medium Given a sorted positive integer array nums and an integer n, add/patch elements to the array such that any number in ra

【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 require

[LeetCode]Patching Array

题目: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

leetcode笔记

leetcode 笔记 Linked List 2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a

[leetcode笔记] Longest Consecutive Sequence

随机挑选一题试试手气.   问题描述: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your al

[LeetCode] Merge Sorted Array [22]

题目 Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B ar

[leetcode笔记] Remove Duplicates from Sorted List II

问题描述: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2-&

330. Patching Array

/* * 330. Patching Array * 2016-7-9 by Mingyang * hehe */ public int minPatches(int[] nums, int n) { if (n <= 0) return 0; nums = nums == null ? new int[0] : nums; int current_ind = 0, ret = 0; long boundary_val = 1, sum = 0; while (boundary_val <=

[leetcode]Convert Sorted Array to Binary Search Tree @ Python

原题地址:http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题意:将一个排序好的数组转换为一颗二叉查找树,这颗二叉查找树要求是平衡的. 解题思路:由于要求二叉查找树是平衡的.所以我们可以选在数组的中间那个数当树根root,然后这个数左边的数组为左子树,右边的数组为右子树,分别递归产生左右子树就可以了. 代码: # Definition for a binary tree node # class