Skip to main content

Posts

Leetcode 63: Unique Paths II

1. Problem Statement (Simple Explanation) Similar to  Unique Paths (62) , but now we have  obstacles . You’re given an m x n grid obstacleGrid: 0 = free cell. 1 = obstacle. Robot: Starts at top-left: (0, 0). Wants to reach bottom-right: (m-1, n-1). Can only move  right  or  down . Cannot  step on cells with obstacles (1). Return the  number of unique paths  from start to goal, avoiding obstacles. Answer is guaranteed ≤ 2 * 10 9 . 2. Examples Example 1: Input: obstacleGrid = [   [0,0,0],   [0,1,0],   [0,0,0] ] There is one obstacle in the middle (1,1). Valid paths: Right → Right → Down → Down Down → Down → Right → Right Output: 2 Example 2: Input: obstacleGrid = [   [0,1],   [0,0] ] Only one path is valid due to the obstacle at (0,1): Down → Right Output: 1 3. Approach – Dynamic Programming with Obstacles (O(m·n)) We extend the DP approach from Problem 62, but...

Equal Elements

You are given an array  A  of size  N . In  one operation , you can: Select indices  i  and  j  with  i≠j Set  A i = A j You need to find the  minimum number of operations  required to make  all elements of the array equal . 1. Problem Restatement For each test case: You have an array  A  of size  N . Operation: pick two positions  i  and  j   (i≠j)  and assign  A i = A j . After some operations, you want all elements of  A  to be the same. Goal:   Output the  minimum  number of operations needed. Input Format: First line: integer  T  — number of test cases. For each test case: First line: integer  N  — array size. Second line:  N  space-separated integers — elements of array  A . Output Format: For each test case, print the minimum number of operations on a new line. Constraints: 1 ≤ T ≤ 1000 1 ≤ N ≤ 2* 10 5 1 ≤ A i ≤ ...