Posts

Showing posts from February, 2025

1079. Letter Tile Possibilities

You have  n    tiles , where each tile has one letter  tiles[i]  printed on it. Return  the number of possible non-empty sequences of letters  you can make using the letters printed on those  tiles .   Example 1: Input: tiles = "AAB" Output: 8 Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA". Example 2: Input: tiles = "AAABBC" Output: 188 Example 3: Input: tiles = "V" Output: 1   Constraints: 1 <= tiles.length <= 7 tiles  consists of uppercase English letters. JAVA class Solution { public int numTilePossibilities ( String tiles ) { int [] cnt = new int [ 26 ]; for ( char c : tiles . toCharArray ()) { ++ cnt [ c - 'A' ]; } return dfs ( cnt ); } private int dfs ( int [] cnt ) { int res = 0 ; for ( int i = 0 ; i...

2698. Find the Punishment Number of an Integer

Given a positive integer  n , return  the  punishment number  of  n . The  punishment number  of  n  is defined as the sum of the squares of all integers  i  such that: 1 <= i <= n The decimal representation of  i * i  can be partitioned into contiguous substrings such that the sum of the integer values of these substrings equals  i .   Example 1: Input: n = 10 Output: 182 Explanation: There are exactly 3 integers i that satisfy the conditions in the statement: - 1 since 1 * 1 = 1 - 9 since 9 * 9 = 81 and 81 can be partitioned into 8 + 1. - 10 since 10 * 10 = 100 and 100 can be partitioned into 10 + 0. Hence, the punishment number of 10 is 1 + 81 + 100 = 182 Example 2: Input: n = 37 Output: 1478 Explanation: There are exactly 4 integers i that satisfy the conditions in the statement: - 1 since 1 * 1 = 1. - 9 since 9 * 9 = 81 and 81 can be partitioned into 8 + 1. - 10 since 10 * 10 = 100 and 100 can ...

1352. Product of the Last K Numbers

Design an algorithm that accepts a stream of integers and retrieves the product of the last  k  integers of the stream. Implement the  ProductOfNumbers  class: ProductOfNumbers()  Initializes the object with an empty stream. void add(int num)  Appends the integer  num  to the stream. int getProduct(int k)  Returns the product of the last  k  numbers in the current list. You can assume that always the current list has at least  k  numbers. The test cases are generated so that, at any time, the product of any contiguous sequence of numbers will fit into a single 32-bit integer without overflowing.   Example: Input ["ProductOfNumbers","add","add","add","add","add","getProduct","getProduct","getProduct","add","getProduct"] [[],[3],[0],[2],[5],[4],[2],[3],[4],[8],[2]] Output [null,null,null,null,null,null,20,40,0,null,32] Explanation ProductOfNumbers productOfNumbers = new Prod...

3066. Minimum Operations to Exceed Threshold Value II

You are given a  0-indexed  integer array  nums , and an integer  k . In one operation, you will: Take the two smallest integers  x  and  y  in  nums . Remove  x  and  y  from  nums . Add  min(x, y) * 2 + max(x, y)  anywhere in the array. Note  that you can only apply the described operation if  nums  contains at least two elements. Return  the  minimum  number of operations needed so that all elements of the array are greater than or equal to   k .   Example 1: Input: nums = [2,11,10,1,3], k = 10 Output: 2 Explanation: In the first operation, we remove elements 1 and 2, then add 1 * 2 + 2 to nums. nums becomes equal to [4, 11, 10, 3]. In the second operation, we remove elements 3 and 4, then add 3 * 2 + 4 to nums. nums becomes equal to [10, 11, 10]. At this stage, all the elements of nums are greater than or equal to 10 so we can stop. It can be shown that 2 is the...