Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 121 of 2547
Program to check number of requests that will be processed with given conditions in python
When building web applications, we often need to implement rate limiting to prevent abuse. This problem demonstrates how to process requests with both per-user and global rate limits within a 60-second sliding window. We have a list of requests where each contains [uid, time_sec] representing a user ID and timestamp. We need to enforce two constraints: u (maximum requests per user in 60 seconds) and g (maximum global requests in 60 seconds). Requests at the same timestamp are processed by lowest user ID first. Example Given requests = [[0, 1], [1, 2], [1, 3]], u = 1, ...
Read MoreProgram to check some elements in matrix forms a cycle or not in python
A matrix cycle exists when we can start from a cell, move through adjacent cells with the same value, and return to the starting position without revisiting the previous cell. This problem uses depth-first search (DFS) to detect such cycles. Problem Understanding Given a 2D matrix, we need to check if we can form a cycle by moving through adjacent cells (up, down, left, right) that have the same value. The key constraint is that we cannot revisit the cell we just came from. For example, consider this matrix ? 2 2 2 1 ...
Read MoreProgram to count how many ways we can cut the matrix into k pieces in python
Suppose we have a binary matrix and a value k. We want to split the matrix into k pieces such that each piece contains at least one 1. The cutting rules are: Select a direction: vertical or horizontal Select an index in the matrix to cut into two sections If we cut vertically, we can no longer cut the left part but can only continue cutting the right part If we cut horizontally, we can no longer cut the top part and can only continue cutting the bottom part We need to find the number of ...
Read MoreProgram to find maximum possible population of all the cities in python
Consider a country represented as a tree with N nodes and N-1 edges. Each node represents a town, and each edge represents a bidirectional road. We need to upgrade some towns into cities following these constraints: no two cities should be adjacent, and every town must be adjacent to at least one city. Our goal is to find the maximum possible population of all upgraded cities. Problem Understanding Given source and dest arrays representing road connections, and a population array for each town, we need to select nodes (upgrade to cities) such that ? No two ...
Read MoreProgram to count subsets that sum up to k in python
Suppose we have a list of numbers called nums and another value k, we have to find the number of subsets in the list that sum up to k. If the answer is very large then mod this with 10^9 + 7. So, if the input is like nums = [2, 3, 4, 5, 7] k = 7, then the output will be 3, as we can choose the subsets [2, 5], [3, 4] and [7]. Algorithm To solve this, we will follow these steps − dp := a list of ...
Read MoreProgram to check given point in inside or boundary of given polygon or not in python
Suppose we have a list of cartesian points [(x1, y1), (x2, y2), ..., (xn, yn)] representing a polygon, and also have two values x and y. We need to check whether point (x, y) lies inside this polygon or on the boundary. So, if the input is like points = [(0, 0), (1, 3), (4, 4), (6, 2), (4, 0)] and pt = (3, 1): ...
Read MoreProgram to check how many ways we can choose empty cells of a matrix in python
Suppose we have an N x N binary matrix where 0 represents empty cells and 1 represents blocked cells. We need to find the number of ways to choose N empty cells such that every row and every column has at least one chosen cell. If the answer is very large, return the result mod 10^9 + 7. Problem Understanding Given a matrix like this: .cell { fill: white; stroke: #333; stroke-width: 1; } .blocked { ...
Read MoreProgram to find minimum number of buses required to reach final target in python
Suppose we have a n x 3 matrix where each row contains three fields [src, dest, id] − this means the bus has route from src to dest. It takes one unit of money to get on a new bus, but if we stay on the same bus we have to pay one unit only. We have to find the minimum cost necessary to take the bus from location 0 to the final stop (largest location). If there is no solution return -1. Problem Example Consider the following bus routes ? Source Destination Bus ID ...
Read MoreProgram to evaluate one mathematical expression without built-in functions in python
Sometimes we need to evaluate mathematical expressions without using Python's built-in eval() function. This requires parsing the expression manually while respecting operator precedence rules. So, if the input is like s = "2+3*5/7", then the output will be 4, as 2 + ((3 * 5) / 7) = 4. Algorithm Overview To solve this, we follow these steps − Reverse the input string to process from right to left Create a get_value() function to parse numbers and handle signs Create a get_term() function to handle multiplication and division (higher precedence) In the main function, handle ...
Read MoreProgram to find maximum profit we can make after k Buy and Sell in python
Stock trading problems are common in dynamic programming. Given a list of stock prices and a maximum number of transactions k, we need to find the maximum profit possible. Each transaction consists of buying and then selling a stock. So, if the input is like prices = [7, 3, 5, 2, 3] and k = 2, then the output will be 3. We can buy at price 3, sell at 5 (profit = 2), then buy at 2 and sell at 3 (profit = 1), giving us total profit of 3. Approach We'll use dynamic programming with ...
Read More