博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
621. Task Scheduler && 358. Rearrange String k Distance Apart
阅读量:5064 次
发布时间:2019-06-12

本文共 2565 字,大约阅读时间需要 8 分钟。

621. Task Scheduler

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].
  2. The integer n is in the range [0, 100]

   

 

public class Solution {    public int leastInterval(char[] tasks, int n) {        if (tasks.length == 0)          return 0;        HashMap
taskMap = new HashMap<>(); for (char t : tasks) { taskMap.put(t, taskMap.getOrDefault(t, 0) + 1); } PriorityQueue
> toDo = new PriorityQueue<>( (a, b) -> a.getValue() == b.getValue() ? a.getKey() - b.getKey() : b.getValue() - a.getValue()); toDo.addAll(taskMap.entrySet()); int steps = 0; while (!toDo.isEmpty()) { List
> processed = new ArrayList<>(); int currentRoundSteps = n; while (currentRoundSteps >= 0 && !toDo.isEmpty()) { Map.Entry
item = toDo.poll(); --currentRoundSteps; ++steps; if (item.getValue() > 1) { item.setValue(item.getValue() - 1); processed.add(item); } } if (!processed.isEmpty()) { steps += currentRoundSteps + 1; } toDo.addAll(processed); } return steps; }}

 

358. Rearrange String k Distance Apart

Given a non-empty string str and an integer k, rearrange the string such that the same characters are at least distance k from each other.

All input strings are given in lowercase letters. If it is not possible to rearrange the string, return an empty string "".

Example 1:

str = "aabbcc", k = 3Result: "abcabc"The same letters are at least distance 3 from each other.

Example 2:

str = "aaabc", k = 3 Answer: ""It is not possible to rearrange the string.

Example 3:

str = "aaadbbcc", k = 2Answer: "abacabcd"Another possible answer is: "abcabcda"The same letters are at least distance 2 from each other.

 

 

 

转载于:https://www.cnblogs.com/neweracoding/p/7203275.html

你可能感兴趣的文章
html+css 布局篇
查看>>
银行排队问题(详解队列)
查看>>
input输入提示历史记录
查看>>
序列化和反序列化(1)---[Serializable]
查看>>
对二维数据进行边界拓展
查看>>
asp.net 验证控件
查看>>
评论列表显示及排序,个人中心显示
查看>>
微软职位内部推荐-Software Engineer II
查看>>
区分Integer.getInteger和Integer.valueOf使用方法
查看>>
MySQL oracle 分页
查看>>
iOS基础-UIKit框架-触摸事件-响应者链条
查看>>
SQL优化
查看>>
利用Highcharts插件制作动态图表
查看>>
用C语言操纵Mysql
查看>>
轻松学MVC4.0–6 MVC的执行流程
查看>>
4.9 Parser Generators
查看>>
[10月18日的脚本] 从Access中导入多个表到Excel
查看>>
centos下安装nginx
查看>>
redis集群如何清理前缀相同的key
查看>>
linux的学习系列 9--网络通信
查看>>