Markdown To PDF
Markdown To PDF
1. Contains Duplicate
Problem: Given an array, return true if any value appears at least twice.
Solution: Use HashSet to track seen numbers. Time: O(n), Space: O(n)
2. Valid Anagram
Solution: Use frequency array for character counting. Time: O(n), Space: O(1)
3. Two Sum
4. Group Anagrams
Solution: Sort characters as key for HashMap. Time: O(n * k * log k), Space: O(n * k)
Solution: Use HashMap + PriorityQueue. Time: O(n log k), Space: O(n)
Two Pointers
6. Valid Palindrome
Problem: Check if string is palindrome after converting to lowercase and removing non-alphanumeric chars.
public boolean isPalindrome(String s) {
int left = 0, right = s.length() - 1;
if (Character.toLowerCase(s.charAt(left)) !=
Character.toLowerCase(s.charAt(right))) {
return false;
}
left++;
right--;
}
return true;
}
Solution: Two pointers from ends, skip non-alphanumeric. Time: O(n), Space: O(1)
7. 3Sum
int left = i + 1;
int right = nums.length - 1;
Solution: Sort + Two pointers. Skip duplicates. Time: O(n²), Space: O(1)
result[0] = 1;
for (int i = 1; i < n; i++) {
result[i] = result[i-1] * nums[i-1];
}
int right = 1;
for (int i = n-1; i >= 0; i--) {
result[i] *= right;
right *= nums[i];
}
return result;
}
Solution: Two passes - prefix and suffix products. Time: O(n), Space: O(1)
return result;
}
}
Solution: Use length encoding with delimiter. Time: O(n), Space: O(n)
int maxLength = 0;
for (int num : nums) {
if (!set.contains(num-1)) {
int currentNum = num;
int currentLength = 1;
while (set.contains(currentNum+1)) {
currentNum++;
currentLength++;
}
Problem: Find two lines that form container with most water.
return maxArea;
}
Solution: Two pointers from ends, move shorter line. Time: O(n), Space: O(1)
return water;
}
Solution: Two pointers with max height tracking. Time: O(n), Space: O(1)
Sliding Window
Problem: Find maximum profit from buying and selling stock once.
return maxProfit;
}
Solution: Track minimum price and max profit. Time: O(n), Space: O(1)
return maxLength;
}
Problem: Find longest substring where any character can be replaced k times.
return maxLength;
}
Solution: Sliding window with character count. Time: O(n), Space: O(1)
while (count == 0) {
if (end - start + 1 < minLen) {
minLen = end - start + 1;
minStart = start;
}
Solution: Sliding window with character count. Time: O(n), Space: O(k)
int n = nums.length;
int[] result = new int[n-k+1];
int ri = 0;
deque.offer(i);
if (i >= k - 1) {
result[ri++] = nums[deque.peek()];
}
}
return result;
}
Solution: Use deque to maintain decreasing sequence. Time: O(n), Space: O(k)
Stack
return stack.isEmpty();
}
Problem: Design stack that supports push, pop, top, and getMin operations.
class MinStack {
private Stack<Integer> stack;
private Stack<Integer> minStack;
public MinStack() {
stack = new Stack<>();
minStack = new Stack<>();
}
Solution: Use two stacks - one for values, one for minimums. Time: O(1), Space: O(n)
return stack.pop();
}
private void backtrack(List<String> result, String current, int open, int close, int max) {
if (current.length() == max * 2) {
result.add(current);
return;
}
Stack (Continued)
return result;
}
int fleets = 0;
double slowest = 0;
for (int i = n - 1; i >= 0; i--) {
if (cars[i][1] > slowest) {
slowest = cars[i][1];
fleets++;
}
}
return fleets;
}
Solution: Sort by position, compare arrival times. Time: O(n log n), Space: O(n)
Binary Search
return -1;
}
int m = matrix.length;
int n = matrix[0].length;
int left = 0;
int right = m * n - 1;
if (value == target) {
return true;
} else if (value < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return false;
}
Problem: Find minimum eating speed to eat all bananas within h hours.
return left;
}
return nums[left];
}
Solution: Binary search with rotated array logic. Time: O(log n), Space: O(1)
return -1;
}
Solution: Binary search with pivot handling. Time: O(log n), Space: O(1)
public TimeMap() {
map = new HashMap<>();
}
Solution: HashMap with sorted lists and binary search. Time: O(log n) for get, O(1) for set. Space: O(n)
Linked List
return prev;
}
Solution: Dummy node and two pointers. Time: O(n + m), Space: O(1)
// Find middle
ListNode slow = head, fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
Solution: Find middle + reverse second half + merge. Time: O(n), Space: O(1)
second.next = second.next.next;
return dummy.next;
}
return copyHead;
}