Suppose we have a 2D binary matrix, we have to find the total number of square submatrices with all 1s. This problem can be solved using dynamic programming by tracking the largest square ending at each position. So, if the input is like: 1 ... Read More
We have a list of numbers representing rocket sizes and directions. Positive integers indicate rightward movement, negative numbers indicate leftward movement. The absolute value represents the rocket's size. When rockets collide, the smaller one is destroyed, and equal-sized rockets both get destroyed. Problem Understanding Given rockets moving in opposite directions, we need to simulate collisions and find the final state. Rockets moving in the same direction never collide. Example For nums = [3, 8, 5, -5], rockets 5 (right) and -5 (left) collide and both are destroyed, leaving [3, 8]. Algorithm We'll use a ... Read More
Suppose we have a list of numbers called nums where each value represents a group of people looking to skydive together. And we have another value k representing how many days they can apply for skydiving. We have to find the minimum capacity of the plane we need to be able to fulfill all requests within k days. The requests should be fulfilled in the order they were given and a plane can only fly once a day. So, if the input is like nums = [16, 12, 18, 11, 13], k = 3, then the output will be ... Read More
Finding the shortest cycle containing a specific target node in a directed graph is a common graph problem. We can solve this using Breadth-First Search (BFS) to explore paths from the target node until we find a cycle back to it. Problem Understanding Given an adjacency list representation of a directed graph and a target node, we need to find the shortest cycle that contains the target node. If no such cycle exists, return -1. ... Read More
We need to find the total cost for completing all shipments between ports, where the cost is the shortest path distance between each pair of ports. This problem uses the Floyd-Warshall algorithm to find all shortest paths in a graph. Problem Understanding Given a list of ports where ports[i] represents connected ports from port i, and shipments as pairs [i, j], we need to find the minimum total cost. Each edge has weight 1, so the cost is the shortest path length. For the example: ports = [[1, 4], [2], [3], [0, 1]], shipments = [[1, 4]], ... Read More
The zigzag string conversion arranges characters in a zigzag pattern across k lines. Starting from the top line, we move diagonally down until reaching the bottom (kth line), then move diagonally up back to the top, and repeat this pattern. For example, if the input is s = "ilovepythonprogramming" and k = 5, the zigzag pattern looks like this: i h r l t o a o y ... Read More
Suppose we have a binary tree, we have to find the longest path that alternates between left and right child and going down. An alternating path means we switch between going left and right at each level. So, if the input is like ? 2 3 4 5 ... Read More
Suppose we have a list of words and a string called letters, we have to find the size of the longest word that can be made by rearranging the given letters. In the letters there may be asterisk character (*) which can match any character. It is not necessary to use all the letters. So, if the input is like words = ["prince", "rice", "price", "limit", "hello"] letters = "*r**ce*", then the output will be 6, as the longest word we can make is "prince" with length 6. Algorithm To solve this, we will follow these steps ... Read More
This problem finds how many words in a list are formed by concatenating other words from the same list. We can reuse words multiple times during concatenation. Given the input words = ["hello", "world", "helloworld", "famous", "worldfamous", "programming"], the output is 2 because "helloworld" is formed by concatenating "hello" + "world", and "worldfamous" is formed by "world" + "famous". Algorithm We use a Trie data structure combined with depth-first search (DFS) ? Build a Trie: Store all words in a trie for efficient prefix matching DFS Search: For each ... Read More
Given a 24-hour time string in "hh:mm" format, we need to find the next closest time that can be formed by reusing the same digits. We can reuse any digit from the original time as many times as needed. For example, if the input is "03:15", the output will be "03:30" since it's the nearest valid time using only digits 0, 3, 1, and 5. Algorithm Approach We'll use backtracking to generate all possible valid times using the given digits, then find the next time in chronological order ? Extract all four digits from the ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance