diff --git a/PartitionLabels.java b/PartitionLabels.java new file mode 100644 index 0000000..a9c9dc6 --- /dev/null +++ b/PartitionLabels.java @@ -0,0 +1,30 @@ +/** + * Approach: Use a HashMap to store the last index of each character to determine partition boundaries. + * Solution: Iterate through the string, updating the current partition end to the farthest last index seen. + * When the current index equals the partition end, close the partition and record its length. + */ +public class PartitionLabels { + public List partitionLabels(String s) { + HashMap hmap = new HashMap<>(); + + for(int i = 0; i < s.length() ;i++) { + char c = s.charAt(i); + hmap.put(c,i); + } + + List result = new ArrayList<>(); + + int start = 0; + int end = 0; + + for(int i = 0; i < s.length() ; i++) { + char c = s.charAt(i); + end = Math.max(end, hmap.get(c)); + if(i == end) { + result.add(end-start+1); + start = i+1; + } + } + return result; + } +} \ No newline at end of file diff --git a/QueueReconstructionByHeight.java b/QueueReconstructionByHeight.java new file mode 100644 index 0000000..77ecc5f --- /dev/null +++ b/QueueReconstructionByHeight.java @@ -0,0 +1,31 @@ +/** + * Approach: Sort people by height in descending order; if heights are equal, sort by k value ascending. + * Solution: Insert each person into a list at the index equal to their k value, maintaining relative order. + * This ensures each person has exactly k taller or equal people in front when reconstruction is complete. + */ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class QueueReconstructionByHeight { + class Solution { + public int[][] reconstructQueue(int[][] people) { + Arrays.sort(people, (a,b) -> { + + if(a[0] == b [0]) { + return a[1] - b[1]; + } + + return b[0] - a[0]; + }); + + List result = new ArrayList<>(); + + for(int[] p : people) { + result.add(p[1],p); + } + + return result.toArray(new int[0][0]); + } +} +} diff --git a/TaskScheduler.java b/TaskScheduler.java new file mode 100644 index 0000000..908c142 --- /dev/null +++ b/TaskScheduler.java @@ -0,0 +1,41 @@ +/** + * Approach: Use a max-heap to schedule the most frequent tasks first while respecting cooldowns using a queue. + * Solution: Pop tasks from the heap, execute them, and push them into a cooldown queue with the next valid time. + * Repeat until both heap and queue are empty, tracking total time to execute all tasks. + */ +public class TaskScheduler { + public int leastInterval(char[] tasks, int n) { + int[] freqMap = new int[26]; + + for (char ch : tasks) { + freqMap[ch - 'A']++; + } + + PriorityQueue maxHeap = new PriorityQueue<>((a, b) -> b - a); + for (int i = 0; i < 26; i++) { + if (freqMap[i] > 0) { + maxHeap.offer(freqMap[i]); + } + } + Deque queue = new LinkedList<>(); // count, time + int time = 0; + + while (!maxHeap.isEmpty() || !queue.isEmpty()) { + time++; + + if (!maxHeap.isEmpty()) { + int currentCount = maxHeap.poll() - 1; + if (currentCount > 0) { + queue.add(new int[] {currentCount, time + n }); + } + } + if (!queue.isEmpty() && queue.peekFirst()[1] == time) { + int[] elementEligibleForMaxHeap = queue.removeFirst(); + maxHeap.offer(elementEligibleForMaxHeap[0]); + } + } + + return time; + + } +} \ No newline at end of file