diff --git a/Problem-1 b/Problem-1 new file mode 100644 index 0000000..a41aca0 --- /dev/null +++ b/Problem-1 @@ -0,0 +1,29 @@ +// Time Complexity : O(m), m->number of tasks +// Space Complexity : O(p), p->number of unique tasks + +// Store the frequency of the characters in a hashmap and find the max frequency +// Find the number of tasks pending to be scheduled and the available slots left after scheduling the tasks with max frequency +// Find the idle slots and return total tasks + idle slots + +class Solution { + public int leastInterval(char[] tasks, int n) { + int maxFreq = 0; + Map mp = new HashMap<>(); + for(char ch : tasks) { + mp.put(ch, mp.getOrDefault(ch, 0) + 1); + maxFreq = Math.max(maxFreq, mp.get(ch)); + } + + int maxFreqCount = 0; + for(Character key : mp.keySet()) { + if(mp.get(key) == maxFreq) + maxFreqCount++; + } + + int partitions = maxFreq - 1; + int pending = tasks.length - maxFreq * maxFreqCount; + int available = partitions * (n - (maxFreqCount - 1)); + int idleSlots = Math.max(0, available - pending); + return tasks.length + idleSlots; + } +} \ No newline at end of file diff --git a/Problem-2 b/Problem-2 new file mode 100644 index 0000000..7cf6a3b --- /dev/null +++ b/Problem-2 @@ -0,0 +1,24 @@ +// Time Complexity : O(n^2) +// Space Complexity : O(n) + +// Sort the people by descending order of height and then by the number of people in front of them +// Iterate through the sorted array and place the people at the index mentioned by the number of people in front of them +// Return the new array + +class Solution { + public int[][] reconstructQueue(int[][] people) { + // Sort the array + Arrays.sort(people, (a,b) -> { + if(a[0] == b[0]) + return a[1] - b[1]; + return b[0] - a[0]; + }); + + // Position people according to the number of people in front of them + List result = new ArrayList<>(); + for(int i = 0; i < people.length; i++) { + result.add(people[i][1], people[i]); + } + return result.toArray(new int[0][0]); + } +} \ No newline at end of file diff --git a/Problem-3 b/Problem-3 new file mode 100644 index 0000000..2de8805 --- /dev/null +++ b/Problem-3 @@ -0,0 +1,33 @@ +// Time Complexity : O(n) +// Space Complexity : O(1) + +// Store the character and its last index in a hashmap +// Iterate through the string and keep the last index of the current character to be the end of that partition +// If any character in between has its last index beyond it, then extend the partition else start a new partition from end + 1 +// Store the lengths of the partitions and return it + +class Solution { + public List partitionLabels(String s) { + HashMap mp = new HashMap<>(); + // Store character and its last index + for(int i = 0; i < s.length(); i++) { + char ch = s.charAt(i); + mp.put(ch, i); + } + List result = new ArrayList<>(); + int start = 0; + int end = 0; + for(int i = 0; i < s.length(); i++) { + char ch = s.charAt(i); + // if current character's last index is after end, then update + end = Math.max(end, mp.get(ch)); + + // Reached end of current partition, add length to result + if(i == end) { + result.add(end - start + 1); + start = end + 1; + } + } + return result; + } +} \ No newline at end of file