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
53 changes: 53 additions & 0 deletions problem1.java
Original file line number Diff line number Diff line change
@@ -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<Character, Integer> 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;
}
}
58 changes: 58 additions & 0 deletions problem2.java
Original file line number Diff line number Diff line change
@@ -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<int[]> 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;
}
}
53 changes: 53 additions & 0 deletions problem3.java
Original file line number Diff line number Diff line change
@@ -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<Integer> partitionLabels(String s) {

List<Integer> result = new ArrayList<>();
HashMap<Character, Integer> 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;
}
}