当前位置:网站首页>LeetCode 1986. The minimum working time to complete the task is one question per day
LeetCode 1986. The minimum working time to complete the task is one question per day
2022-07-07 16:59:00 【@Little safflower】
Problem description
You are arranged n A mission . The duration of the task is n Array of integers for tasks Express , The first i Tasks cost tasks[i] Hours to complete . One Working hours in , You can at most Continuous work sessionTime Hours , Then take a rest .
You need to complete the given task according to the following conditions :
If you start a task at a certain time , You need to The same Time period to complete it .
After completing a task , You can Immediately Start a new task .
You can press In any order To complete the task .
Here you are. tasks and sessionTime , Please follow the above requirements , Return to what is needed to complete all tasks least Count Working hours .Test data guarantee sessionTime Greater than or equal to tasks[i] Medium Maximum .
Example 1:
Input :tasks = [1,2,3], sessionTime = 3
Output :2
explain : You can complete all tasks in two working hours .
- The first working period : Complete the first and second tasks , cost 1 + 2 = 3 Hours .
- The second working period : Complete the third task , cost 3 Hours .
Example 2:Input :tasks = [3,1,3,1,1], sessionTime = 8
Output :2
explain : You can complete all tasks in two working hours .
- The first working period : Complete all tasks except the last one , cost 3 + 1 + 3 + 1 = 8 Hours .
- The second working period , Finish the last task , cost 1 Hours .
Example 3:Input :tasks = [1,2,3,4,5], sessionTime = 15
Output :1
explain : You can complete all tasks in one working time .
Tips :
n == tasks.length
1 <= n <= 14
1 <= tasks[i] <= 10
max(tasks[i]) <= sessionTime <= 15source : Power button (LeetCode)
link :https://leetcode.cn/problems/minimum-number-of-work-sessions-to-finish-the-tasks
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Java
class Solution {
public int minSessions(int[] tasks, int sessionTime) {
Arrays.sort(tasks);
int n = tasks.length;
int left = 1;
int right = n;
while(left < right){
int mid = (left + right) >> 1;
int[] timeParagraph = new int[mid];
if(dfs(n - 1,tasks,timeParagraph,mid,sessionTime)){
right = mid;
}else {
left = mid + 1;
}
}
return left;
}
public boolean dfs(int index,int[] tasks,int[] timeParagraph,int mid,int sessionTime){
if(index == -1) return true;
boolean[] visible = new boolean[sessionTime + 1];
// Traverse every time segment
for(int i = 0;i < mid;i++){
// If the value of the previous bucket has already appeared , You can skip the bucket , Try to put into the next bucket
if(visible[timeParagraph[i]]) continue;
visible[timeParagraph[i]] = true;
int t = timeParagraph[i] + tasks[index];
if(t <= sessionTime){
timeParagraph[i] = t;
if(dfs(index - 1,tasks,timeParagraph,mid,sessionTime)) return true;
timeParagraph[i] -= tasks[index];
}
}
return false;
}
}边栏推荐
猜你喜欢

"The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."
字节跳动Android金三银四解析,android面试题app

如何选择合适的自动化测试工具?

Binary search tree (basic operation)

Skimage learning (3) -- gamma and log contrast adjustment, histogram equalization, coloring gray images
Direct dry goods, 100% praise

Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions

null == undefined

time标准库

网关Gateway的介绍与使用
随机推荐
打造All-in-One应用开发平台,轻流树立无代码行业标杆
[designmode] facade patterns
Seaborn数据可视化
three. JS create cool snow effect
LeetCode 300. 最长递增子序列 每日一题
1亿单身男女“在线相亲”,撑起130亿IPO
【PHP】PHP接口继承及接口多继承原理与实现方法
Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions
Deep listening array deep listening watch
QT视频传输
Prometheus API deletes all data of a specified job
面向接口编程
Three. JS series (2): API structure diagram-2
LeetCode 1031. 两个非重叠子数组的最大和 每日一题
运算符
Sqlserver2014+: create indexes while creating tables
Find tags in prefab in unity editing mode
LeetCode 1654. 到家的最少跳跃次数 每日一题
Build an all in one application development platform, light flow, and establish a code free industry benchmark
最新2022年Android大厂面试经验,安卓View+Handler+Binder