/ 卡路里_最大卡路里

/ 卡路里

Problem statement:

问题陈述:

Shivang is very foodie but he has a diet plan. He has an array of elements indicating the calorie of food he can consume on that day. In his diet plan, he can’t eat on for three consecutive days. But since Shivang is very foodie, so he decided to take as much as calorie while remembering the fact that he cannot eat on three consecutive days. Help him in finding the number of calories he could take.

Shivang是位美食家,但他有饮食计划。 他具有一系列元素,这些元素指示他当天可以吃的食物的热量。 在他的饮食计划中,他连续三天不能进食。 但是由于Shivang是一位非常美食的人,因此他决定摄取尽可能多的卡路里,同时记住自己连续三天不能进食的事实。 帮助他找到可以摄取的卡路里数量。

Input:

输入:

n i.e number of days. In the next line is n space-separated values of ai indicating the intake of calorie per day.

n即天数。 在下一行中, i的 n个以空格分隔的值表示每天的卡路里摄入量。

Output:

输出:

Print the maximum amount of calorie, Shivang can take in a new line.

打印最大卡路里,Shivang可以换行。

Constraints:

限制条件:

1<=n<=100000
1<=ai<=100000

Example:

例:

Input:
2
n=5
Array input, calorie for each day:
3 2 1 10 3

Output:
18

Explanation:
Shivang will eat on 1st, 2nd, 4th and 5th day 
So, total he intakes 18 which is maximum

Input:
8
3 2 3 2 3 5 1 3 

Output:
17

Explanation:
Shivang will eat on 1st, 3rd, 5th , 6th and 8th 
day which totals (3+3+3+5+3) = 17

Solution Approach:

解决方法:

Let say,

说,

f(n) = maximum calorie can be taken till nth day under the constraint

No, let's think of the case,

不,让我们考虑一下情况,

  1. If Shivang takes food on ith day, he need to skip on (i-1)th day and sum with f(i-2)

    如果Shivang在 i 一天需要吃的,他需要跳过(I-1) 天,和与F(I-2)

  2. Tale food on ith day, (i-1)th day and skip on (i-2)th day, sum up with f(i-3)

    i上故事食品天, 第(i-1) 天,并跳过对第(i-2) 天,总结和f(I-3)

  3. Skip ith day, so take f(i-1)

    跳过 i 天,所以采取F(I-1)

So,

所以,

f(i)=maximum(f(i-1),f(i-2)+arr[i],arr[i]+arr[i-1]+f(i-3) i>=3

For base case,
f(0)=arr[0]
f(1)=arr[0]+arr[1]
f(2)=maximum(arr[0]+arr[1],arr[1]+arr[2],arr[2]+arr[0])

Converting to Dynamic programming

转换为动态编程

Now, we need to convert the above recursion into dynamic programming to avoid the overlapping sub-problem re-computation

现在,我们需要将上述递归转换为动态编程,以避免重叠的子问题重新计算

1)  Declare int DP[n];
2)  Fill with the base case
        DP[0]=arr[0];
        DP[1]=arr[0]+arr[1];
        DP[2]=std::max(arr[1]+arr[2],std::max(arr[0]+arr[2],DP[1]));
3)  Compute the other values
        for i=3 to n-1
            DP[i]=maximum(arr[i]+arr[i-1]+DP[i-3],arr[i]+DP[i-2],DP[i-1]);
        end for
4)  Return the result, DP[n-1];

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;

int maximumCalorie(vector<int> arr, int n)
{

    int DP[n];
    // base case
    DP[0] = arr[0];
    DP[1] = arr[0] + arr[1];
    DP[2] = std::max(arr[1] + arr[2], std::max(arr[0] + arr[2], DP[1]));

    for (int i = 3; i < n; i++) {
        DP[i] = std::max(arr[i] + arr[i - 1] + DP[i - 3], std::max(arr[i] + DP[i - 2], DP[i - 1]));
    }

    return DP[n - 1];
}

int main()
{
    int t, n, item;
    
    cout << "Enter number of days\n";
    cin >> n;
    cout << "Enter calories\n";
    vector<int> a;
    
    for (int j = 0; j < n; j++) {
        scanf("%d", &item);
        a.push_back(item);
    }
    
    cout << "Maximum calorie sudhir can take: " << maximumCalorie(a, n) << endl;

    return 0;
}

Output:

输出:

Enter number of days:
8 
Enter calories:
3 2 3 2 3 5 1 3 
Maximum calorie sudhir can take: 17


翻译自: https://www.includehelp.com/icp/maximum-calorie.aspx

/ 卡路里

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值