Backpacking Problem Variation via Greedy Approach: How Many Appl

  • 时间:2020-09-18 17:39:21
  • 分类:网络文摘
  • 阅读:77 次

You have some apples, where arr[i] is the weight of the i-th apple. You also have a basket that can carry up to 5000 units of weight. Return the maximum number of apples you can put in the basket.

Example 1:
Input: arr = [100,200,150,1000]
Output: 4
Explanation: All 4 apples can be carried by the basket since their sum of weights is 1450.

Example 2:
Input: arr = [900,950,800,1000,700,800]
Output: 5
Explanation: The sum of weights of the 6 apples exceeds 5000 so we choose any 5 of them.

Constraints:
1 <= arr.length <= 10^3
1 <= arr[i] <= 10^3

This is a simple variation of the back-packing problems. You are given the weight of each items, and you know the maximum capacity of the bag which is 5000 units. Then, you need to know the maximum items you can put.

Greedy Approach: by Sorting to Pick the Most Items

We can sort the items/apples by weights, in the ascending order. The time complexity via sorting is O(N.LogN). Then, we can follow the greedy strategy to pick the least weighted item at a time, until the total weights exceed the maximum.

The greedy approach works, because if you pick a heavier item, you can always pick a lighter one, which will not be a worse solution (you have more remaining capacity for extra items)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
    int maxNumberOfApples(vector<int>& arr) {
        sort(begin(arr), end(arr));
        int r = 0, curSum = 0;
        for (int i = 0; i < arr.size(); ++ i) {
            if (curSum + arr[i] <= 5000) {
                r ++;
                curSum += arr[i];
            } else {
                break;
            }
        }
        return r;
    }
};
class Solution {
public:
    int maxNumberOfApples(vector<int>& arr) {
        sort(begin(arr), end(arr));
        int r = 0, curSum = 0;
        for (int i = 0; i < arr.size(); ++ i) {
            if (curSum + arr[i] <= 5000) {
                r ++;
                curSum += arr[i];
            } else {
                break;
            }
        }
        return r;
    }
};

The above greedy solution runs at O(N.LogN) time overall. Another similar implementation in C++ saving up a variable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
    int maxNumberOfApples(vector<int>& arr) {
        sort(begin(arr), end(arr));
        for (int i = 0, res = 0; i < arr.size(); ++ i) {
            if (res + arr[i] <= 5000) {
                res += arr[i];
            } else {
                return i;
            }
        }
        return arr.size();
    }
};
class Solution {
public:
    int maxNumberOfApples(vector<int>& arr) {
        sort(begin(arr), end(arr));
        for (int i = 0, res = 0; i < arr.size(); ++ i) {
            if (res + arr[i] <= 5000) {
                res += arr[i];
            } else {
                return i;
            }
        }
        return arr.size();
    }
};

You may also like the following relevant article: Algorithms Series: 0/1 BackPack – Dynamic Programming and BackTracking

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
有一架天平和一个50克的砝码,如果要得到150克糖果  看似容易-六年级易错题集锦  从前往后数小明排在第7位  三年级上册第九单元思考题:学校举行乒乓球比赛  “先填空,再列综合算式”总出错怎么办  火车的钟声  谷歌seo的内容素材和文章构思从哪里获取?(下篇)  谷歌seo的内容素材和文章构思从哪里获取?(上篇)  seo专家告诉你,新网站怎么做网站优化  企业做Google SEO如何用内链优化来提高排名 
评论列表
添加评论