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 153 of 377
Grumpy Bookstore Owner in Python
The Grumpy Bookstore Owner problem is a classic sliding window optimization challenge. A bookstore owner can use a technique to stay calm for X consecutive minutes, and we need to find the maximum number of happy customers by choosing the optimal window. Problem Understanding Given three inputs ? customers[i] − number of customers in minute i grumpy[i] − 1 if owner is grumpy, 0 if calm X − technique duration (consecutive minutes) When grumpy, customers are unhappy. We can use the technique once to make the owner calm for X minutes. Find the maximum ...
Read MoreCapacity To Ship Packages Within D Days in Python
The Capacity to Ship Packages Within D Days problem asks us to find the minimum ship capacity needed to transport all packages within a given number of days. Given an array of package weights and D days, we need to determine the least weight capacity that allows shipping all packages sequentially. Problem Understanding For example, with weights [3, 2, 2, 4, 1, 4] and D = 3 days, the minimum capacity is 6 ? Day 1: 3, 2 (total weight: 5) Day 2: 2, 4 (total weight: 6) Day 3: 1, 4 (total weight: 5) ...
Read MoreConstruct Binary Search Tree from Preorder Traversal in Python
A Binary Search Tree (BST) can be constructed from its preorder traversal using a stack-based approach. In preorder traversal, we visit the root first, then the left subtree, followed by the right subtree. 8 5 10 1 7 12 ...
Read MoreSmallest String Starting From Leaf in Python
Suppose we have the root of a binary tree, where each node contains a value from 0 to 25, representing the letters 'a' to 'z': a value of 0 represents 'a', a value of 1 represents 'b', and so on. We have to find the lexicographically smallest string that starts at a leaf of this tree and ends at the root. z(25) b(1) d(3) b(1) ...
Read MoreBasic Calculator II in Python
A basic calculator evaluates mathematical expressions containing non-negative integers and operators (+, -, *, /). The calculator must handle operator precedence correctly, where multiplication and division are performed before addition and subtraction. For the input "3+2*2", the output should be 7 because multiplication has higher precedence than addition. Algorithm Approach We use a stack-based approach to handle operator precedence ? Remove spaces from the expression Use a stack to store intermediate results Process multiplication and division immediately Store addition and subtraction operands for later summation Implementation class Solution: ...
Read MoreBinary Tree Zigzag Level Order Traversal in Python
Binary tree zigzag level order traversal visits nodes level by level, alternating direction each level. The first level goes left to right, second level right to left, third level left to right, and so on. 3 9 20 15 7 ...
Read MoreKeys and Rooms in Python
The Keys and Rooms problem is a graph traversal challenge where we need to determine if all rooms can be visited starting from room 0. Each room contains keys to other rooms, and we need to check if we can reach every room using available keys. Problem Understanding Given N rooms numbered 0 to N-1, we start in room 0. Each room i contains a list of keys rooms[i], where each key opens a specific room. We need to return True if we can visit all rooms, False otherwise. Key constraints ? Initially, all rooms ...
Read MoreMaximum Swap in Python
The Maximum Swap problem asks us to find the maximum possible number by swapping at most two digits in a given non-negative integer. For example, given 2736, we can swap digits 2 and 7 to get 7236, which is the maximum possible value. Algorithm Overview The approach works by comparing the original number with its digits sorted in descending order. When we find the first position where they differ, we perform the optimal swap ? Convert the number into a list of digits Create a sorted version in descending order to find the target arrangement Find ...
Read MorePalindromic Substrings in Python
A palindromic substring is a substring that reads the same forwards and backwards. Given a string, we need to count all palindromic substrings where substrings with different start or end indices are counted separately, even if they contain the same characters. For example, in the string "aaa", there are 6 palindromic substrings: "a", "a", "a", "aa", "aa", "aaa". Brute Force Approach The simplest approach is to check every possible substring and verify if it's a palindrome ? class Solution: def countSubstrings(self, s): counter ...
Read MoreMinimum Moves to Equal Array Elements II in Python
Finding the minimum number of moves to make all array elements equal is a classic problem that can be solved efficiently using the median approach. A move consists of incrementing or decrementing any element by 1. Algorithm Overview The key insight is that the optimal target value is the median of the array. This minimizes the total distance (moves) needed to make all elements equal ? Steps to Solve Sort the array to find the median easily Find the median element (middle element of sorted array) ...
Read More