Given an integer n, you must transform it into 0 using the following operations any number of times:
Change the rightmost (0th) bit in the binary representation of n.
Change the ith bit in the binary representation of n if the (i-1)th bit is set to 1 and the (i-2)th through 0th bits are set to 0.
Return the minimum number of operations to transform n into 0.
Example 1:
Input: n = 0
Output: 0
Example 2:
Input: n = 3
Output: 2
Explanation: The binary representation of 3 is "11".
"11" -> "01" with the 2nd operation since the 0th bit is 1.
"01" -> "00" with the 1st operation.
Example 3:
Input: n = 6
Output: 4
Explanation: The binary representation of 6 is "110".
"110" -> "010" with the 2nd operation since the 1st bit is 1 and 0th through 0th bits are 0.
"010" -> "011" with the 1st operation.
"011" -> "001" with the 2nd operation since the 0th bit is 1.
"001" -> "000" with the 1st operation.
Given an array of positive integers arr, calculate the sum of all possible odd-length subarrays.
A subarray is a contiguous subsequence of the array.
Return the sum of all odd-length subarrays of arr.
Example 1:
Input: arr = [1,4,2,5,3]
Output: 58
Explanation: The odd-length subarrays of arr and their sums are:
[1] = 1
[4] = 4
[2] = 2
[5] = 5
[3] = 3
[1,4,2] = 7
[4,2,5] = 11
[2,5,3] = 10
[1,4,2,5,3] = 15
If we add all these together we get 1 + 4 + 2 + 5 + 3 + 7 + 11 + 10 + 15 = 58
Example 2:
Input: arr = [1,2]
Output: 3
Explanation: There are only 2 subarrays of odd length, [1] and [2]. Their sum is 3.
Example 3:
Input: arr = [10,11,12]
Output: 66
Constraints:
1 <= arr.length <= 100
1 <= arr[i] <= 1000
Solution 0: Brute Force
Enumerate all odd length subarrys: O(n^2), each take O(n) to compute the sum.
Total time complexity: O(n^3) Space complexity: O(1)
Solution 1: Running Prefix Sum
Reduce the time complexity to O(n^2)
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution{
public:
intsumOddLengthSubarrays(vector<int>&arr){
constintn=arr.size();
intans=0;
for(inti=0,s=0;i<n;++i,s=0)
for(intj=i;j<n;++j){
s+=arr[j];
ans+=s*((j-i+1)&1);
}
returnans;
}
};
Solution 2: Math
Count how many times arr[i] can be in of an odd length subarray we chose the start, which can be 0, 1, 2, … i, i + 1 choices we chose the end, which can be i, i + 1, … n – 1, n – i choices Among those 1/2 are odd length. So there will be upper((i + 1) * (n – i) / 2) odd length subarrays contain arr[i]
ans = sum(((i + 1) * (n – i) + 1) / 2 * arr[i] for in range(n))
Given a binary string s (a string consisting only of ‘0’s and ‘1’s), we can split s into 3 non-empty strings s1, s2, s3 (s1+ s2+ s3 = s).
Return the number of ways s can be split such that the number of characters ‘1’ is the same in s1, s2, and s3.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: s = "10101"
Output: 4
Explanation: There are four ways to split s in 3 parts where each part contain the same number of letters '1'.
"1|010|1"
"1|01|01"
"10|10|1"
"10|1|01"
Example 2:
Input: s = "1001"
Output: 0
Example 3:
Input: s = "0000"
Output: 3
Explanation: There are three ways to split s in 3 parts.
"0|0|00"
"0|00|0"
"00|0|0"
Example 4:
Input: s = "100100010100110"
Output: 12
Constraints:
s[i] == '0' or s[i] == '1'
3 <= s.length <= 10^5
Solution: Counting
Count how many ones in the binary string as T, if not a factor of 3, then there is no answer.
Count how many positions that have prefix sum of T/3 as l, and how many positions that have prefix sum of T/3*2 as r.
Ans = l * r
But we need to special handle the all zero cases, which equals to C(n-2, 2) = (n – 1) * (n – 2) / 2
Given an array nums that represents a permutation of integers from 1 to n. We are going to construct a binary search tree (BST) by inserting the elements of nums in order into an initially empty BST. Find the number of different ways to reorder nums so that the constructed BST is identical to that formed from the original array nums.
For example, given nums = [2,1,3], we will have 2 as the root, 1 as a left child, and 3 as a right child. The array [2,3,1] also yields the same BST but [3,2,1] yields a different BST.
Return the number of ways to reordernumssuch that the BST formed is identical to the original BST formed fromnums.
Since the answer may be very large, return it modulo 10^9 + 7.
Example 1:
Input: nums = [2,1,3]
Output: 1
Explanation: We can reorder nums to be [2,3,1] which will yield the same BST. There are no other ways to reorder nums which will yield the same BST.
Example 2:
Input: nums = [3,4,5,1,2]
Output: 5
Explanation: The following 5 arrays will yield the same BST:
[3,1,2,4,5]
[3,1,4,2,5]
[3,1,4,5,2]
[3,4,1,2,5]
[3,4,1,5,2]
Example 3:
Input: nums = [1,2,3]
Output: 0
Explanation: There are no other orderings of nums that will yield the same BST.
Example 4:
Input: nums = [3,1,2,5,4,6]
Output: 19
Example 5:
Input: nums = [9,4,2,1,3,6,5,7,8,14,11,10,12,13,16,15,17,18]
Output: 216212978
Explanation: The number of ways to reorder nums to get the same BST is 3216212999. Taking this number modulo 10^9 + 7 gives 216212978.
Constraints:
1 <= nums.length <= 1000
1 <= nums[i] <= nums.length
All integers in nums are distinct.
Solution: Recursion + Combinatorics
For a given root (first element of the array), we can split the array into left children (nums[i] < nums[0]) and right children (nums[i] > nums[0]). Assuming there are l nodes for the left and r nodes for the right. We have C(l + r, l) different ways to insert l elements into a (l + r) sized array. Within node l / r nodes, we have ways(left) / ways(right) different ways to re-arrange those nodes. So the total # of ways is: C(l + r, l) * ways(l) * ways(r) Don’t forget to minus one for the final answer.
Given two non-negative integers low and high. Return the count of odd numbers between low and high (inclusive).
Example 1:
Input: low = 3, high = 7
Output: 3
Explanation: The odd numbers between 3 and 7 are [3,5,7].
Example 2:
Input: low = 8, high = 10
Output: 1
Explanation: The odd numbers between 8 and 10 are [9].
Constraints:
0 <= low <= high <= 10^9
Solution: Math
The count of odd numbers between [1, low – 1] is low / 2 e.g. low = 6, we have [1,3,5] in range [1, 5] and count is 6/2 = 3. The count of odd numbers between [1, high] is (high + 1) / 2 e.g. high = 7, we have [1,3,5,7] in range [1, 7] and count is (7+1) / 2 = 4
Then the count of odd numbers in range [low, high] = count(1, high) – count(1, low-1) e.g. in range [6, 7] we only have [7], count: 4 – 3 = 1