File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed
25 - Greedy Algorithm Problems/04 - Check if it is possible to survive on Island Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change 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+ };
You can’t perform that action at this time.
0 commit comments