Python Articles

Page 674 of 855

Path With Maximum Minimum Value in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 535 Views

In this problem, we need to find the maximum possible minimum value along any path from the top-left corner [0, 0] to the bottom-right corner [R-1, C-1] of a matrix. A path can move in four cardinal directions (north, east, west, south), and the score is determined by the smallest value encountered along that path. For example, if we have a path 8 → 4 → 5 → 9, the path's score would be 4 (the minimum value). Problem Example Consider this matrix ? 5 4 5 1 2 6 ...

Read More

Smallest Subsequence of Distinct Characters in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 274 Views

Finding the lexicographically smallest subsequence of distinct characters is a classic problem that can be solved using a greedy approach with a stack. Given a string, we need to find the smallest subsequence that contains all unique characters exactly once. For example, if the input is "cdadabcc", the output should be "adbc". Algorithm Overview The approach uses a stack to build the result and two dictionaries to track character positions and inclusion status ? last_occurrence: Stores the last position of each character in_stack: Tracks whether a character is already in the result stack stack: Builds ...

Read More

Insufficient Nodes in Root to Leaf Paths in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 354 Views

When working with binary trees, we sometimes need to remove nodes based on path sum criteria. A node is insufficient if every root-to-leaf path passing through that node has a sum strictly less than a given limit. This problem requires us to delete all insufficient nodes simultaneously and return the modified tree. Problem Understanding Given a binary tree and a limit value, we need to identify and remove nodes where all paths from root to leaf through that node have sums less than the limit. The key insight is that we work backwards from leaves, checking if any ...

Read More

Grumpy Bookstore Owner in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 314 Views

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 More

Capacity To Ship Packages Within D Days in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 437 Views

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 More

Construct Binary Search Tree from Preorder Traversal in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 880 Views

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 More

Smallest String Starting From Leaf in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 330 Views

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 More

Basic Calculator II in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

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 More

Binary Tree Zigzag Level Order Traversal in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 829 Views

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 More

Keys and Rooms in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 604 Views

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 More
Showing 6731–6740 of 8,546 articles
« Prev 1 672 673 674 675 676 855 Next »
Advertisements