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
Programming Articles
Page 558 of 2547
Lemonade 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 MoreLargest Triangle Area in Python
Given a list of points on a plane, we need to find the area of the largest triangle that can be formed by any 3 of the points. This problem requires checking all possible combinations of three points and calculating their triangle areas. So, if the input is like [[0, 0], [0, 1], [1, 0], [0, 2], [2, 0]], then the output will be 2.0 ...
Read MoreNumber of Lines To Write String in Python
When writing a string onto lines with a maximum width of 100 units, we need to determine how many lines are required and the width used by the last line. Each character has a specific width given in a widths array where widths[0] represents the width of 'a', widths[1] represents 'b', and so on. The problem asks us to find ? How many lines have at least one character from the string What is the width used by the last line Algorithm We follow these steps ? Initialize lines = 1 and ...
Read MoreUnique Morse Code Words in Python
In this problem, we need to find how many unique Morse code representations exist for a given list of words. Each letter maps to a specific Morse code pattern, and words with the same Morse code transformation are considered identical. The International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes. For example, "a" maps to ".-", "b" maps to "-...", "c" maps to "-.-.", and so on. Morse Code Mapping Here is the complete list of Morse code patterns for all 26 letters ? morse_codes ...
Read MorePrime Number of Set Bits in Binary Representation in Python
Suppose we have two integers L and R, we have to find the count of numbers in the range [L, R] (inclusive) having a prime number of set bits in their binary form. So, if the input is like L = 6 and R = 10, then the output will be 4, as there are 4 numbers 6(110), 7(111), 9(1001), 10(1010), all have prime number of set bits. Understanding Set Bits Set bits are the bits that are '1' in the binary representation of a number. For example: 6 in binary is 110, which has ...
Read MoreShortest Completing Word in Python
The Shortest Completing Word problem requires finding the shortest word from a dictionary that contains all letters from a given license plate. The word must include each letter at least as many times as it appears in the license plate, ignoring case and non-alphabetic characters. For example, if the license plate is "PP", the word "pile" doesn't work because it only has one 'P', but "topper" works because it has two 'P's. Problem Understanding Given a license plate string and a list of words, we need to: Extract only alphabetic characters from the license plate ...
Read MoreLargest Number At Least Twice of Others in Python
Given an integer array nums, we need to check whether the largest element is at least twice as large as every other number in the array. If it satisfies this condition, return the index of the largest element, otherwise return -1. So, if the input is like [3, 6, 1, 0], then the output will be 1, as 6 is the largest number, and for every other number in the array x, 6 is more than twice as big as x. Since the index of 6 is 1, the output is 1. Algorithm To solve this problem, ...
Read More