From 1455df9e3fe62a97871c73bb3e3d29c65ffcd3b0 Mon Sep 17 00:00:00 2001 From: Sumit Saha <47948885+SUMIT-RIJU@users.noreply.github.com> Date: Sat, 22 Oct 2022 00:43:10 +0530 Subject: [PATCH] Create EggDrop(DP approach).cpp --- C++ algos/EggDrop(DP approach).cpp | 49 ++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 C++ algos/EggDrop(DP approach).cpp diff --git a/C++ algos/EggDrop(DP approach).cpp b/C++ algos/EggDrop(DP approach).cpp new file mode 100644 index 0000000..1ca36e9 --- /dev/null +++ b/C++ algos/EggDrop(DP approach).cpp @@ -0,0 +1,49 @@ +int eggDrop(int n, int k) { + + int eggFloor[n + 1][k + 1]; + + int res; + + int i, j, x; + + for (i = 1; i <= n; i++) { + + eggFloor[i][1] = 1; + + eggFloor[i][0] = 0; + + } + + for (j = 1; j <= k; j++) + + eggFloor[1][j] = j; + + + + for (i = 2; i <= n; i++) { + + for (j = 2; j <= k; j++) { + + eggFloor[i][j] = INT_MAX; + + for (x = 1; x <= j; x++) { + + res = 1 + max( + + eggFloor[i - 1][x - 1], + + eggFloor[i][j - x]); + + if (res < eggFloor[i][j]) + + eggFloor[i][j] = res; + + } + + } + + } + + return eggFloor[n][k]; + +}