/ 卡路里_最大卡路里

/ 卡路里

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

/ 卡路里

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540547.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540547.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540547.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

[轉載] Python類中的私有變量和公有變量

參考鏈接&#xff1a; Python中的私有變量 我們這里就直奔主題&#xff0c;不做基礎鋪墊&#xff0c;默認你有一些Python類的基礎&#xff0c;大家在看這篇博客的時候&#xff0c;如果基礎知識忘了&#xff0c;可以去菜鳥教程 從一個簡單的類開始 class A(): #定義一…

OpenCV探索之路(二十五):制作簡易的圖像標注小工具

搞圖像深度學習的童鞋一定碰過圖像數據標注的東西&#xff0c;當我們訓練網絡時需要訓練集數據&#xff0c;但在網上又沒有找到自己想要的數據集&#xff0c;這時候就考慮自己制作自己的數據集了&#xff0c;這時就需要對圖像進行標注。圖像標注是件很枯燥又很費人力物力的一件…

固件的完整形式是什么?

FW&#xff1a;前進 (FW: Forward) FW is an abbreviation of "Forward". FW是“ Forward”的縮寫 。 It is an expression, which is commonly used in Gmail or messaging platform. It is also written as FWD or Fwd or Fw. It shows that the email has been s…

[轉載] python __slots__ 詳解(上篇)

參考鏈接&#xff1a; Python的__name __(特殊變量) python中的new-style class要求繼承Python中的一個內建類型&#xff0c; 一般繼承object&#xff0c;也可以繼承list或者dict等其他的內建類型。 在python新式類中&#xff0c;可以定義一個變量__slots__&#xff0c;它的作…

委托BegionInvoke和窗體BegionInvoke

委托BegionInvoke是指通過委托方法執行多線程任務&#xff0c;例如&#xff1a; //定義委托成員變量 delegate void dg_DeleAirport(); //指定委托函數 dg_DeleAirport dga AirportBLL.DeleteHistoryTransAirport; //通過BeginInvoke以異步線程方式執行委托函數&#xff0c;可…

圖論 弦_混亂的弦

圖論 弦Problem statement: 問題陳述&#xff1a; You are provided an input string S and the string "includehelp". You need to figure out all possible subsequences "includehelp" in the string S? Find out the number of ways in which the s…

[轉載] Python列表操作

參考鏈接&#xff1a; Python中的基本運算符 Python列表&#xff1a; 序列是Python中最基本的數據結構。序列中的每個元素都分配一個數字 - 它的位置&#xff0c;或索引&#xff0c;第一個索引是0&#xff0c;第二個索引是1&#xff0c;依此類推&#xff1b; Python有6個序列的…

「原創」從馬云、馬化騰、李彥宏的對話,看出三人智慧差在哪里?

在今年中國IT領袖峰會上&#xff0c;馬云、馬化騰、李彥宏第一次單獨合影&#xff0c;同框畫面可以說很難得了。BAT關心的走勢一直是同行們競相捕捉的熱點&#xff0c;所以三位大Boss在這次大會上關于人工智能的見解&#xff0c;也受到廣泛關注與多方解讀。馬云認為機器比人聰明…

python 注釋含注釋_Python注釋

python 注釋含注釋Python注釋 (Python comments) Comments in Python are used to improve the readability of the code. It is useful information given by the programmer in source code for a better understanding of code and logic that they have used to solve the …

C2的完整形式是什么?

C2&#xff1a;核心2 (C2: Core 2) C2 is an abbreviation of "Core 2" or "Intel Core 2". C2是“ Core 2”或“ Intel Core 2”的縮寫 。 It is a family of Intels processor which was launched on the 27th of July, 2006. It comprises a series of…

scala特性_Scala | 特性應用

scala特性特性應用 (Trait App) Scala uses a trait called "App" which is used to convert objects into feasible programs. This conversion is done using the DelayedInit and the objects are inheriting the trait named App will be using this function. T…

[轉載] Python3中的表達式運算符

參考鏈接&#xff1a; Python中的除法運算符 1&#xff1a;Python常用表達式運算符 yield 生成器函數send協議 lambda args:expression 創建匿名函數 x if y else z 三元選擇表達式(當y為真時&#xff0c;x才會被計算) x or y 邏輯或(僅但x為假時y才會被計算) x and …

字符串矩陣轉換成長字符串_字符串矩陣

字符串矩陣轉換成長字符串Description: 描述&#xff1a; In this article, we are going to see how backtracking can be used to solve following problems? 在本文中&#xff0c;我們將看到如何使用回溯來解決以下問題&#xff1f; Problem statement: 問題陳述&#xf…

pythonchallenge_level2

level2 地址&#xff1a;http://www.pythonchallenge.com/pc/def/ocr.html。 源碼&#xff1a;gitcode.aliyun.com:qianlizhixing12/PythonChallenge.git。 問題&#xff1a;找出頁面源碼一點提示注釋中的稀有字符。 #!/usr/bin/env python3 # -*- coding:UTF-8 -*-# Level 2im…

[轉載] python類運算符的重載

參考鏈接&#xff1a; Python中的運算符重載 alist input().split() blist input().split() n float(input()) class Vector: def __init__(self, x0, y0, z0): # 請在此編寫你的代碼(可刪除pass語句) self.X x self.Y y self.Z z # 代碼結束 def __add__(self, other):…

r語言 運算符_R語言運算符

r語言 運算符R語言中的運算符 (Operators in R Language) Generally speaking, an operator is a symbol that gives proper commands to the compiler regarding a specific action to be executed. The operators are used for carrying out the mathematical or logical cal…

[轉載] Python基礎之類型轉換與算術運算符

參考鏈接&#xff1a; Python中的運算符函數| 1 一、注釋 1.注釋&#xff1a;對程序進行標注和說明&#xff0c;增加程序的可讀性。程序運行的時候會自動忽略注釋。 2.單行注釋&#xff1a;使用#的形式。但是#的形式只能注釋一行&#xff0c;如果有多行&#xff0c;就不方便…

java awt 按鈕響應_Java AWT按鈕

java awt 按鈕響應The Button class is used to implement a GUI push button. It has a label and generates an event, whenever it is clicked. As mentioned in previous sections, it extends the Component class and implements the Accessible interface. Button類用于…