Essential Leetcode Problems for SDEs
Essential Leetcode Problems for SDEs
The Longest Palindromic Substring problem uses dynamic programming to efficiently track and calculate palindromic substrings by maintaining a two-dimensional DP table, where each cell at index (i, j) indicates whether the substring from index i to j forms a palindrome. This avoids the need to repeatedly check the same substrings, which would otherwise result in a time complexity of O(n^3). Instead, by leveraging previously computed results, the overall complexity reduces to O(n^2).
The Sliding Window technique enables solving the Longest Substring Without Repeating Characters problem efficiently by dynamically adjusting the starting and ending indices of the window. By expanding the window with a non-repeating character and contracting it when a repeat is found, the substring's length is optimized in linear time, O(n), as each character is processed only a few times .
The DP approach to "Decode Ways" effectively utilizes memoization by keeping a record of computed results for subproblems, thus avoiding redundant calculations performed in recursive solutions. This tabulation-based strategy considerably cuts down from exponential time O(2^n) to linear O(n) complexity. Compared to exhaustive recursion, DP's awareness of overlapping sub-problems grants it significant performance superiority, especially for larger input sizes .
In the "Word Ladder" problem, hash tables offer an efficient way of storing and retrieving each intermediate word state, allowing quick checks and transformations between possible word sequences. This approach reduces redundant processing since each word transformation is recorded and referenced, limiting the broad search space typical in simple BFS, resulting in significant performance improvements with a time complexity of O(M^2 * N).
The "Dungeon Game" problem presents challenges in determining the minimum initial strength required through a grid traversal, requiring carefully structured DP solutions. A critical challenge is setting correct conditions at each grid cell based on remaining health points, which are addressed by reverse traversing the grid and maintaining a DP table of minimum health needed, backward from the goal to the start. This ensures decisions are made with future path requirements in mind, achieving an O(m*n) solution .
The 3Sum problem benefits from the two pointers technique as it allows for an O(n^2) solution by first sorting the array and then using two pointers to simultaneously search the subarray for pairs summing to a target value (the negative of the current element). This technique avoids the need to resort to a more computationally expensive O(n^3) triple nested loop, as managing these two pointers helps in narrowing down the possibilities quickly and efficiently .
Binary search is crucial in solving the "Median of Two Sorted Arrays" by applying it to the smaller of the two arrays to partition both arrays in a way that elements left to the partition make up the left half of the median and those right make up the right half. This method drastically reduces the complexity from a naive O(m + n) to O(log(min(m, n))), as it eliminates whole chunks of potential median candidates in each step of the binary search .
The "Fraction to Recurring Decimal" problem utilizes hash maps to detect the start and end of repeating decimal sequences by tracking remainders. Each remainder is mapped to its corresponding position in the result sequence. This mapping is essential because remainders that repeat indicate the beginning of a recurring cycle, allowing us to enclose this sequence in parentheses, hence accurately representing recurring decimals .
Using a heap or priority queue in the "Maximum Number of Events That Can Be Attended" problem allows for efficient selection of events by prioritizing event ending times. The greediness in selecting events with the earliest finishing time ensures that more events can be attended as the end time of one event directly affects the start time of the next available event. This approach ensures that events are processed in optimal order by maintaining event duration information in O(n log n) complexity .
The Greedy Algorithm in the "Remove K Digits" exploits a sequential minimization approach by iteratively comparing and removing higher digits when smaller ones come after, to achieve minimum resultant number optimally. This approach leverages locally optimal choices, leading to a globally optimal solution with reduced computational overhead, operating in linear O(n) complexity instead of more costly combinatorial solutions .