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
Articles by Arnab Chakraborty
Page 144 of 377
Number of Recent Calls in Python
The RecentCounter class counts recent requests within a 3000 millisecond time window. When you call ping(t), it returns how many pings occurred in the range [t - 3000, t], including the current ping. Problem Overview We need to implement a class that: Maintains a sliding window of 3000 milliseconds Counts pings within this time range Efficiently removes outdated pings Algorithm Steps To solve this problem, we follow these steps − Initialize the class with an empty queue to store timestamps For each ping(t) call: Remove timestamps older than t - 3000 ...
Read MoreSmallest Range I in Python
The Smallest Range I problem asks us to find the minimum possible difference between the maximum and minimum values of an array after we're allowed to add any value between -K and K to each element. Given an array A of integers, for each integer A[i] we can choose any x with range [-K, K] and add x to A[i]. After this process, we get array B. We need to find the smallest possible difference between the maximum and minimum values of B. Problem Understanding Let's understand this with an example. If A = [0, 10] and ...
Read MoreGroups of Special-Equivalent Strings in Python
In Python, finding groups of special-equivalent strings involves understanding that two strings are special-equivalent if we can transform one into the other by swapping characters at even positions with each other, and characters at odd positions with each other. A special-equivalent group is a collection of strings where every pair in the group is special-equivalent, and the group cannot be made larger by adding another string from the array. Understanding Special-Equivalent Strings Two strings are special-equivalent if: Characters at even indices (0, 2, 4, ...) can be rearranged among themselves Characters at odd indices (1, 3, ...
Read MoreProjection Area of 3D Shapes
When we place cubes on a grid, we can calculate the projection area by viewing the 3D structure from three different angles: top view (xy-plane), front view (yz-plane), and side view (xz-plane). Each projection shows the shadow cast by the cubes onto that plane. Grid: [[1, 2], [3, 4]] 3D View: 1 ...
Read MoreWalking Robot Simulation in Python
A walking robot simulation problem involves a robot on an infinite grid starting at point (0, 0) facing north. The robot receives commands to turn or move forward while avoiding obstacles. Problem Description The robot can receive three types of commands: -2: Turn left 90 degrees -1: Turn right 90 degrees 1-9: Move forward x units Obstacles are represented as coordinates in an array. If the robot encounters an obstacle, it stops at the previous valid position. We need to find the ...
Read MoreBinary Gap in Python
A binary gap is the longest sequence of consecutive zeros that are surrounded by ones on both sides in the binary representation of a positive integer. We need to find the maximum distance between two consecutive 1's in the binary representation. For example, if the input is 22, the binary representation is 10110. There are gaps of length 1 (between positions 1 and 3) and length 2 would be the answer if we count the distance differently. Let's implement this step by step. Algorithm To solve this problem, we follow these steps: Convert the number ...
Read MoreLemonade Change in Python
The lemonade change problem is a classic greedy algorithm challenge. At a lemonade stand where each drink costs $5, customers pay with $5, $10, or $20 bills, and we must provide correct change. Starting with no money, we need to determine if we can serve all customers. Problem Analysis For each customer payment: $5 bill: No change needed, keep the bill $10 bill: Give back one $5 bill as change $20 bill: Give back $15 change (prefer one $10 + one $5, or three $5 bills) Algorithm Approach We use a greedy strategy, ...
Read MoreBuddy Strings in Python
Buddy strings are two strings where you can swap exactly two characters in one string to make it equal to the other string. Given two strings A and B, we need to determine if they are buddy strings. For example, if A = "ba" and B = "ab", we can swap positions 0 and 1 in string A to get "ab", which equals B. So the output will be True. Algorithm To solve this problem, we need to check several conditions: If lengths of A and B are different, return False If A and B ...
Read MoreFlipping an Image in Python
Flipping an image in Python involves two operations: horizontally reversing each row and then inverting the binary values (0 becomes 1, 1 becomes 0). This is commonly used in image processing and computer vision applications. Given a binary matrix representing an image, we need to flip it horizontally and then invert all values ? Input and Expected Output For the input matrix: 110 101 000 The expected output after flipping and inverting: 100 010 111 Algorithm Steps Create an empty result list For each row in the ...
Read MorePositions of Large Groups in Python
Sometimes we need to find positions of consecutive character groups in a string that have 3 or more characters. For example, in the string "abbxxxxzyy", the groups are "a", "bb", "xxxx", "z", and "yy", where only "xxxx" qualifies as a large group. Problem Understanding Given a string of lowercase letters, we need to identify large groups (3+ consecutive identical characters) and return their starting and ending positions as a list of ranges. Approach We'll use Python's itertools.groupby() to group consecutive identical characters, then check if each group has 3 or more characters ? from ...
Read More