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
Python Articles
Page 670 of 855
Smallest Subsequence of Distinct Characters in Python
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 MoreInsufficient Nodes in Root to Leaf Paths in Python
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 MoreGrumpy 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 More