leetcode
-
Number of 1 Bits
Number of 1 Bits: use the division technique - divide the initial number by 2, keep the resulting integer and remainder. The remainder will be either 0 or 1, that is the associated bit. Divide the resulting integer and repeat the previous steps until this integer is equal to 0. Collect the bits - they represent the binary for the number.
-
Best Time to Buy and Sell Stock
Best Time to Buy and Sell Stock: either use a brute force solution (computing the profit between every pair of days), which results in quadratic complexity, or use two pointers to track the minimum price and the potential selling price; update the profit whenever a higher value is found (linear complexity).
-
Two Sum
Two Sum: either use a brute force solution (ensuring that the two numbers are not the same), which results in quadratic complexity, or use a hashmap (a dictionary in Python) to store the numbers and their indices, and check if the difference between the current value and the target value is already in the dictionary (linear complexity).