- package com.company;
- public class Main {
- public static void main(String[] args) {
- int[] tasks = new int[]{4, 4, 1, 3};
- int slotTime = 4;
- int result = minSlots(tasks, slotTime);
- System.out.println(result);
- }
- public static int minSlots(int[] tasks, int slotTime){
- return minSlotsRecursion(tasks, slotTime, 0, 0, 0, new boolean[tasks.length]);
- }
- public static int minSlotsRecursion(int[] tasks, int slotTime, int index, int currentSubsetSum, int totalSlots, boolean[] seen){
- if(index == tasks.length){
- if(allSeen(seen))
- return totalSlots + 1;
- else if(currentSubsetSum == 0)
- return Integer.MAX_VALUE;
- else {
- //System.out.println(currentSubsetSum);
- return minSlotsRecursion(tasks, slotTime, 0, 0, totalSlots + 1, seen);
- }
- }
- //include
- int include = Integer.MAX_VALUE;
- if(!seen[index] && currentSubsetSum + tasks[index] <= slotTime){
- seen[index] = true;
- include = minSlotsRecursion(tasks, slotTime, index + 1, currentSubsetSum + tasks[index], totalSlots, seen);
- seen[index] = false;
- }
- //exclude
- int exclude = minSlotsRecursion(tasks, slotTime, index + 1, currentSubsetSum, totalSlots, seen);
- return Math.min(include, exclude);
- }
- public static boolean allSeen(boolean[] seen){
- for (int i = 0; i < seen.length; i++){
- if(!seen[i])
- return false;
- }
- return true;
- }
- }
[text] solution
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new:
File Description
- solution
- Paste Code
- 10 Aug-2022
- 1.69 Kb
You can Share it:
Latest Code Pastes