diff --git a/PartitionLabels.java b/PartitionLabels.java new file mode 100644 index 0000000..22fea99 --- /dev/null +++ b/PartitionLabels.java @@ -0,0 +1,34 @@ +/* +* Approach: Greedy Approach + - Idea is to be greedy and find the last index of the seen element and make the partition till that index + - Firstly, create a map or char array of size 26 to store the last index of the characters + - In the second pass, iterate over the string, get the first element and set end to last index of that element + - keep iterating and keep udating the end and once we have iterated to all the elements till the end, then thats our partition + - TC: O(n) + - SC: O(n) + */ +class Solution { + public List partitionLabels(String s) { + HashMap map = new HashMap<>(); + List res = new ArrayList<>(); + int end = 0; + //start and end to get the size of the partition + int start = 0; + + for(int i = 0; i < s.length(); i++){ + char ch = s.charAt(i); + map.put(ch, i); + } + + for(int i = 0; i < s.length(); i++){ + char curr = s.charAt(i); + end = Math.max(end, map.get(curr)); + //once the i reaches the end index, that means we have found our first partition, so add it to the result and update the start pointer to point to the next element + if(i == end){ + res.add(end - start + 1); + start = i + 1; + } + } + return res; + } +} diff --git a/QueueReconstructionByHeight.java b/QueueReconstructionByHeight.java new file mode 100644 index 0000000..a72c922 --- /dev/null +++ b/QueueReconstructionByHeight.java @@ -0,0 +1,28 @@ +/* +* Approach: Greedy approach +- Idea is to think about relative positions and think of people with the tallest height +- Sort people array by heights first and then by number of people in the front. +- Iterate through the sorted array (consider example 1) + - first person will be of height tallest and least number of people in the front so go ahead and add them to the list (7,0) + - second person is either of height smaller or the same height but number of people will either be same/diff for diff height or different for same height (7,1), go ahead and add it to the list + - similary third is (6,1) -> so height 6 can have one person of height greater or equal to 6 in front of it so go ahead and slide person of height (7,1) +- TC: O(n^2logn) -> nlogn for sorting the people array and n^2 for sliding of people in the front +- SC: O(n) + */ +class Solution { + public int[][] reconstructQueue(int[][] people) { + List q = new ArrayList<>(); + Arrays.sort(people, (a,b) ->{ //nlogn + if(a[0] == b[0]){ + return a[1] - b[1]; + } + return b[0] - a[0]; + }); + + for(int[] per: people){//n^2 - sliding of people in front that is costly + q.add(per[1], per); + } + + return q.toArray(new int[0][]); + } +} \ No newline at end of file diff --git a/TaskScheduler.java b/TaskScheduler.java new file mode 100644 index 0000000..1dea77a --- /dev/null +++ b/TaskScheduler.java @@ -0,0 +1,36 @@ +/* +* Approach: The idea is to find how many idle cpus will be there in order to finish the given tasks +- Firstly, we need to be greedy and look for most critical or highest freq tasks and how many highfreq tasks are there +- Then calculate the partitions, available slots, pending tasks and idle state + - consider ex1: maxFreq = 3, totalmaxFreqTasks = 2 and n = 2 + A _ _ A _ _ A -> from this, we can calculate + - partitions: 2 -> maxFreq - 1; + - available slots: partitions * (n - (totalmaxFreqTasks - 1)) -> 4 slots -> if there are two maxFreq tasks, we need to put them together to get the local minima, resulting into global minima. + - pending tasks: once all high freq tasks are done, we will have tasks remaining -> task.length - (maxFreq * totalmaxFreqTasks) + - idle: max of (0, (available slots - pending)), max because if the answer is negative that means we dont have any idle cpu so in that case the number of idle cpus should be 0 and not negative because that will mean we are producing extra timelol +- Finally, return the total number of tasks + idle cpu intervals +TC: O(n) -> iterate over tasks array to count maxFreq and iterate over keySet to get totalnumofmaxtasks +SC: O(n) -> worst case for storing tasks and its freq + */ +class Solution { + public int leastInterval(char[] tasks, int n) { + HashMap map = new HashMap<>(); + int maxFreq = 0; + int numOfMaxFreqTasks = 0; + for(char c: tasks){ + map.put(c, map.getOrDefault(c, 0) + 1); + maxFreq = Math.max(maxFreq, map.get(c)); + } + for(char k: map.keySet()){ + if(map.get(k) == maxFreq) + numOfMaxFreqTasks++; + } + + int partitions = maxFreq - 1; + int availableSlots = partitions * (n - (numOfMaxFreqTasks - 1)); + int pendingTasks = tasks.length - (maxFreq * numOfMaxFreqTasks); + int idle = Math.max(0, (availableSlots - pendingTasks)); + + return tasks.length + idle; + } +} \ No newline at end of file