I realized that a number whose binary representation contains only set bits is of the form ( 2^k - 1 ) (like 1, 3, 7, 15, 31, …). So I can find the smallest such number that is greater than or equal to n. I start from 1 and keep generating numbers of this form until I find one ≥ n, then return it.
I start by identifying all indices where nums[i] == 0 — these are the possible starting positions. For each such index, I simulate the described process twice: once moving left and once moving right. During the simulation, I maintain a copy of the array so I can decrement values and reverse direction as required. If I exit the bounds of the array and all elements are 0, that starting position and direction count as a valid selection. I sum up all valid cases and return that count.
class Solution:
def countValidSelections(self, nums: List[int]) -> int:I scan the rows and count the number of security devices ('1') in each row. I only care about rows that contain at least one device. For every pair of consecutive non-empty rows (i.e., rows with devices and no device-containing rows between them) each device in the earlier row forms a beam with every device in the later row, so I add prev_count * curr_count to the answer and then update prev_count = curr_count. This gives the total beams in a single pass.
class Solution:
def numberOfBeams(self, bank: List[str]) -> int:I store the balances in a zero-indexed Python list but treat account numbers as 1-indexed. For every operation I first check that the account indices are within [1, n]. For withdraw and transfer I additionally check that the source account has enough balance. All updates are done in-place, and each operation runs in constant time.
class Bank:I observe that Hercy saves money in weekly cycles. Each week starts on Monday with an increasing base amount. The first week starts with 1, the second week starts with 2, and so on. For each full week, the total is the sum of 7 consecutive numbers starting from that base. So, for w full weeks, I can compute the sum using the arithmetic series formula. For the remaining r days (less than a week), I add the first r terms of the next week's sequence.
class Solution:
def totalMoney(self, n: int) -> int:I simply search upward from n+1 until I find a number that is numerically balanced. For each candidate number I count its digits; if any '0' appears I reject it immediately (because 0 would have to appear zero times) and otherwise I check that for every digit d present, its frequency equals d.
class Solution:
def nextBeautifulNumber(self, n: int) -> int:To solve this, I repeatedly reduced the string by replacing it with the sequence formed by taking the sum of each pair of adjacent digits modulo 10. I continued this process until only two digits remained. Then, I simply checked if those final two digits were equal—returning True if they were and False otherwise.
class Solution:
def hasSameDigits(self, s: str) -> bool:I start with X = 0. Each operation either increments (++X or X++) or decrements (--X or X--) the value of X by 1. So, I just iterate through the operations, adding 1 for "++" and subtracting 1 for "--".
