Skip to main content

Posts

Leetcode 43: Multiply Strings

1. Problem Statement (Simple Explanation): You’re given two  non-negative integers  num1 and num2 as  strings . You must: Return their product as a  string . You  cannot : Use built-in big integer libraries. Convert the entire strings directly to integers. Lengths: 1 <= len(num1), len(num2) <= 200 Digits only, no leading zero except "0" itself. 2. Examples: Example 1: Input: num1 = "2", num2 = "3" 2 * 3 = 6 Output: "6" Example 2: Input: num1 = "123", num2 = "456" 123 * 456 = 56088 Output: "56088" 3. Intuition – Simulate Grade-School Multiplication: We multiply like on paper: For num1 = "123", num2 = "456":    1 2 3 x    4 5 6 ---------      7 3 8   (123 * 6, shifted by 0)    6 1 5     (123 * 5, shifted by 1)  4 9 2       (123 * 4, shifted by 2) ---------  5 6 0 8 8 But instead of summing separate rows, we can direct...
Recent posts

Cycle Sort

Cycle sort is a comparison-based sorting algorithm that is designed to minimize the number of writes. It works by dividing the input sequence into cycles, and then for each cycle, it finds the correct position for each element by performing a series of swaps. This algorithm has a time complexity of O(n^2), but it is very efficient when the number of writes is a concern. The main idea behind cycle sort is to minimize the number of writes by moving each element to its correct position without unnecessary swaps. This is achieved by dividing the input sequence into cycles, where each cycle is a permutation of a subset of the elements in the input sequence. For example, if the input sequence is [4, 1, 5, 3, 2], the cycles would be [4, 2], [1], [5], [3]. For each cycle, cycle sort finds the correct position for each element by performing a series of swaps. It starts by selecting the smallest element in the cycle, and then it swaps it with the element that should be in its correct position. I...

Leetcode 42: Trapping Rain Water

  1. Problem Statement (Simple Explanation): You’re given an array height of length n: Each height[i] is a non-negative integer representing a bar’s height. The width of each bar is 1. Imagine this array as an elevation map. You must compute  how many units of water  can be trapped between the bars after raining. 2. Examples: Example 1: Input: height = [0,1,0,2,1,0,1,3,2,1,2,1] Visualization shows 6 units of trapped water. Output: 6 Example 2: Input: height = [4,2,0,3,2,5] Trapped water: Between 4 and 3: 2 units above index 1, 4 units above index 2 (total 6). Between 3 and 5: 1 unit above index 4, 2 units above index 3 (total 3). Total: 6 + 3 = 9. Output: 9 3. Intuition: At any position i, the water height is: water_at_i = min(max_left[i], max_right[i]) - height[i] where: max_left[i] = max height on the left of i including i. max_right[i] = max height on the right of i ...