LeetCode 330. Patching Array

Before solving this problem. Better to have a look on the this one first.

EPI: Compute the smallest non-constructible change.:

Suppose you have some coins, there are some amount of changes that you may not be able to make with these coins. eg. you can't create a change amount greater than the sum of coin's denominations. For example, if your coins have denomination 1, 1, 1, 1, 1, 5, 10, 25, then the smallest value of change which can't be make is 21.

Write a program which takes an array of positive integers and returns the smallest number which is not to the sum of a subset of elements of the array.

HINT: Small example should lead you to a general hypothesis.

HMM....

First thought was to use brute force. However, it is quite straightforward that brute force time complexity will be very high: take subsets and compute the sum. 

The hint suggests to have a look at the small examples:

Suppose we have array [1, 2], we can make numbers: 1, 2, 3. If we have array [1, 3], we can make number 1, 3, 4.... Thus, the smallest number marks the lower bound the the sum of the array makes the upper bound. 

Continuing with a larger one: [1, 2, 4], it can produce: 1, 2, 3, 4, 5, 6, 7, and <1, 2, 5> can produce 1, 2, 3, 5, 6, 7, 8. Generalizing, suppose a collection of numbers can produce every value up to and including V, but not V+1, now consider the effect of adding a new element u to the collection. if u <= V+ 1, we can still produce every value up to and including V + u and we can't produce V+u+1. On the other hand, if u > V + 1, then, even by adding u to the collection, we still can't produce V + 1.

#include <iostream>
#include <vector>
using namespace std;

int smallestNonconstructable(vector<int>& nums) {
  int unreacheable = 0;
  for(int i = 0; i < nums.size(); ++i) {
    if(nums[i] > unreacheable+1) break;
    else unreacheable += nums[i];
  }
  return unreacheable + 1;
}

int main(void) {
  vector<int> nums{1, 2, 4};
  cout << smallestNonconstructable(nums) << endl;
}


Now, time to have a look at the LeetCode 330 Problem.

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.

int patchingArray(vector<int>& nums, int target) {
  sort(nums.begin(), nums.end());
  long long reachable = 0;
  int i = 0, patch = 0;
  while(reachable < target) {
    if(i >= (int) nums.size() || reachable + 1 < nums[i]) {
      patch += 1;
      reachable = reachable + (reachable + 1);
    } else {
      reachable = reachable + nums[i];
      i++;
    }
  }
  return patch;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值