625. Minimum Factorization & 621. Task Scheduler
来源:互联网 发布:tcp监听端口检测失败 编辑:程序博客网 时间:2024/06/06 01:20
思路:子所以把这两个题放在一起,感觉都有贪心的一个概念
Given a positive integer a
, find the smallest positive integer b
whose multiplication of each digit equals to a
.
If there is no answer or the answer is not fit in 32-bit signed integer, then return 0.
Example 1
Input:
48Output:
68
Example 2
Input:
15Output:
35
先把9的因子取出,再把8的因子取出,。。。。。
大的因子取得越多,后面剩下的数就越小,最后的结果就越小
public class Solution { public int smallestFactorization(int a) { if(a == 1)return 1; int[] n = new int[10]; for(int i=9; i>=2; i--) { while(a%i == 0) { n[i]++; a/=i; } } if(a != 1)return 0; StringBuilder sb = new StringBuilder(); for(int i=1; i<=9; i++) for(int j=0; j<n[i]; j++) sb.append(i); try { return Integer.valueOf(sb.toString()); } catch (Exception e) { return 0;} }}
Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one task or just be idle.
However, there is a non-negative cooling interval n that means between two same tasks, there must be at least n intervals that CPU are doing different tasks or just be idle.
You need to return the least number of intervals the CPU will take to finish all the given tasks.
Example 1:
Input: tasks = ['A','A','A','B','B','B'], n = 2Output: 8Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.
Note:
- The number of tasks is in the range [1, 10000].
思路:先把能整块处理的先处理掉, 剩下来的再化零为整
26个大写字母,开一个length为26的数组,先统计各个字母的个数,如果有n个字母的计数不为0,就说明可以n个task没有空闲的交替执行,执行一次就把计数减小,当不够n个字母的时候跳出循环,处理剩下的
package l621;import java.util.Arrays;/* * 先把能整块处理的先处理掉 * 剩下来的再化零为整 * * 但是BUG */public class Solution { public int leastInterval(char[] tasks, int n) { if(n == 0)return tasks.length; int[] a = new int[26]; int ret = 0; for(char c : tasks) a[c-'A']++; while(true) { Arrays.sort(a); if(25-n<0 || a[25-n]==0) break; int t = a[25-n]; ret += t*(n+1); for(int i=25-n; i<26; i++) a[i] -= t; } if(a[25] != 0) { ret += (a[25]-1)*(n+1); for(int i=25; i>=0; i--) if(a[i] == a[25]) ret++; } return ret; }}
但是WA,后发现,不能一次把t个后面连续n个都消掉,只能消掉一个,再排序,重新选最大的N个消,
因为消掉1个后,可能消掉的数不再是Top n,强行消为0的话就相当于在数组中找小的数删掉
比如 1 2 3 3 4 5,找到后面3 4 5 ,现在不能消掉3个变为1 2 3 0 1 2
因为消掉1个后变为1 2 3 2 4 5,倒数第三个已经比倒数第4个小了,强行消除的话会把非0的个数减小,这样可能本来可以作为整体没有空闲去处理的情况反而变得只能零散的处理,得到就不是最优解
因为太贪心了(想把a[25-n]完全消掉),导致最后得不偿失(非0的个数减小),最后反而结果偏大
import java.util.Arrays;public class Solution { public int leastInterval(char[] tasks, int n) { if(n == 0)return tasks.length; int[] a = new int[26]; int ret = 0; for(char c : tasks) a[c-'A']++; while(true) { Arrays.sort(a); if(25-n<0 || a[25-n]==0) break; int t = a[25-n]; ret += (n+1); for(int i=25-n; i<26; i++) a[i] -= 1; } if(a[25] != 0) { ret += (a[25]-1)*(n+1); for(int i=25; i>=0; i--) if(a[i] == a[25]) ret++; } return ret; }}
- 625. Minimum Factorization & 621. Task Scheduler
- leetcode-625. Minimum Factorization
- 625. Minimum Factorization
- leetcode 625. Minimum Factorization
- leetcode 625. Minimum Factorization
- LeetCode 621. Task Scheduler
- 【LeetCode】621. Task Scheduler
- 621. Task Scheduler
- [LeetCode] 621. Task Scheduler
- Leetcode 621. Task Scheduler
- [leetcode]621. Task Scheduler
- leetcode 621. Task Scheduler
- Leetcode:621. Task Scheduler
- 621. Task Scheduler
- [LeetCode]621. Task Scheduler
- 621. Task Scheduler
- 621. Task Scheduler
- 621. Task Scheduler
- MySQL的事务处理以及隔离级别
- java面试宝典及答案
- pandas修改DataFrame的列名
- XML生成和解析
- Java与算法(9)
- 625. Minimum Factorization & 621. Task Scheduler
- HEVC函数入门(1)——HM编码器的基本结构
- 我的网络编程学习之路——TCP(流)套接字的读和写
- 守护进程
- java炒冷饭系列03 接口修饰符
- java开发交流
- 欢迎使用CSDN-markdown编辑器
- Java实现-删除数字
- 深入理解Spring--动手实现一个简单的SpringIOC容器