Suppose we have a number n, indicating that there are n different courses labeled from 1 to n. We also have an array called relations where relations[i] contains a pair (prevCourse_i, nextCourse_i), representing a prerequisite relationship between the course prevCourse_i and the course nextCourse_i: so course prevCourse_i has to be taken before the course nextCourse_i. The last parameter we have is k. In one semester, we can take at most k number of courses as long as we have taken all the prerequisites in the previous semester for the courses we are taking. We have to find the minimum number ... Read More
Sometimes we need to find the maximum absolute sum of any subarray within an array. This problem extends Kadane's algorithm by considering both maximum and minimum subarray sums, as the absolute value of a negative sum might be larger than the positive maximum. Problem Understanding Given an array nums, we need to find the maximum absolute sum of any contiguous subarray. The absolute sum of a subarray [nums_l, nums_l+1, ..., nums_r] is |nums_l + nums_l+1 + ... + nums_r|. For example, if nums = [2, -4, -3, 2, -6], the subarray [2, -4, -3, 2] has sum ... Read More
Suppose we have an array of positive values candiesCount where candiesCount[i] denotes the number of candies of the ith type we have. We also have another array called queries where queries[i] has three parameters [favoriteType_i, favoriteDay_i, dailyCap_i]. We have some rules: We start eating candies on day 0. We cannot eat any candy of type i unless we have eaten all candies of previous i-1 types. We must eat at least one candy per day until we have eaten all of them. Following these rules, we have ... Read More
A Minimum Spanning Tree (MST) contains the minimum weight edges that connect all vertices in a graph. In this problem, we need to identify critical edges (edges whose removal increases MST weight) and pseudo-critical edges (edges that can appear in some MSTs but not all). Problem Understanding Given an undirected weighted graph with n vertices (0 to n-1), we need to find: Critical edges: Removing them increases the MST weight Pseudo-critical edges: Can be part of an MST but aren't required ... Read More
Suppose we have a tree with n nodes that are numbered from 0 to n-1. The tree is given by a parent array, where parent[i] is the parent of node i. The root of the tree is node 0. We have to find the kth ancestor of a given node. If the ancestor is not present, then return -1. So, if the input is like ? 0 1 2 ... Read More
Suppose we have an array called houses and have another value k. Here houses[i] represents the location of the ith house along a street, we have to allocate k mailboxes in the street, and find the minimum total distance between each house and its nearest mailbox. So, if the input is like houses = [6, 7, 9, 16, 22] and k = 2, then the output will be 9 because if we place mailbox at 7 and 18, then minimum total distance from each house is |6-7|+|7-7|+|9-7|+|16-18|+|22-18| = 1+0+2+2+4 = 9. Algorithm To solve this problem, we ... Read More
Suppose there is an array of size m representing m houses in a small city. Each house must be painted with one of the n colors (labeled from 1 to n). Some houses are already painted, so no need to paint them again. Houses colored with the same adjacent color form a neighborhood. We have the array houses, where houses[i] represents the color of house i. If the color value is 0, the house is not colored yet. We have another array called cost, which is a 2D array where cost[i][j] represents the cost to color house i with ... Read More
Suppose we are given an m x n grid box, where each cell has a board that is positioned either from the top-right to bottom-left, or from the top-left to the bottom-right. A ball is dropped from each top cell, and we need to determine where each ball lands at the bottom of the box. The grid is represented as a matrix where 1 means the diagonal board spans from top-left to bottom-right, and -1 means it spans from top-right to bottom-left. If a ball gets stuck or goes out of bounds, we return -1 for that position. ... Read More
A flexible queue is a data structure that allows insertion and removal of elements from the front, middle, and back positions. This is useful when you need dynamic access to different parts of your queue. Understanding the Queue Operations Our queue supports six main operations ? push_from_front() − Insert element at the beginning push_from_middle() − Insert element at the middle position push_from_back() − Insert element at the end pop_from_front() − Remove and return first element pop_from_middle() ... Read More
Suppose we have two strings, s and t, we have to check whether s and t are close or not. We can say two strings are close if we can attain one from the other using the following operations − Exchange any two existing characters. (like abcde to aecdb) Change every occurrence of one existing character into another existing character, and do the same with the other characters also. (like aacabb → bbcbaa (here all a's are converted to b's, and vice versa)) We can use the operations on either ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance