Skip to main content

Posts

Leetcode 71: Simplify Path

1. Problem Statement (Simple Explanation) You’re given an  absolute Unix-style path  string path that: Always starts with '/'. May contain: directory names (letters, digits, _, more periods, etc.) special entries: "." → current directory (ignore it) ".." → parent directory (go up one level if possible) multiple slashes "//", "///", which are treated as a single slash. You must return the  simplified canonical path , such that: Starts with exactly one '/'. Directories are separated by a single '/'. No trailing slash (unless the path is just root /). No "." or ".." segments remain. "...", "...." etc. are treated as  normal directory names . 2. Examples Example 1: Input: path = "/home/" Output: "/home" Trailing slash removed. Example 2: Input: path = "/home//foo/" Output: "/home/foo" Multiple slashes collapsed; trailing slash removed. Example 3: I...

Leetcode 70: Climbing Stairs

1. Problem Statement (Simple Explanation) You need to climb a staircase with n steps. Each move you can climb either  1 step  or  2 steps . You want to know:  in how many distinct ways  can you reach exactly step n from step 0? 2. Examples Example 1: Input: n = 2 Ways: 1 + 1 2 Total: 2 Output: 2 Example 2: Input : n = 3 Ways: 1 + 1 + 1 1 + 2 2 + 1 Total: 3 Output: 3 3. Approach – Fibonacci-style DP (O(n), O(1) space) Let f(n) = number of ways to climb n steps. To reach step n, your last move could be: From step n-1 with a 1-step jump → f(n-1) ways. From step n-2 with a 2-step jump → f(n-2) ways. So: f(n) = f(n-1) + f(n-2) Base cases: f(1) = 1 (only 1) f(2) = 2 (1+1, 2) This is exactly the  Fibonacci  recurrence starting from those seeds. We can compute iteratively in O(n) time and O(1) space. Pseudo-code: Complexity: Time: O(n) Space: O(1) Giv...

Little Elephant and Lemonade

There are n rooms (numbered 0 to n-1). Room i contains Ci bottles, each with some volume. Little Elephant visits rooms in a sequence given by array P of length m: First visit → room P[0] Second visit → room P[1] … m-th visit → room P[m-1] Rule when visiting a room: He looks at all bottles currently present in that room , finds the one with maximum volume , drinks it completely (i.e., removes that bottle). If the room is visited again later, he again drinks the largest remaining bottle in that room (if any). Your task: For each test case, compute the total volume of lemonade he drinks over all visits. Input Format: First line: integer T — number of test cases. For each test case: First line: two integers n and m n — number of rooms m — number of visits Second line: m integers — array P (room indices visited). Next n lines — description of bottles in each room: For room i, the line format is: Ci V0 V1 ... V(Ci-1) Ci = count of bottles in room i Vj = volume of j-th bottle Output Format...