leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 8614a5600e2a0f02eed9ddf469fd718554a90642
parent 30572febabe3c4cd7b633994a463f339e02bdd70
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date:   Thu,  1 Jun 2023 21:51:40 -0500

completed minimum number of work sessions using dart with TLE.

Diffstat:
Aminimum-number-of-work-sessions-to-finish-the-tasks/minimum-number-of-work-sessions.dart | 44++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 44 insertions(+), 0 deletions(-)

diff --git a/minimum-number-of-work-sessions-to-finish-the-tasks/minimum-number-of-work-sessions.dart b/minimum-number-of-work-sessions-to-finish-the-tasks/minimum-number-of-work-sessions.dart @@ -0,0 +1,44 @@ +//This is my solution to the minimum work sessions problem using +//DFS and backtracking. The goal is to find the minimum number of sessions +//to completed the tasks where you can not split task time between multiple sessions +//and the length of each session is sessionTime. +//This gives a TLE because it is very brute force. + +class Solution { + int minSessions(List<int> tasks, int sessionTime) { + Map<int,int> vals = {}; + for(int i = 0; i < tasks.length ; ++i){ + vals[tasks[i]] = (vals[tasks[i]] ?? 0) + 1; + } + int sessions = (vals[sessionTime] ?? 0); + Map<String,int> memo = {}; + tasks.sort(); + tasks = tasks.sublist(0,tasks.length - sessions); + return minSess(tasks , sessionTime, 0,sessions); + } + int minSess(List<int> tasks , int sessionTime, int currentTime, int sessions){ + if(tasks.length == 0){ + return sessions; + } + int ret_val = 1000000000; + for(int i = 0 ; i < tasks.length ; ++i){ + if(tasks[i] > currentTime){ + List<int> new_tasks = List.from(tasks); + new_tasks.removeAt(i); + int current = minSess(new_tasks, sessionTime, sessionTime - tasks[i] , sessions + 1); + if(current < ret_val){ + ret_val = current; + } + } + else{ + List<int> new_tasks = List.from(tasks); + new_tasks.removeAt(i); + int current = minSess(new_tasks, sessionTime, currentTime - tasks[i] , sessions); + if(current < ret_val){ + ret_val = current; + } + } + } + return ret_val; + } +}