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:

48 
Output:
68

Example 2
Input:

15
Output:
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:

  1. 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;    }}







原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 电视遥控器不小心按了高清键怎么办 康佳电视打开右下角是红色的怎么办 天猫超市退款成功后收到货怎么办 天猫还没收到货就确认收货了怎么办 支付宝红包抵扣被关了怎么办 水貂绒大衣白色过色了怎么办 双面羊绒大衣袖子洗短了怎么办 特殊类型订单销量评价删除后怎么办 淘宝上卖家发货发错了地址怎么办 买完保险想换保险代理人怎么办 怀孕内裤两边磨的好疼怎么办 露肩连体裤穿着卡裆怎么办 魅族手机恋与制作人换诺基亚怎么办 蚊子叮咬后擦风油精了红肿怎么办 机动车已转让没过户出了事情怎么办 签好的合同如果甲方违约怎么办? 饭店没签合同辞职不给工资怎么办 两家为了带孩子闹翻了怎么办啊? 抵押后租赁的房屋被法院拍卖怎么办 房子买20年了没有过户怎么办 二手车没过户行驶证丢了怎么办 买的二手车行驶证丢了怎么办 在京东仓库做事把东西损坏了怎么办 微信显示该账号登陆环境异常怎么办 干洗店把衣服洗坏了怎么办 千牛上怎么改淘宝密码忘记了怎么办 公司收了代理商押金到期不退怎么办 淘宝显示签收但是我没收到货怎么办 淘宝东西没收到显示签收了怎么办 开拼多多店铺的密码忘了怎么办 拼多多密码跟店铺名忘了怎么办 闲鱼上卖出的宝贝被调包了怎么办 上传身份证照片说格式错误该怎么办 我给厂里打的款不给发货怎么办 净值接近不定期份额折算阀值怎么办 有锁电信4g掉了怎么办 在电脑中找不到想作废的发票怎么办 科目三补考费发票丢了怎么办 母婴店飞鹤奶粉突然厂家撤货怎么办 澳门买的保健品感觉是假的怎么办 淘宝买东西提交需求时卡死了怎么办