dp 扔雞蛋_使用動態編程(DP)的雞蛋掉落問題

dp 扔雞蛋

Problem statement: You are given N floor and K eggs. You have to minimize the number of times you have to drop the eggs to find the critical floor where critical floor means the floor beyond which eggs start to break. Assumptions of the problem:

問題陳述:給您N樓和K蛋。 您必須盡量減少丟雞蛋的次數,以找到關鍵的地板,其中關鍵的地板意味著雞蛋開始破損的地板 。 問題的假設:

  1. If egg breaks at ith floor then it also breaks at all greater floors.

    如果雞蛋在第1層破裂,那么它在所有較大的層也破裂。

  2. If egg does not break at ith floor then it does not break at all lower floors.

    如果雞蛋沒有在那么它不會在所有低層打破。

  3. Unbroken egg can be used again.

    完整的雞蛋可以再次使用。

Note: You have to find minimum trials required to find the critical floor not the critical floor.

注意:您必須找到找到關鍵樓層而不是關鍵樓層所需的最少試驗。

Constraints:

限制條件:

    1 <= N <= 1000
1 <= K <= 100

Example 1:

范例1:

    Input:
4
2
Output:
Number of trials when number of eggs is 2 and number of floors is 4: 3

Example 2:

范例2:

    Input:
36
2
Output:
Number of trials when number of eggs is 2 and number of floors is 36: 8

Explanation of the problem:

問題說明:

For the Input 1,

對于輸入1,

Case 1. Drop at first floor:

案例1.降在一樓:

  1. Egg does not break:

    雞蛋不碎:

    If egg does not break then we have three floors left and two eggs. We can either choose

    如果雞蛋沒有破裂,那么我們剩下三層樓和兩個雞蛋。 我們可以選擇

    2nd or 3rd floor and proceed similarly but we can easily see we have to do atleast 3 trials.

    2 3 ,同樣繼續進行,但我們可以很容易地看到,我們所要做的ATLEAST 3項試驗。

  2. Egg breaks:

    雞蛋休息時間:

    If egg breaks then we found the critical floor in only 1 trial.

    如果雞蛋破裂,那么我們僅在1次試驗中發現了臨界水平。

In case 1, the worst possibility is that egg does not break so trials will be 3 for case 1.

在第一種情況下,最可能的情況是雞蛋不會破裂,因此第一種情況下的試驗為3。

Case 2. Drop at second floor:

情況2。掉到二樓:

  1. Egg breaks:

    雞蛋休息時間:

    We are left with one egg and we have to check only 1 floor so number of trials 2.

    我們只剩下一個雞蛋,我們只需要檢查1層,那么試驗次數為2。

  2. Egg does not break:

    雞蛋不碎:

    We still have two eggs and two floors to check we have to check one by one on these floors so trials needed are 3.

    我們仍然有兩個雞蛋和兩個樓層要檢查,我們必須在這些樓層上一個接一個地檢查,因此需要進行3次試驗。

So for case 2 together the worst possibility is 3.

因此,對于情況2,最壞的可能性是3。

In the end we have to find the minimum of case 1, case 2, case 3, case 4.

最后,我們必須找到情況1,情況2,情況3,情況4的最小值。

Note: Dropping from 3rd and 4th floor is same as dropping from 2nd and 1st floor respectively only difference is that subcases A and B just gets swapped.

注意:從跌落3 4層是與從 2 1層分別唯一的區別滴是子情況AB只是獲取交換。

Algorithm:

算法:

  1. Create a 2D dp matrix where jth cell of ith row represents the minimum number of trials needed to check i floors using j eggs.

    創建一個二維dp矩陣,其中 i行的第j 單元格代表使用j個雞蛋檢查i個樓層所需的最小試驗次數。

  2. If there are 0 eggs then there are 0 trials so initializing all cell corresponding to this situation.

    如果有0個卵,則有0個試驗,因此初始化對應于這種情況的所有單元。

  3. If there are 1 egg then we have to drop egg at every floor so the number of trials is number of floors.

    如果有1個雞蛋,那么我們必須在每個樓層放雞蛋,這樣試驗的次數就是樓層數。

  4. If there are 0 floors so there are 0 trials and if there is one floor there is only one trial.

    如果樓層數為0,則試驗為0,而樓層數為1,則只有一次試驗。

  5. Start filling the dp matrix from egg = 2 && floors = 2.

    egg = 2 && floors = 2開始填充dp矩陣。

  6. We have to choose a floor x between the 1 – floor and then find cases when egg breaks at xth floor and egg do not break at xth floors (A and B subparts of cases).

    我們有選擇的1樓之間的地板X -地板,然后找到情況下,當在X 和雞蛋的蛋破裂不會在 X層(例AB子部分)打破。

  7. We have to take the maximum of these A and B subparts as we are taking worst case possible. Also, we will also add one to it as one trial is also needed to check xth floor.

    由于我們正在考慮最壞的情況,因此我們必須充分利用這些A和B子部分。 此外,我們還將添加一個到它也需要一個試驗檢查X

  8. Taking the minimum of answers of all such x's and storing at my particular number of floors and number of eggs.

    以所有這些x的最小答案,并以我的特定樓層數和雞蛋數存儲。

The time complexity of the above code is O(N * K * K).

上面代碼的時間復雜度為O(N * K * K)。

使用動態編程(DP)解決雞蛋掉落問題的C ++程序 (C++ program for Egg dropping problem using Dynamic Programming (DP))

#include <iostream>
#include <limits.h>
using namespace std;
int eggDrop(int floors, int eggs){
int dp[floors + 1][eggs + 1] = {0};
// intializing with some cases
// case 1. when there are 0 floors
for(int egg = 0;egg<=eggs;egg++){
dp[0][egg] = 0;
}
// case 2. when there are only 1 floor so there 
// are only 1 way only check the first floor
for(int egg = 0;egg<=eggs;egg++){
dp[1][egg] = 1;
}
// case 3. when there are 0 eggs
for(int floor = 0;floor <=floors;floor++){
dp[floor][0] = 0;
}
// case 4. when there are 1 egg then we have to 
// check every floor so our answer would be number of 
// floors
for(int floor = 0;floor <= floors;floor++){
dp[floor][1] = floor;
}
// we will start filling dp matrix from 
// floor = 2 && egg = 2
for(int egg = 2;egg<=eggs;egg++){
for(int floor = 2;floor<=floors;floor++){
// choosing an ith floor between 1 - floor
int mini = INT_MAX;
for(int i = 1;i<=floor;i++){
// dp[i - 1][egg-1] means to find the answer when 
// the egg is broken at ith floor
// dp[floor - i][egg] means to find the answer 
// when the egg is not broken at ith floor
int ans = max(dp[i-1][egg-1], dp[floor - i][egg]);
// by trying to check the floor i have used one trial
ans++;
// taking the minimum of all possible floor 
// possible between 1 - floor
mini = min(mini, ans); 
}
// storing the answer
dp[floor][egg] = mini;
}
}
return dp[floors][eggs];
}
// driver program
int main() {
int floors, eggs;
cin >> floors >> eggs;
cout<<"number of trials when number of eggs is " << eggs;
cout<<" and number of floors is " << floors;
cout<<": "  << eggDrop(floors,eggs);
return 0;
}

Output

輸出量

4
2
number of trials when number of eggs is 2 and number of floors is 4: 3

翻譯自: https://www.includehelp.com/algorithms/egg-dropping-problem-using-dynamic-programming.aspx

dp 扔雞蛋

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

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

相關文章

MOVSX和MOVZX

MOVSX 先符號擴展,再傳送 格式&#xff1a; MOVSX 操作數A &#xff0c;操作數B //操作數B的空間小于A比如說我們使用命令&#xff1a; movsx eax&#xff0c;bxbx是16位&#xff0c;eax是32位&#xff0c;傳值過程&#xff1a; 先用bx的符號位把eax高16填滿&#xff0c;b…

統計學習以及支持向量機的國內外基本比較重要的書

1、支持向量機導論&#xff0c;此書乃是SVM方面的經典著作&#xff0c; 該書的作者也是近年來SVM、kernel methods學術圈內的活躍學者&#xff0c;對于這些領域均有過重要的貢獻。這本書從“線性機器、核方法、統計學習理論、凸優化”四個方面揭示了SVM的內在機理 --利用核…

Java——集合(TreeSet)

package com.wsq.set; //這里進行調用Person()方法&#xff0c;要進行導包 import java.util.TreeSet; import com.wsq.bean.Person; public class Demo3_TreeSet { /*** TreeSet集合是用來對元素進行排序的&#xff0c;同樣它也可以保證元素的唯一* 當compareTo()方法返…

setmonth_日期setMonth()方法以及JavaScript中的示例

setmonthJavaScript日期setMonth()方法 (JavaScript Date setMonth() method) setMonth() method is a Date class method, it is used to set the month to the Date object with a valid month value (between 0 to 11. 0 for January, 1 for February and so on). setMonth(…

LEA與XCHG

LEA 格式&#xff1a; LEA 通用寄存器 內存地址功能&#xff1a;取地址命令 將內存地址賦值給寄存器 lea eax,dword ptr ds:[ecx0x16]dword 雙字 就是四個字節ptr pointer縮寫 即指針ds 數據段版寄存器[]里的數據是一個地址值&#xff0c;這個地址指向一個雙字型數據 將dwo…

分域名優化的時候要考慮備選IP的問題

我們在需要下載很多內容的時候&#xff0c;很容易想到做分域名的并發下載&#xff0c;給原來的服務器多分幾個域名&#xff0c;因為分不同的域名可能可以在瀏覽器中分到更多的下載進程&#xff0c;提高下載速度。 但是在做網絡應用的時候&#xff0c;我們的一個域名下面有的時候…

面試題-ASP 與 ASP.Net的區別?

比較簡潔的回答&#xff1a; 1.開發語言不同&#xff0c;ASP局限于用腳本語言來開發&#xff0c;而ASP.Net可以使用C#,VB.C等來開發。 2.運行機制不同&#xff0c;ASP是解釋運行的&#xff0c;執行效率較低。ASP.Net是編譯性的編程框架。 3.開發方式不同&#xff0c;ASP里前臺H…

Java——集合(輸入5個學生的信息按總分高低排序)

題目要求&#xff1a; 鍵盤錄入5個學生信息&#xff08;姓名&#xff0c;語文成績&#xff0c;數學成績&#xff0c;英語成績&#xff09;&#xff0c;按照總分從高到低輸出到控制臺 分析&#xff1a; 1&#xff0c;定義一個學生類 * 成員變量&#xff1a;姓名&#xff0c;…

日期setHours()方法以及JavaScript中的示例

JavaScript Date setHours()方法 (JavaScript Date setHours() method) setHours() method is a Date class method, it is used to set the hour to the Date object with a valid hour value (between 00 to 23). setHours()方法是Date類方法&#xff0c;用于將小時設置為具有…

Google SSL zz

// Google SSL// Modified from SSL Certificates Pro//z 2011-12-29 8:59 AM is2120csdn : reader,calendar// UserScript// name Google SSL// namespace http://raychow.info/// version 2.1.2// description 強制 Google 使用安全連接。//// include htt…

阿諾德.施瓦辛格 訓練方法

阿諾德.施瓦辛格 訓練方法七次“奧林匹亞先生”獲得者、著名影星阿諾德.施瓦辛格&#xff0c;是廣大健美愛好者崇拜的偶像。即使在今天&#xff0c;他那無與倫比的二頭肌和胸肌仍為人們津津樂道。本文是他通過親身體會&#xff0c;講述了怎樣鍛煉才能增長肌肉的觀點和方法&…

ADC和SBB命令

ADC 帶進位加法指令 用法&#xff1a; adc 操作數1&#xff0c;操作數2相當于&#xff1a; 操作數1操作數2進位標志CF->操作數1現在的eax是0&#xff0c;C1&#xff0c;用adc指令直接會是0x6 SBB 帶進位減法指令 用法&#xff1a; sbb 操作數1&#xff0c;操作數2相當…

Java——集合(輸入一串字符串,統計字符串中每個字符出現的次數)

A&#xff1a;案例演示 需求&#xff1a;輸入一串字符串&#xff0c;統計字符串中每個字符出現的次數** 分析&#xff1a;1&#xff0c;定義一個需要被統計字符的字符串2&#xff0c;將字符串轉化為字符數組&#xff0c;才能拿到每一個字符3&#xff0c;定義雙列集合存儲字符串…

entry數組_數組entry()方法以及JavaScript中的示例

entry數組JavaScript entry()方法 (JavaScript entries() method) entries() method is used to create an iterator object of an array to access the keys (index) and values. entry()方法用于創建數組的迭代器對象&#xff0c;以訪問鍵(索引)和值。 Syntax: 句法&#xf…

mul和div指令(8位,16位,32位)

MUL 無符號乘法指令&#xff0c;默認操作數與eax相乘&#xff08;這里只說32位&#xff0c;其他與下面的div類似&#xff09; 格式&#xff1a; mul 操作數 //操作數只有一個操作數與eax相乘&#xff0c;結果共有16位&#xff08;這里的16位是16進制數&#xff09;&#xff…

2011年年終盤點

不知不覺又到了年底&#xff0c;我坐在電腦前&#xff0c;竭力的回憶&#xff0c;卻發現回憶中一片空白&#xff0c;能記起也就那么幾件事。 一、在暑假做了一個多月的電子商務 在這個過程中&#xff0c;我了解到電子商務的基本流程&#xff0c;以及一些銷售技巧&#xff0c;還…

ASP.NET Application,Session,Cookie和ViewState等對象用法和區別

ASP.NET Application,Session,Cookie和ViewState等對象用法和區別 在ASP.NET中&#xff0c;有很多種保存信息的內置對象&#xff0c;如:Application,Session,Cookie,ViewState和Cache等。下面分別介紹它們的用法和區別。 方法 信息量大小 作用域和保存時間 應用…

Java——集合(HashMap與Hashtable的區別)

* HashMap和Hashtable的區別* 共同點&#xff1a;* 底層都是哈希算法&#xff0c;都是雙列集合* 區別&#xff1a;* 1&#xff0c;HashMap是線程不安全的&#xff0c;效率高* Hashtable是線程安全的&#xff0c;效率低 * 2&#xff0c;HashMap可以存儲null鍵和null值* Has…

判斷字符串是否構成回文_構成字符串回文的最小刪除數

判斷字符串是否構成回文Problem statement: 問題陳述&#xff1a; Given string str find the minimum number of deletions such that the resultant string is a palindrome. 給定的字符串str找到最小的刪除數&#xff0c;以使最終的字符串成為回文。 Input:Each input con…

imul和idiv指令

imul 有符號乘法指令&#xff0c;分單操作數&#xff0c;雙操作數和但操作數 單操作數&#xff1a;此形式與mul指令使用完全相同&#xff0c;操作數乘以al、ax、或eax寄存器中的值&#xff0c;乘積分別存儲到ax、dx&#xff1a;ax或edx&#xff1a;eax中 執行指令&#xff1a…