Skip to main content

Posts

Leetcode 73: Set Matrix Zeroes

1. Problem Statement (Simple Explanation) You’re given an m x n integer matrix. Rule: If any element matrix[i][j] is 0, then  its entire row i and column j  must be set to 0. You must modify the matrix  in-place . 2. Examples Example 1: Input: matrix = [   [1,1,1],   [1,0,1],   [1,1,1] ] matrix[1][1] == 0 → set row 1 and column 1 to zeros. Output: [   [1,0,1],   [0,0,0],   [1,0,1] ] Example 2: Input: matrix = [   [0,1,2,0],   [3,4,5,2],   [1,3,1,5] ] Zero positions: (0,0) and (0,3). Rows to zero: row 0 Cols to zero: col 0, col 3 Zeroing them yields: Output: [   [0,0,0,0],   [0,4,5,0],   [0,3,1,0] ] 3. Approaches 3.1 Naive (extra matrix, O(m·n) extra space): Copy matrix, then for each zero in original, update row+col in copy. Not allowed (we want constant extra space). 3.2 Bet...

Leetcode 72: Edit Distance

1. Problem Statement (Simple Explanation) You’re given two strings: word1 word2 You can transform word1 into word2 using  three  operations: Insert  a character Delete  a character Replace  a character You must return the  minimum number of operations  required. This is the classic  Levenshtein distance  problem. 2. Examples Example 1: Input: word1 = "horse" word2 = "ros" One optimal sequence: "horse" → "rorse" (replace 'h' with 'r') "rorse" → "rose" (delete 'r') "rose" → "ros" (delete 'e') Output: 3 Example 2: Input: word1 = "intention" word2 = "execution" One optimal sequence (5 operations): intention → inention (delete 't') inention → enention (replace 'i' with 'e') enention → exention (replace 'n' with 'x') exention → exection (replace 'n' with...