Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 39 of 377

Longest Valid Parentheses in Python

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

Finding the longest valid parentheses substring is a common problem that can be solved efficiently using a stack-based approach. Given a string containing only '(' and ')' characters, we need to find the length of the longest valid (well-formed) parentheses substring. For example, in the string "))(())())", the longest valid parentheses substring is "(())())" with length 6. Algorithm Approach We use a stack to track indices of unmatched parentheses ? Initialize a stack with −1 to handle edge cases For each character, if it's '(', push its index onto the stack If it's ')', check ...

Read More

Merge k Sorted Lists in Python

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

Merging k sorted lists is a classic algorithm problem. Given multiple sorted linked lists, we need to combine them into a single sorted list. Python's heapq module provides an efficient solution using a min-heap data structure. Problem Understanding Given k sorted linked lists like [1, 4, 5], [1, 3, 4], [2, 6], we need to merge them into one sorted list [1, 1, 2, 3, 4, 4, 5, 6]. Algorithm Steps Create a min-heap to store the smallest elements from each list Add the first node of each non-empty list to the heap Repeatedly extract ...

Read More

Compare Version Numbers in Python

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

Comparing version numbers is a common programming task. Python provides several ways to compare version strings like "1.0.1" and "1.2.3". When comparing versions, we return 1 if the first version is greater, -1 if it's smaller, and 0 if they're equal. Understanding Version Number Comparison Version numbers consist of numeric parts separated by dots. Each part represents a different level of revision ? Version "2.5" means the 5th second-level revision of the 2nd first-level revision Missing parts default to 0 (e.g., "1.2" is equivalent to "1.2.0.0...") Compare each part from left to right until finding a ...

Read More

Longest Well-Performing Interval in Python

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

The Longest Well-Performing Interval problem requires finding the longest subarray where tiring days (hours > 8) outnumber non-tiring days. We solve this using a prefix sum approach with a hashmap to track cumulative balance efficiently. Understanding the Problem A tiring day occurs when hours worked > 8. A well-performing interval is a subarray where tiring days strictly outnumber non-tiring days. We transform each day into +1 (tiring) or -1 (non-tiring) and find the longest subarray with positive sum. Algorithm Approach We use a prefix sum technique with the following key insights: Convert hours to ...

Read More

Corporate Flight Bookings in Python

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

The Corporate Flight Bookings problem involves calculating the total number of seats booked on each flight when given multiple booking ranges. Each booking specifies a range of flights and the number of seats to book across that range. Problem Understanding Given n flights labeled 1 to n, and a list of bookings where each booking [i, j, k] means k seats are booked from flight i to flight j (inclusive), we need to find the total seats booked on each flight. Example With bookings [[1, 2, 10], [2, 3, 20], [2, 5, 25]] and n = ...

Read More

Car Pooling in Python

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

Car pooling is a common algorithmic problem where we need to determine if a vehicle can accommodate all passenger trips without exceeding its capacity. The vehicle travels only eastward, picking up and dropping off passengers at specific locations. Problem Understanding Given a list of trips where each trip contains [num_passengers, start_location, end_location] and a vehicle capacity, we need to check if all trips can be completed without exceeding the capacity limit. Algorithm Approach We use a difference array technique to track passenger changes at each location ? Create an array to track passenger count ...

Read More

Largest Values From Labels in Python

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

The Largest Values From Labels problem involves selecting items from a collection to maximize the sum while respecting constraints on the total number of items and usage limits per label. Problem Statement Given a set of items where the i-th item has values[i] and labels[i], we need to find a subset S such that: |S| ≤ num_wanted For every label L, the number of items in S with label L is ≤ use_limit The goal is to find the largest possible sum of the subset S. ...

Read More

Letter Tile Possibilities in Python

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

Given a set of letter tiles, we need to find the number of possible non-empty sequences we can make using these tiles. For example, with tiles "AAB", we can form 8 different sequences: "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA". This problem uses backtracking with frequency counting to generate all possible permutations while avoiding duplicates. Algorithm Approach We use a depth-first search (DFS) approach with the following steps: Count the frequency of each letter in the input tiles For each recursive call, try using each available letter Backtrack by restoring the letter count after ...

Read More

Flip Columns For Maximum Number of Equal Rows in Python

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

Given a binary matrix of 0s and 1s, we can flip any columns to maximize the number of rows with equal values. Flipping a column changes all 0s to 1s and all 1s to 0s in that column. The goal is to find the maximum number of rows that can have identical values after optimal column flips. Original Matrix: 0 0 0 0 ...

Read More

Distant Barcodes in Python

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

In a warehouse with a row of barcodes, we need to rearrange them so that no two adjacent barcodes are the same. For example, if we have [1, 1, 1, 2, 2, 2], we want to output [2, 1, 2, 1, 2, 1]. The strategy is to place the most frequent barcode first at even positions (0, 2, 4...), then fill odd positions with remaining barcodes. Algorithm Steps Count frequency of each barcode Sort barcodes by frequency (ascending order) Place the most frequent barcode at even positions (0, 2, 4...) Place remaining barcodes at odd positions ...

Read More
Showing 381–390 of 3,768 articles
« Prev 1 37 38 39 40 41 377 Next »
Advertisements