(int apple)
| 3 | public class Code03_AppleMinBags { |
| 4 | |
| 5 | public static int minBag(int apple) { |
| 6 | if (apple < 0) { |
| 7 | return -1; |
| 8 | } |
| 9 | if (apple == 0) { |
| 10 | return 0; |
| 11 | } |
| 12 | // max 决定用多少个8号袋 100 / 8 = 12 11 10 ... 0 |
| 13 | for (int max = (apple / 8); max >= 0; max--) { |
| 14 | // max 个 8号袋的时候,max * 8, 6号袋剩下的苹果数 = apple - max * 8 |
| 15 | int rest = apple - (max * 8); |
| 16 | if (rest % 6 == 0) { |
| 17 | return max + rest / 6; |
| 18 | } |
| 19 | } |
| 20 | return -1; |
| 21 | } |
| 22 | |
| 23 | // O(1) |
| 24 | public static int test(int apple) { |