Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions PartitionLabels.java
Original file line number Diff line number Diff line change
@@ -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<Integer> partitionLabels(String s) {
HashMap<Character, Integer> hmap = new HashMap<>();

for(int i = 0; i < s.length() ;i++) {
char c = s.charAt(i);
hmap.put(c,i);
}

List<Integer> 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;
}
}
31 changes: 31 additions & 0 deletions QueueReconstructionByHeight.java
Original file line number Diff line number Diff line change
@@ -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<int[]> result = new ArrayList<>();

for(int[] p : people) {
result.add(p[1],p);
}

return result.toArray(new int[0][0]);
}
}
}
41 changes: 41 additions & 0 deletions TaskScheduler.java
Original file line number Diff line number Diff line change
@@ -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<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
for (int i = 0; i < 26; i++) {
if (freqMap[i] > 0) {
maxHeap.offer(freqMap[i]);
}
}
Deque<int[]> 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;

}
}