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 669 of 855
The Earliest Moment When Everyone Become Friends in C++
The problem asks us to find the earliest moment when everyone in a social group becomes acquainted with everyone else. This is a classic Union-Find (Disjoint Set Union) problem where we need to track connected components and determine when all people form a single connected group. Given N people with IDs from 0 to N-1 and a list of friendship logs, each log contains [timestamp, person_A, person_B]. We need to find the earliest timestamp when all people are connected through friendships. Algorithm Overview We use the Union-Find data structure with the following approach − Sort ...
Read MoreBroken Calculator in Python
A broken calculator problem involves finding the minimum operations needed to transform one number into another using only two allowed operations: double (multiply by 2) and decrement (subtract 1). Given an initial number X displayed on the calculator, we need to reach the target number Y using the minimum number of operations. Problem Understanding The calculator supports only two operations: Double − Multiply the current number by 2 Decrement − Subtract 1 from the current number For example, if X = 5 and Y = 8, we can reach 8 from 5 in ...
Read MoreInsert Delete GetRandom O(1) in Python
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 MoreFlatten Nested List Iterator in Python
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 MoreCourse Schedule in Python
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 MoreMinimum Cost Tree From Leaf Values in Python
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 MoreMaximum Nesting Depth of Two Valid Parentheses Strings in Python
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 MoreDelete Nodes And Return Forest in Python
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 MoreFilling Bookcase Shelves in Python
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 MorePath With Maximum Minimum Value in Python
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