Posts

Showing posts from June, 2026

LC Trick to Prevent TLE: Static Variables

The solution to the problem below without the static "memo" (inspired by DP memoization) leads to TLE. Since the solution for test case N can be leveraged by test cases M (where M>N), you can use a static variable to cache some of the previously seen results. This is perfectly legal in LC land. Code is down below, cheers, ACC. Minimum Cost to Split into Ones II - LeetCode You are given an integer n . In one operation, you may split an integer x into two positive integers a and b such that a + b = x . The cost of this operation is a * b . Return the minimum total cost required to split the integer n into n ones.   Example 1: Input: n = 3 Output: 3 Explanation: One optimal set of operations is: x a b a + b a * b Cost 3 1 2 3 2 2 2 1 1 2 1 1 Thus, the minimum total cost is 2 + 1 = 3 . Example 2: Input: n = 4 Output: 6 Explanation:​​​​​​​ One optimal set of operations is: x a b a + b a * b Cost 4 2 2 4 4 4 2 1 1 2 1 1 Thus, the minimum total cost is 4 + 1 + 1 = 6 .  ...

A non-recursive trick for subsets generation IV

Another problem that requires subset generation, and the same non-recursive technique is applied: go from 0 to 2^N-1, creating the subsets. N=12 so very small. I could have used StringBuilder to reduce the mem and processing pressure, but string worked fine too. Code is down below, cheers, ACC. Valid Binary Strings With Cost Limit - LeetCode You are given two integers n and k . The cost of a binary string s is defined as the sum of all indices i (0-based) such that s[i] == '1' . A binary string is considered valid if: It does not contain two consecutive '1' characters. Its cost is less than or equal to k . Return a list of all valid binary strings of length n in any order.   Example 1: Input: n = 3, k = 1 Output: ["000","010","100"] Explanation: The binary strings of length 3 without consecutive '1' characters are: "000" : cost = 0 " 100" : cost = 0 "010" : cost = 1 "001" : cost...