diff --git a/problem1.java b/problem1.java new file mode 100644 index 0000000..73f6d3f --- /dev/null +++ b/problem1.java @@ -0,0 +1,53 @@ +import java.util.*; + +class Solution { + + /** + Time Complexity : O(N) + Explanation: + We count task frequencies once and then scan the frequency map. + + Space Complexity : O(1) + Explanation: + HashMap stores at most 26 uppercase English letters. + + Did this code successfully run on LeetCode : Yes + + Any problem you faced while coding this : + Initially struggled to calculate idle slots correctly. + Fixed it by using the most frequent task as the base structure: + - partitions = maxfreq - 1 + - available idle slots depend on cooldown n + - pending tasks fill those idle slots + Also handled multiple tasks having the same maximum frequency. + */ + + public int leastInterval(char[] tasks, int n) { + + HashMap map = new HashMap<>(); + + int l = tasks.length; + int maxfreq = 0; + int noofmaxfreq = 0; + + // Count task frequency + for (char task : tasks) { + map.put(task, map.getOrDefault(task, 0) + 1); + maxfreq = Math.max(maxfreq, map.get(task)); + } + + // Count how many tasks have max frequency + for (char task : map.keySet()) { + if (map.get(task) == maxfreq) { + noofmaxfreq++; + } + } + + int partitions = maxfreq - 1; + int available = partitions * (n - (noofmaxfreq - 1)); + int pending = l - (noofmaxfreq * maxfreq); + int empty = Math.max(0, available - pending); + + return l + empty; + } +} \ No newline at end of file diff --git a/problem2.java b/problem2.java new file mode 100644 index 0000000..8f70a18 --- /dev/null +++ b/problem2.java @@ -0,0 +1,58 @@ +import java.util.*; + +class Solution { + + /** + Time Complexity : O(N^2) + Explanation: + - Sorting takes O(N log N) + - Inserting into ArrayList at specific index takes O(N) + - Total worst case becomes O(N^2) + + Space Complexity : O(N) + Explanation: + Extra list is used to reconstruct the queue. + + Did this code successfully run on LeetCode : Yes + + Any problem you faced while coding this : + Initially tried placing shorter people first, + but later insertions disturbed the arrangement. + Fixed it by: + 1) Sorting taller people first + 2) For same height, sort by smaller k first + Then inserting each person at index k automatically places them + in the correct position. + */ + + public int[][] reconstructQueue(int[][] people) { + + List li = new ArrayList<>(); + + // Sort: + // height descending + // k ascending + Arrays.sort(people, (a, b) -> { + + if (a[0] == b[0]) { + return a[1] - b[1]; + } + + return b[0] - a[0]; + }); + + // Insert person at index k + for (int[] person : people) { + li.add(person[1], person); + } + + // Convert list to array + int[][] res = new int[li.size()][2]; + + for (int i = 0; i < li.size(); i++) { + res[i] = li.get(i); + } + + return res; + } +} \ No newline at end of file diff --git a/problem3.java b/problem3.java new file mode 100644 index 0000000..4c3748d --- /dev/null +++ b/problem3.java @@ -0,0 +1,53 @@ +import java.util.*; + +class Solution { + + /** + Time Complexity : O(N) + Explanation: + We scan the string once to store last occurrence of each character, + then scan again to create partitions. + + Space Complexity : O(1) + Explanation: + HashMap stores at most 26 lowercase English letters. + + Did this code successfully run on LeetCode : Yes + + Any problem you faced while coding this : + Initially confused about where a partition should end. + Fixed it by storing the last index of every character. + While scanning, keep updating the partition end as the farthest + last occurrence of characters seen so far. + When current index reaches end, we close the partition. + */ + + public List partitionLabels(String s) { + + List result = new ArrayList<>(); + HashMap map = new HashMap<>(); + + // Store last occurrence of each character + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + map.put(c, i); + } + + int start = 0; + int end = 0; + + // Build partitions + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + + end = Math.max(end, map.get(c)); + + if (i == end) { + result.add(end - start + 1); + start = i + 1; + } + } + + return result; + } +} \ No newline at end of file