Watering Plants II Problem Description Alice and Bob want to water n plants in their garden. The plants are arranged in a row and are labeled from 0 to n - 1 from left to right where the ith plant is located at x = i. Each plant needs a specific amount of water.
Merge Sorted Array Problem Description You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively. Merge nums1 and nums2 into a single array sorted in non-decreasing order. The final sorted array should not be
Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the MinStack class: • MinStack() initializes the stack object. • void push(int val) pushes the element val onto the stack. • void pop() removes the element on the top of the stack. • int top() gets
Separate Black and White Balls Problem Description There are n balls on a table, each ball has a color black or white. You are given a 0-indexed binary string s of length n, where 1 and 0 represent black and white balls, respectively. In each step, you can choose two adjacent balls and swap them.
Reverse Prefix of Word Problem Description Given a 0-indexed string word and a character ch, reverse the segment of word that starts at index 0 and ends at the index of the first occurrence of ch (inclusive). If the character ch does not exist in word, do nothing. For example, if word = "abcdefd&
String Compression Problem Description Given an array of characters chars, compress it using the following algorithm: Begin with an empty string s. For each group of consecutive repeating characters in chars: * If the group's length is 1, append the character to s. * Otherwise, append the character followed by the group&
Greedy Minimum Cost to Move Chips to the Same Position Solution in C++/Java/Python/JS Problem Description We have n chips, where the position of the ith chip is position[i]. We need to move all the chips to the same position. In one step, we can change the position of the ith chip from position[i] to: 1) position[i] + 2 or position[i]
Container with Most Water Problem Description You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]). Find two lines that together with the x-axis form a container, such that the container contains
Merge Two 2D Arrays by Summing Values Problem Statement You are given two 2D integer arrays nums1 and nums2. nums1[i] = [idᵢ, valᵢ] indicate that the number with the idᵢ has a value equal to valᵢ. nums2[i] = [idᵢ, valᵢ] indicate that the number with the idᵢ has a value equal to valᵢ. Each array contains unique
Minimum Length of String After Deleting Similar Ends Problem Description Given a string s consisting only of characters 'a', 'b', and 'c'. You are asked to apply the following algorithm on the string any number of times: 1. Pick a non-empty prefix from the string s where all the characters in the
Count Beautiful Substrings II Solution In C++/Java/Python/JS Problem Description: You are given a string s and a positive integer k. Let vowels and consonants be the number of vowels and consonants in a string. A string is beautiful if: vowels == consonants. (vowels * consonants) % k == 0, in other terms the multiplication of vowels and consonants is divisible by
Check if a Parentheses String Can Be Valid Problem Statement A parentheses string is a non-empty string consisting only of '(' and ')'. It is valid if any of the following conditions is true: It is (). It can be written as AB (A concatenated with B), where A and B are valid parentheses strings. It can
Count the Number of Good Partitions Solution In C++/Python/Java/Javascript Problem Description: You are given a 0-indexed array nums consisting of positive integers. A partition of an array into one or more contiguous subarrays is called good if no two subarrays contain the same number. Return the total number of good partitions of nums. Since the answer may be large,
Remove Outermost Parentheses Problem Description A valid parentheses string is either empty "", "(" + A + ")", or A + B, where A and B are valid parentheses strings, and + represents string concatenation. For example, "", "()", "(())()", and "(()(()))" are all valid parentheses strings. A valid parentheses
Merge Strings Alternately Problem Description You are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional letters onto the end of the merged string. Return the merged string. Examples Input: word1 = "abc&
Rank Teams by Votes Problem Description: In a special ranking system, each voter gives a rank from highest to lowest to all teams participating in the competition. The ordering of teams is decided by who received the most position-one votes. If two or more teams tie in the first position, we consider the second
Reverse Substrings Between Each Pair of Parentheses Problem Description You are given a string s that consists of lower case English letters and brackets. Reverse the strings in each pair of matching parentheses, starting from the innermost one. Your result should not contain any brackets. Examples Input: s = "(abcd)" Output: "dcba" Input: s
Custom Sort String Solution In C++/Java/Python/Javascript Problem Description: You are given two strings order and s. All the characters of order are unique and were sorted in some custom order previously. Permute the characters of s so that they match the order that order was sorted. More specifically, if a character x occurs before a character
Sort the Jumbled Numbers Problem Description: You are given a 0-indexed integer array mapping which represents the mapping rule of a shuffled decimal system. mapping[i] = j means digit i should be mapped to digit j in this system. The mapped value of an integer is the new integer obtained by replacing each occurrence
Largest Number Problem Description: Given a list of non-negative integers nums, arrange them such that they form the largest number and return it. Since the result may be very large, so you need to return a string instead of an integer. Examples: Input: nums = [10,2] Output: "210" Explanation: Arrange
+5 Problems on Comparator Sort | Code C++/Python/Java Sorting is one of the cornerstones of computer science, enabling faster searches and streamlined data processing. Among the various sorting methods, comparator-based sorting stands out for its versatility and adaptability. However, despite its widespread use, it has its own set of challenges that can stump even seasoned programmers. This guide
Comparator Sort A comparator sort is a sorting algorithm that determines the order of elements in a list, array, or other data structure using a comparison function. This comparison function is applied to pairs of elements and decides their relative order. This comparison function is used to compare two elements at a
Maximum Nesting Depth of the Parentheses Given a valid parentheses string s, return the nesting depth of s. The nesting depth is the maximum number of nested parentheses. Examples What is a valid Parentheses ? If for each opening parentheses, there is a closing parentheses, then this pair of parentheses is known as a valid parentheses. For
2D-Arrays V So ,while learning about the Symmetrix matrix we had to revise our concepts on the Transpose of a matrix , the upper triangular matrix and the lower triangular matrix as well. Let's now learn about more such 2D-Arrays . Q13. Identity Matrix An identity matrix is a square matrix where,
2D-Arrays IV We’ve covered how to add and subtract two matrices, print the upper and lower triangles of a matrix, and print both the left and right diagonals. These fundamental matrix operations serve as the building blocks for more complex algorithms. But did we miss to traverse the 2d-array in the