当前位置:网站首页>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;
}
}
边栏推荐
- OpenGL personal notes
- 【PHP】PHP接口继承及接口多继承原理与实现方法
- [Android -- data storage] use SQLite to store data
- 二叉搜索树(特性篇)
- LeetCode-SQL第一天
- Localstorage and sessionstorage
- AutoLISP series (2): function function 2
- LeetCode 1774. 最接近目标价格的甜点成本 每日一题
- 字节跳动Android面试,知识点总结+面试题解析
- Horizontal and vertical centering method and compatibility
猜你喜欢
随机推荐
OpenGL personal notes
Geoserver2.18 series (5): connect to SQLSERVER database
LeetCode 1043. 分隔数组以得到最大和 每日一题
全网“追杀”钟薛高
Pychart ide Download
AutoLISP series (2): function function 2
LocalStorage和SessionStorage
打造All-in-One应用开发平台,轻流树立无代码行业标杆
作为Android开发程序员,android高级面试
LeetCode 1986. 完成任务的最少工作时间段 每日一题
两类更新丢失及解决办法
网关Gateway的介绍与使用
QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏
使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑
【DesignMode】享元模式(Flyweight Pattern)
Binary search tree (basic operation)
【PHP】PHP接口继承及接口多继承原理与实现方法
QML beginner
如何快速检查钢网开口面积比是否符合 IPC7525
C语言进阶——函数指针