ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[LeetCode] 1553. Minimum Number of Days to Eat N Oranges

2020-08-18 02:00:53  阅读:374  来源: 互联网

标签:1553 cur Days oranges Day queue Oranges 橘子 Eat


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

吃掉N个橘子的最少天数。

题意是给一个数字N代表橘子的个数,现在有三种吃法,每次吃橘子的时候你只能选择一种吃法。请问如何吃才能尽快吃完橘子,吃的次数最少。吃法如下,

  • 吃掉一个橘子。
  • 如果剩余橘子数 n 能被 2 整除,那么你可以吃掉 n/2 个橘子。
  • 如果剩余橘子数 n 能被 3 整除,那么你可以吃掉 2*(n/3) 个橘子。

这个题我一开始做的时候试着用贪心的思路。因为乍一看如果橘子数量很大而且剩余橘子数量能被3整除的话,一直选择第三种吃法似乎是最快的;如果剩余橘子数量不能被3整除,则试试看能不能被2整除;如果再不行则试着只吃掉一个橘子。这个思路会超时。

一个能通过的思路是BFS。每次判断的时候,三种方法都试一下,将三种方法吃剩下的橘子数放入queue,就跟树的层序遍历一样。如此遍历,最后看看需要几轮能把橘子吃完,返回那个轮数。这样常规地做BFS思路是没问题,但是依然会超时。

既然常规的BFS会超时,同时发现其实有一些中间结果是重复出现的,那么就试着用一个hashset记录一下中间结果。如果再次出现,则直接跳过,不需要再次放入queue。

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public int minDays(int n) {
 3         int steps = 0;
 4         Queue<Integer> queue = new LinkedList<>();
 5         queue.offer(n);
 6         HashSet<Integer> set = new HashSet<>();
 7         while (!queue.isEmpty()) {
 8             int size = queue.size();
 9             for (int i = 0; i < size; i++) {
10                 int cur = queue.poll();
11                 if (set.contains(cur)) {
12                     continue;
13                 }
14                 set.add(cur);
15                 if (cur == 0) {
16                     return steps;
17                 }
18                 if (cur % 3 == 0) {
19                     queue.offer(cur / 3);
20                 }
21                 if (cur % 2 == 0) {
22                     queue.offer(cur / 2);
23                 }
24                 if (cur >= 1) {
25                     queue.offer(cur - 1);
26                 }
27             }
28             steps++;
29         }
30         return steps;
31     }
32 }

 

LeetCode 题目总结

标签:1553,cur,Days,oranges,Day,queue,Oranges,橘子,Eat
来源: https://www.cnblogs.com/cnoodle/p/13521191.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有