You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package class_2022_02_3_week;
import java.util.HashMap;
// 来自学员看到的腾讯面试
// 测试链接 : https://leetcode.com/problems/minimum-number-of-days-to-eat-n-oranges/
public class Code02_MinimumNumberOfDaysToEatNOranges {
// 所有的答案都填在这个表里
// 这个表对所有的过程共用
public static HashMap<Integer, Integer> dp = new HashMap<>();
public static int minDays(int n) {
if (n <= 1) {
return n;
}
if (dp.containsKey(n)) {
return dp.get(n);
}
// 1) 吃掉一个橘子
// 2) 如果n能被2整除吃掉一半的橘子剩下一半
// 3) 如果n能被3正数吃掉三分之二的橘子剩下三分之一
// 因为方法2和3是按比例吃橘子所以必然会非常快
// 所以,决策如下:
// 可能性1为了使用2方法先把橘子吃成2的整数倍然后直接干掉一半剩下的n/2调用递归
// 即n % 2 + 1 + minDays(n/2)
// 可能性2为了使用3方法先把橘子吃成3的整数倍然后直接干掉三分之二剩下的n/3调用递归
// 即n % 3 + 1 + minDays(n/3)
// 至于方法1),完全是为了这两种可能性服务的,因为能按比例吃,肯定比一个一个吃快(显而易见的贪心)
int ans = Math.min(n % 2 + 1 + minDays(n / 2), n % 3 + 1 + minDays(n / 3));
dp.put(n, ans);
return ans;
}
}