Filling Bookcase Shelves Problem

Problem You are given an array books where books[i] = [thicknessi, heighti] indicates the thickness and height of the ith book. You are also given an integer shelfWidth. We want to place these books in order onto bookcase shelves that have a total width shelfWidth. We choose some of the books to place on this shelf such that the sum of their thickness is less than or equal to shelfWidth, then build another level of the shelf of the bookcase so that the total height of the bookcase has increased by the maximum height of the books we just put down. We repeat this process until there are no more books to place. ...

Minimum Deletions to Make String Balanced Problem

Problem You are given a string s consisting only of characters 'a' and 'b'​​​​. You can delete any number of characters in s to make s balanced. s is balanced if there is no pair of indices (i,j) such that i < j and s[i] = 'b' and s[j]= 'a'. Return the minimum number of deletions needed to make s balanced. Examples Example 1: Input: s = "aababbab" Output: 2 Explanation: You can either: Delete the characters at 0-indexed positions 2 and 6 ("aa{b}abb{a}b" -> "aaabbb"), or Delete the characters at 0-i ndexed positions 3 and 6 ("aab{a}bb{a}b" -> "aabbbb"). ...

Count Number of Teams Problem

Problem There are n soldiers standing in a line. Each soldier is assigned a unique rating value. You have to form a team of 3 soldiers amongst them under the following rules: Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]). A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n). Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams). ...

Maximum Score Words Formed by Letters Problem

Problem Given a list of words, list of single letters (might be repeating) and score of every character. Return the maximum score of any valid set of words formed by using the given letters (words[i] cannot be used two or more times). It is not necessary to use all characters in letters and each letter can only be used once. Score of letters 'a', 'b', 'c', … ,'z' is given by score[0], score[1], … , score[25] respectively. ...

Calculate the Binomial Coefficient

Problem Given two parameters n and k and returns the value of Binomial Coefficient C(n, k). Definition Binomial coefficients, denoted by C(n, k), have two common interpretations: the coefficient of X^k in the expansion of (1 + X)^n. For eg., in (1 + X)^3, C(3, 2) gives you the coefficient of the term X^2. Expanding the expression, we see 3X^2, and indeed, C(3, 2) is equal to 3. the number of ways to choose k objects from a set of n, regardless of order. Formula $$ C(n, k) \text{ OR } \binom{n}{k} = \frac{n!}{k! * (n - k)!} $$ ...

Largest Rectangle in Histogram Problem

Problem Given an array of integers heights representing the histogram’s bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram. Examples Example 1: Input: heights = [2,1,5,6,2,3] Output: 10 Explanation: The above is a histogram where width of each bar is 1. The largest rectangle is shown in the red area, which has an area = 10 units. ...

Least Number of Perfect Squares that Sums to n

Problem Given an integer n, return the least number of perfect square numbers that sum to n. A perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, 1, 4, 9, and 16 are perfect squares while 3 and 11 are not. Examples Example 1: Input: n = 12 Output: 3 Explanation: 12 = 4 + 4 + 4. ...

Longest arithmetic progression in a sorted array

Problem Given a sorted array, find the longest arithmetic progression in the same. Solution Method 1 - Dynamic Programming Before solving this problem, let us solve a different problem first. How to find if a sorted array contains an arithmetic progression of length 3? This can be done by looping over the array and then finding numbers ‘i’ and ‘k’ on either side of current number ‘j’ such that ...

Partition to K Equal Sum Subsets Problem

Problem Given an integer array nums and an integer k, return true if it is possible to divide this array into k non-empty subsets whose sums are all equal. Examples Example 1: Input: nums = [4,3,2,3,5,2,1], k = 4 Output: true Explanation: It is possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums. ...

Select Copy Paste Problem 1 - Minimum operations

Problem There is only one character 'A' on the screen of a notepad. You can perform one of two operations on this notepad for each step: Copy All: You can copy all the characters present on the screen (a partial copy is not allowed). Paste: You can paste the characters which are copied last time. Given an integer n, return the minimum number of operations to get the character 'A' exactly n times on the screen. ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy