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
Articles by Arnab Chakraborty
Page 88 of 377
Program 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 MoreProgram to find minimum cost to reach final index with at most k steps in python
Suppose we have a list of numbers nums and another value k. The items at nums[i] represent the costs of landing at index i. We start from index 0 and need to reach the last index of nums. In each step we can jump from position X to any position up to k steps away. We need to minimize the sum of costs to reach the last index. So, if the input is like nums = [2, 3, 4, 5, 6] and k = 2, then the output will be 12, as we can select the path: 2 + ...
Read MoreProgram to find maximum profit after buying and selling stocks at most two times in python
Given a list of stock prices in chronological order, we need to find the maximum profit from buying and selling stocks at most two times. The constraint is that we must buy before selling, and we cannot hold multiple stocks simultaneously. For example, if prices = [2, 6, 3, 4, 2, 9], the maximum profit is 11 by buying at 2, selling at 6 (profit: 4), then buying at 2 and selling at 9 (profit: 7). Algorithm Approach We use dynamic programming to track four states ? first_buy: Maximum profit after first purchase first_sell: Maximum ...
Read MoreProgram to check a string can be broken into given list of words or not in python
Suppose we have a word list and another string s with no spaces. We have to check whether the string can be broken down using the list of words or not. So, if the input is like words = ["love", "python", "we", "programming", "language"] s = "welovepythonprogramming", then the output will be True Algorithm To solve this, we will follow these steps − words := a new set of all unique words Define a function rec() . This will take i if i ...
Read MoreProgram to find maximum number of boxes we can fit inside another boxes in python
Suppose we have a list of boxes where each box is represented by [width, height]. We can put one box inside another box only if both the width and height of the first box are smaller than the second box. We need to find the maximum number of boxes we can nest inside each other. Problem Example Given the following boxes ? Width Height 12 12 10 10 6 6 5 10 The output will be 3, as we can fit the box [6, ...
Read MoreProgram to find length of longest consecutive path of a binary tree in python
Suppose we have a binary tree; we have to find the longest consecutive path in the binary tree. A consecutive path is a sequence of nodes where each node's value differs from the previous by exactly 1 (either increasing or decreasing). So, if the input is like 3 2 4 5 9 ...
Read MoreProgram to make almost BST to exact BST in python
A binary search tree (BST) has a specific property: for each node, all values in the left subtree are smaller, and all values in the right subtree are larger. When two nodes are swapped in a BST, we can identify and fix them by performing an inorder traversal and finding the nodes that violate the BST property. Understanding the Problem In an inorder traversal of a valid BST, the values should be in ascending order. When exactly two nodes are swapped, we'll find one or two violations where a node's value is less than the previous node's value. ...
Read More