Skip to content

Commit 2dbbdd0

Browse files
authored
Create main.cpp
1 parent f30027f commit 2dbbdd0

File tree

1 file changed

+19
-0
lines changed
  • 25 - Greedy Algorithm Problems/04 - Check if it is possible to survive on Island

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
// Function to find the minimum number of days to prepare food
4+
int minimumDays(int S, int N, int M) {
5+
// Total food required for S days
6+
int totalFood = S * M;
7+
8+
// Check if it is impossible to prepare food
9+
// Case 1: If the total food required exceeds the food production capacity (7 * M > 6 * N and S > 6)
10+
// Case 2: If the food required per day (M) exceeds the total food production capacity per day (N)
11+
if ((7 * M > 6 * N && S > 6) || M > N) return -1;
12+
13+
// If the total food is divisible by N, return the exact number of days
14+
if (totalFood % N == 0) return totalFood / N;
15+
16+
// If not, return the number of days needed, rounded up (totalFood / N + 1)
17+
else return totalFood / N + 1;
18+
}
19+
};

0 commit comments

Comments
 (0)