625. Minimum Factorization & 621. Task Scheduler

18 篇文章 0 订阅
16 篇文章 0 订阅

思路:子所以把这两个题放在一起,感觉都有贪心的一个概念

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 = 2
Output: 8
Explanation: 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;
    }
}

然后为了避免每次都sort,把数据放到priority queue里面就好了

import heapq
from collections import Counter
class Solution(object):
    def leastInterval(self, tasks, n):
        """
        :type tasks: List[str]
        :type n: int
        :rtype: int
        """
        c=Counter(tasks)
        q = []
        for k in c: heapq.heappush(q, (-c[k], k))
        
        res = 0
        while q:
            k = n+1
            tmp = []
            while k>0 and q:
                cnt, c = heapq.heappop(q)
                tmp.append((cnt+1, c))
                k-=1
                res+=1
            for cnt, c in tmp:
                if cnt<0: heapq.heappush(q, (cnt,c))
            
            if not q: break
            res+=k  # if k is still > 0, it means we need to be idle
        
        return res
    
s=Solution()
print(s.leastInterval(tasks = ["A","A","A","B","B","B"], n = 2))






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值