1553. Minimum Number of Days to Eat N Oranges
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.
Given the integer n, return the minimum number of days to eat n oranges.
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
| class Solution { public: int minDays(int n) { int day = 1; unordered_set<int> vis; queue<int> q; q.push(n); while(!q.empty()) { int sz = q.size(); while(sz--) { auto num = q.front(); q.pop(); if(!vis.count(num - 1)) { if(num - 1 == 0) return day; q.push(num - 1); vis.insert(num-1); } if(num % 2 == 0 and !vis.count(num/2)) { q.push(num/2); vis.insert(num/2); } if(num % 3 == 0 and !vis.count(num/3)) { q.push(num/3); vis.insert(num/3); } } day++; } return day; } };
|