Count Submatrices with Top-Left Element and Sum Less Than k Solution In C++/Python/java/JS Problem Description: You are given a 0-indexed integer matrix grid and an integer k. Return the number of submatrices that contain the top-left element of the grid, and have a sum less than or equal to k. Examples: Input: grid = [[7,6,3],[6,6,1]], k = 18 Output: 4
Sliding Window Subarrays with K Different Integers Solution In C++/Java/Python/JS Problem Description Given an integer array nums and an integer k, return the number of good subarrays of nums. A good array is an array where the number of different integers in that array is exactly k. For example, [1,2,3,1,2] has 3 different integers: 1, 2,
Frequency of the Most Frequent Element Solution In C++/Java/Python/JS Problem Description: The frequency of an element is the number of times it occurs in an array. You are given an integer array nums and an integer k. In one operation, you can choose an index of nums and increment the element at that index by 1. Return the maximum
Reverse Words in a String III Solution In C++/Java/Python/JS Problem Description: Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Examples: Input: s = "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc" Input: s = "Mr Ding&
Reverse Words in a String Solution In C++/Java/Python/JS Problem Description: Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space. Return a string of the words in reverse order, concatenated by a single space. Note
Check if an Integer is a Power of 2/3/4/5... Solution In C++/Java/Python/JS Problem Description: Given an integer n, check if it is a power of 2, 3, 4, 5.... Return True if it is, otherwise return False. Examples: Input: n = 16 Output: True Explanation: 16 is a power of 2 and 4. Input: n = 27 Output: True Explanation: 27 is a power
Find in Mountain Array Solution In C++/Java/Python/JS Problem Description: (This problem is an interactive problem.) You may recall that an array arr is a mountain array if and only if: * arr.length >= 3 * There exists some i with 0 < i < arr.length - 1 such that: * arr[0] < arr[1] < ... < arr[
Find Minimum in Rotated Sorted Array II Solution In C++/Java/Python/JS Problem Description: Suppose an array of length n, sorted in ascending order, is rotated between 1 and n times. For example, the array nums = [0,1,4,4,5,6,7] might become: * [4,5,6,7,0,1,4] if it was rotated 4 times. * [0,1,4,4,
Most Beautiful Item for Each Query Solution In C++/Java/Python/JS Problem Description: You are given a 2D integer array items where items[i] = [priceᵢ, beautyᵢ] denotes the price and beauty of an item respectively. You are also given a 0-indexed integer array queries. For each queries[j], you want to determine the maximum beauty of an item whose price is
Online Election Solution In C++/Java/Python/JS Problem Description: You are given two integer arrays persons and times. In an election, the ith vote was cast for persons[i] at time times[i]. For each query at a time t, find the person that was leading the election at time t. Votes cast at time t will
Find Target Indices After Sorting Array Solution In C++/Java/Python/JS Problem Description: You are given a 0-indexed integer array nums and a target element target. A target index is an index i such that nums[i] == target. Return a list of the target indices of nums after sorting nums in non-decreasing order. If there are no target indices, return an
Search Insert Position Solution In C++/Java/Python/JS Problem Description: Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You must write an algorithm with O(log n) runtime complexity. Examples: Input: nums
Find the Distance Value Between Two Arrays Solution In C++/Java/Python/JS Problem Description: Given two integer arrays arr1 and arr2, and the integer d, return the distance value between the two arrays. The distance value is defined as the number of elements arr1[i] such that there is not any element arr2[j] where | arr1[i]-arr2[j] | <= d. Examples:
First Bad Version Solution in C++/Java/Python/JS Problem Description: You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad. Suppose you
Find Smallest Letter Greater Than Target Solution in C++/Java/Python/JS Problem Description: You are given an array of characters letters that is sorted in non-decreasing order, and a character target. There are at least two different characters in letters. Return the smallest character in letters that is lexicographically greater than target. If such a character does not exist, return the
[C++] References in C++ Hey there, future coding wizards! Ever felt like you were wrestling with C++, trying to get variables to play nice and cooperate? Well, get ready to meet a game-changer: References! Think of references as nicknames for your variables. They're aliases, secret identities, that let you work with the
[C++] Break Continue Goto Statements Alright, future C++ wizards! Get ready to level up your coding game because today we're diving headfirst into the exciting world of jump statements! Think of them as the secret agents of your code, allowing you to control the flow of execution and bend the rules in ways
[C++] Nested Loops and Variations Diving Deep with C++ Nested Loops: Your Beginner-Friendly Guide! So, you're ready to level up your C++ game and explore the fascinating world of nested loops? Awesome! You've come to the right place. Think of loops as magical repeaters, and nested loops? Well, they're
[C++] Loops and its Types Imagine you have an obedient robot which does your household chores: you want it to repeat the same action, like sweeping the floor, again and again without having to yell “sweep more!” every single time. Loops in C++ let you automate repetitive tasks by telling the computer: “Hey, keep doing
Grumpy Bookstore Owner Solution In C++/Java/Python/JS Problem Description: There is a bookstore owner that has a store open for n minutes. You are given an integer array customers of length n where customers[i] is the ,of the customers that enter the store at the start of the ith minute and all those customers leave after
Fruit Into Baskets Solution In C++/Java/Python/JS Problem Description: You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array fruits where fruits[i] is the type of fruit the ith tree produces. You want to collect as much fruit as possible.
Binary Search Arranging Coins Solution In C++/Java/Python/JS Problem Description: You have n coins and you want to build a staircase with these coins. The staircase consists of k rows where the ith row has exactly i coins. The last row of the staircase may be incomplete. Given the integer n, return the number of complete rows of
Binary Search Longest Subsequence with Limited Sum Solution in C++/Java/Python/JS Problem Description: You are given an integer array nums of length n, and an integer array queries of length m. Return an array answer of length m where answer[i] is the maximum size of a subsequence that you can take from nums such that the sum of its elements
Binary Search Special Array with X Elements Greater Than or Equal X Solution In C++/Java/Python/JS Problem Description: You are given an array nums of non-negative integers. nums is considered special if there exists a number x such that there are exactly x numbers in nums that are greater than or equal to x. Notice that x does not have to be an element in nums.
Find First and Last Position of Element in Sorted Array Solution | Code in C++/Java/Python/JS Problem Description: Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value. If target is not found in the array, return [-1, -1]. You must write an algorithm with O(log n) runtime complexity. Examples: Input: nums = [5,7,