Author: devangini123

🌙 Depth First Search (DFS) Example: 1 DFS: [0, 1, 3, 2, 4] Example: 2 DFS: [1, 4, 5, 6, 7, 3, 9, 8, 2] Another Possible Solution: DFS: [1, 2, 3, 4, 5, 6, 7, 8, 9] Real-World Example DFS: [Mussoorie, Dehradun, Delhi, Agra, Jaipur, Chandigarh] We implement Depth-First Search (DFS) using a Stack data structure Push the start node into the stack. Create a visited set to track visited nodes. While the stack is not empty: Pop the top node. If not visited → mark visited and process it. Push all unvisited neighbors into the stack. Repeat until…

Read More

🌙 Breadth First Search (BFS) Example: 1 BFS: [0, 1, 2, 3, 4] Example: 2 BFS: [1, 2, 4, 3, 5, 9, 8, 6, 7] Real-World Example BFS: [Mussoorie, Dehradun, Chandigarh, , Delhi, Agra, Jaipur] This solution is also considered valid. BFS: [Mussoorie, Dehradun, Chandigarh, , Delhi, Jaipur, Agra] We implement Breadth-First Search (BFS) using a queue data structure Start by choosing a source node (the node from where you want to begin the traversal). Create a visited array or set to keep track of nodes you have already visited. Add the starting node to the queue and mark it…

Read More

🌙 Graphs A graph is a data structure that helps us represent relationships or connection between objects. It consists of: Vertices(Node): Entities or objects. Edges: Links or connection between vertices. Mathematically, A graph is written as G = (V, E) Degree How many connections does the node have. As how many edges going from the particular nodes. Examples: How many degree’s they have? 3 → 4 degree’s 7 → 2 degree’s 2 → 1 degree’s There is a special type of graph called a directed graph, which has in-degree and out-degree.(Further, we learn about these topics in detail) Path It…

Read More

🌙 Linked List A Linked List is a linear data structure in which elements (called nodes) are connected using pointers. Each node contains A value (the actual data). A reference (or pointer) to the next node (and optionally to the previous node in doubly linked lists). Types of Linked Lists Value: The data part Next: a pointer/reference to the next node. It moves only in one direction (forward). Structure: Structure: [value | next] -> [value | next] -> [value | null] Value: The data next: pointer to the next node prev: pointer to the previous node Allows bidirectional traversal Structure:…

Read More

🌙 Problem Statement: There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m – 1][n – 1]). The robot can only move either down or right at any point in time. Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner. The test cases are generated so that the answer will be less than or equal to 2 * 109. The answer is guaranteed…

Read More

🌙 Problem Statement: You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money. Return the number of combinations that make up that amount. If that amount of money cannot be made up by any combination of the coins, return 0. You may assume that you have an infinite number of each kind of coin. The answer is guaranteed to fit into a signed 32-bit integer. Example 1: Input: amount = 5, coins = [1,2,5] Output: 4 Explanation: there are four ways to make up the amount: 5=5 5=2+2+1…

Read More

🌙 Problem Statement: Given an integer array nums, return true if you can partition the array into two subsets such that the sum of the elements in both subsets is equal or false otherwise. Example 1: Input: nums = [1,5,11,5] Output: true Explanation: The array can be partitioned as [1, 5, 5] and [11]. Example 2: Input: nums = [1, 2, 3, 5] Output: false Explanation: The array can be partitioned as [1, 5, 5] and [11]. Constraints 1 { if(remS == 0) return true; if(remS < 0) return false; if(dp[remS][start] != undefined) return dp[remS][start]; for(let i=start; i < arr.length;…

Read More

🌙 Problem Statement: Given an integer array nums, return the length of the longest strictly increasing subsequence. Example 1: Input: nums = [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. Example 2: Input: nums = [0,1,0,3,2,3] Output: 4 Example 3: Input: nums = [7,7,7,7,7,7,7] Output: 1 Constraints 1

Read More

🌙 Problem Statement: Given an integer array nums, find a subarray that has the largest product, and return the product. The test cases are generated so that the answer will fit in a 32-bit integer. Example 1: Input: nums = [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6. Example 2: Input: nums = [-2,0,-1] Output: 0 Explanation: The result cannot be 2, because [-2,-1] is not a subarray. Constraints 1 val = val; return; } e = e->next; } Entry *ne = (Entry*)malloc(sizeof(Entry)); ne->key = strdup(key); ne->val = val; ne->next = table[h]; table[h] = ne; } int dp_get(const…

Read More

🌙 Problem Statement: Given an integer array nums, find a subarray that has the largest product, and return the product. The test cases are generated so that the answer will fit in a 32-bit integer. Example 1: Input: nums = [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6. Example 2: Input: nums = [-2,0,-1] Output: 0 Explanation: The result cannot be 2, because [-2,-1] is not a subarray. Constraints 1

Read More