forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin_days_to_eat_n_oranges.cpp
147 lines (90 loc) · 2.98 KB
/
min_days_to_eat_n_oranges.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
There are n oranges in the kitchen and you decided to eat some of these oranges every day as follows:
Eat one orange.
If the number of remaining oranges (n) is divisible by 2 then you can eat n/2 oranges.
If the number of remaining oranges (n) is divisible by 3 then you can eat 2*(n/3) oranges.
You can only choose one of the actions per day.
Return the minimum number of days to eat n oranges.
Example 1:
Input: n = 10
Output: 4
Explanation: You have 10 oranges.
Day 1: Eat 1 orange, 10 - 1 = 9.
Day 2: Eat 6 oranges, 9 - 2*(9/3) = 9 - 6 = 3. (Since 9 is divisible by 3)
Day 3: Eat 2 oranges, 3 - 2*(3/3) = 3 - 2 = 1.
Day 4: Eat the last orange 1 - 1 = 0.
You need at least 4 days to eat the 10 oranges.
Example 2:
Input: n = 6
Output: 3
Explanation: You have 6 oranges.
Day 1: Eat 3 oranges, 6 - 6/2 = 6 - 3 = 3. (Since 6 is divisible by 2).
Day 2: Eat 2 oranges, 3 - 2*(3/3) = 3 - 2 = 1. (Since 3 is divisible by 3)
Day 3: Eat the last orange 1 - 1 = 0.
You need at least 3 days to eat the 6 oranges.
Example 3:
Input: n = 1
Output: 1
Example 4:
Input: n = 56
Output: 6
Constraints:
1 <= n <= 2*10^9
// Best
// Memoization
class Solution {
public:
unordered_map <int, int> dp;
int solve(int n) {
if (dp.find(n) != dp.end()) return dp[n];
int op1 = INT_MAX, op2 = INT_MAX, op3 = INT_MAX;
if (n % 2 == 0) op2 = 1 + solve(n / 2);
if (n % 3 == 0) op3 = 1 + solve(n / 3);
if (n % 2 != 0 || n % 3 != 0) op1 = 1 + solve(n - 1);
return dp[n] = min(op1, min(op2, op3));
}
int minDays(int n) {
dp[0] = 0;
dp[1] = 1;
return solve(n);
}
};
// Memoization but array takes a lot of space (Run time error)
class Solution {
public:
vector <int> dp;
int solve(int n) {
if (n == 0) return 0;
else if (n == 1) return 1;
else if (n == 2) return 2;
else if (n == 3) return 2;
if (dp[n] != -1) return dp[n];
int op1 = INT_MAX, op2 = INT_MAX, op3 = INT_MAX;
if (n % 2 == 0) op2 = 1 + solve(n / 2);
if (n % 3 == 0) op3 = 1 + solve(n / 3);
if (n % 2 != 0 || n % 3 != 0) op1 = 1 + solve(n - 1);
return dp[n] = min(op1, min(op2, op3));
}
int minDays(int n) {
dp.resize(n + 1, -1);
return solve(n);
}
};
// DP (run time error dude to large space taken by array)
class Solution {
public:
int minDays(int n) {
if (n == 1 || n == 2) return n;
if (n == 3) return 2;
vector <int> dp(n + 1);
dp[1] = 1;
dp[2] = 2;
dp[3] = 2;
for (int i = 4; i <= n; i++) {
int mini = dp[i - 1];
if (i % 2 == 0) mini = min(mini, dp[i / 2]);
if (i % 3 == 0) mini = min(mini, dp[i / 3]);
dp[i] = mini + 1;
}
return dp[n];
}
};