Programming Articles

Page 18 of 2547

Insert Delete GetRandom O(1) in Python

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

The Insert Delete GetRandom O(1) problem requires designing a data structure that supports three operations in average O(1) time: inserting elements, removing elements, and returning a random element with equal probability. Problem Requirements insert(val) − Insert an item val to the set if not already present remove(val) − Remove an item val from the set if it exists getRandom() − Return a random element with equal probability for all elements Solution Approach We use two data structures to achieve O(1) operations: Dictionary (present) − Maps values to their indices in the array ...

Read More

Flatten Nested List Iterator in Python

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

A nested list iterator flattens a multi-level list structure into a single sequence. Each element can be either an integer or another list containing integers or nested lists. Problem Statement Given a nested list like [[1, 1], 2, [1, 1]], we need to create an iterator that returns elements in flattened order: 1, 1, 2, 1, 1. Algorithm The solution uses recursion to flatten the nested structure during initialization ? Initialize an empty result list and index counter Recursively traverse the nested list using getVal() If element is integer, add to result list If ...

Read More

Course Schedule in Python

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

The Course Schedule problem asks whether we can complete all courses given their prerequisites. This is essentially a cycle detection problem in a directed graph where courses are nodes and prerequisites are edges. Problem Understanding Given a number of courses and prerequisite pairs, we need to determine if it's possible to finish all courses. For example, if we have 2 courses and prerequisites = [[1, 0]], it means to take course 1, we must first complete course 0. This is possible, so we return True. Algorithm Approach We solve this using Depth-First Search (DFS) with cycle ...

Read More

Minimum Cost Tree From Leaf Values in Python

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

The Minimum Cost Tree From Leaf Values problem asks us to build a binary tree from an array where each leaf corresponds to array elements in order, and each internal node's value equals the product of the maximum leaf values in its left and right subtrees. We need to find the minimum sum of all internal node values. Given an array of positive integers, we must construct binary trees such that: Each node has either 0 or 2 children Array values correspond to leaf values in an inorder traversal Each non-leaf node equals the product of the ...

Read More

Maximum Nesting Depth of Two Valid Parentheses Strings in Python

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

A Valid Parentheses String (VPS) consists only of "(" and ")" characters and satisfies specific structural properties. The goal is to split a VPS into two disjoint subsequences A and B such that the maximum nesting depth is minimized. Understanding Valid Parentheses Strings A string is a Valid Parentheses String if ? It is the empty string, or It can be written as AB, where A and B are VPS's, or It can be written as (A), where A is a VPS. Nesting Depth Definition The nesting depth of a VPS is defined ...

Read More

Delete Nodes And Return Forest in Python

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

When working with binary trees, we sometimes need to delete specific nodes and return the remaining forest of trees. This problem requires us to remove nodes with values in a given list and return the roots of all remaining subtrees. Problem Understanding Given a binary tree and a list of values to delete, we need to ? Remove all nodes with values in the to_delete list Return the roots of all remaining subtrees (the forest) When a node is deleted, its children become new roots if they exist ...

Read More

Filling Bookcase Shelves in Python

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

The bookshelf filling problem involves arranging books with given thickness and height onto shelves of limited width while maintaining their original order. We need to find the minimum possible total height of the bookcase. Each book has dimensions books[i][0] (thickness) and books[i][1] (height). Books must be placed in the given order, and each shelf has a maximum width of shelf_width. 1, 1 2, 3 2, 3 ...

Read More

Path With Maximum Minimum Value in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 528 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 258 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 345 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
Showing 171–180 of 25,469 articles
« Prev 1 16 17 18 19 20 2547 Next »
Advertisements