Optimizing Disjoint Set Data Structure For Minimum Cost Queries
TC: O(n*4alpha) optimized using Disjoint Set data structure & bitwise AND op to find min cost of edges & common parents.
There is always a price for those who persevere.
TC: O(n*4alpha) optimized using Disjoint Set data structure & bitwise AND op to find min cost of edges & common parents.
Validating a Binary Search Tree (BST) in O(n) time complexity. A BST is valid if for every node, its value is greater than all values in its left subtree and less than all values in its right subtree.
Min capability of k subarrays: O(nlog(Max(nums))) Find min of all max values possible using binary search.
Max Candies Problem: Find max candies that can be allocated to kids from same lot, given candies array and number of kids. Use binary search to find optimal solution.
Count substrings with sum/k or count=k in O(n) time using a sliding window approach with a map to track vowel counts.
Implementing a Word Dictionary using Trie data structure. The `WordDictionary` class uses a Trie to store words and provides methods for adding words and searching for words with wildcards (`.`).
Minimax algorithm for grid game: TC O(n), SC O(n). Robot 1 tries to minimize values collected by Robot 2, who maximizes. Prefix sums used to optimize calculations.
BFS algorithm used to find min cost path in grid with obstacles. Time complexity O(n*mlog(n*m)), space complexity O(m*n).
BFS solution for minimum obstacles: TC:O(n*mlog(n*m)), SC:O(n*m). Dijkstra's algorithm used with priority queue to find shortest path.
Count Stars Between Pipes: Given a string s and ranges a, count stars enclosed between first & last pipes for each range. Use prefix & suffix arrays to find nearest pipes & calculate star counts.
Minimize Xor: Given two numbers, num1 and num2, find the minimum number that can be obtained by performing bitwise operations on num1 to match the bit count of num2.
Count prefix and suffix pairs in a list of words. Brute force: O(n^2*m). Optimal solution using Trie: O(n*m).
Min Operations: Given a string of 1s and 0s, find the minimum number of operations to move all 1s to one side. TC: O(n), SC: O(n)
Find anagrams in string s of length n and pattern p of length k. TC: O(n), SC: O(k) for substrings + O(1) for constant space array.
Calculating prefix sum in O(n) time, then iterating over it to find valid splits also in O(n). Solution returns count of ways to split array.
Create prefix sum matrix O(n*m) time, O(n*m) space. Sum of rectangle is calculated in O(row1+row2) time using prefix sum matrix.
Calculate prefix[] in O(n) and get vowel counts in O(k). Solution: iterate words, update prefix[], then calculate result for each query using prefix[]. Vowel check: a,e,i,o,u.
Calculate prefix sums in O(n) time, then use them to find subarray sums in O(1) time with NumArray class. Prefix[i] stores sum till ith index, making range sum calculation efficient.
Sort jobs by profit in descending order, then schedule as close to deadline as possible. Use a slot array to keep track of assigned jobs and maximize profit.
Erase Overlap Intervals: Sort intervals by end time, remove overlapping ones. Count non-overlapping intervals and return length - count.
Sort balloons by end point, then iterate to find non-overlapping intervals. Count the number of intervals and return as minimum arrows needed.
Min platforms required at railway station: O(nlogn) time complexity. Sort trains by arrival & departure times to minimize platform usage.
SlidingWindow Problem: O(n*100) TC, O(n) SC. Find xth smallest negative num in each window of size k. Use HashMap to store freq of nums[i].
Min operations to make all numbers in array increasing: iterate from end, update lower values with greatest divisor. Return -1 if no possible increase.
Solution: Count substrings with at least k distinct characters. Use sliding window approach, incrementing right pointer and decrementing left pointer when necessary hash values reach threshold.
Optimal approach for counting max or subsets in O(2n) time complexity using backtracking. Memoization also possible but not optimal.
Merge intervals from two lists in O(nlogn) time, where n is the total number of intervals. Use a single list to combine and sort both input lists, then greedily find overlapping intervals.