1012. Complement of Base 10 Integer
Every non-negative integerN
has a binary representation. For example,5
can be represented as"101"
in binary,11
as"1011"
in binary, and so on. Note that except forN = 0
, there are no leading zeroes in any binary representation.The complement of a binary representation is the number in binary you get when changing every
1
to a0
and0
to a1
. For example, the complement of"101"
in binary is"010"
in binary.For a given number
N
in base-10, return the complement of it's binary representation as a base-10 integer.Example 1:
Input: 5 Output: 2 Explanation: 5 is "101" in binary, with complement "010" in binary, which is 2 in base-10.
Example 2:
Input: 7 Output: 0 Explanation: 7 is "111" in binary, with complement "000" in binary, which is 0 in base-10.
Example 3:
Input: 10 Output: 5 Explanation: 10 is "1010" in binary, with complement "0101" in binary, which is 5 in base-10.
Note:
0 <= N < 10^9
Approach #1: Math. [C++]
class Solution { public: int bitwiseComplement(int N) { int X = 1; while (N > X) X = X * 2 + 1; return X ^ N; } };
Analysis:
Claim ----- The XOR operation evaluates the difference in the individual bits, i.e it gives information about whether the bits are identical or not.
Proof ----- It's easy once youknow the definition of XOR. 0^0 = 1^1 = 0 (as the bits don't differ), whereas 0^1 = 1^0 = 1 (as the bits are difference).
Claim ----- XOR of identical numbers is zero.
Proof ----- As argued above, the bits of identical numbers do not differ at any position. Hence, XOR is zero.
Claim ----- 0 XOR any number is the number itself.
Proof ----- XOR gives us the bit difference. Since all the bits in 0 are unset, therefore the difference in bits is the number itself.
Claim ----- XOR of a number with its complement results in a number with all set bits.
Proof ----- This is trivial, since bits of a number and its complement differ at every position(according to the definition of complement).
So, number ^ complement = all_set_bits ==> number ^ number ^ complement = number ^ all_set_bits ===> 0 ^ complement = number ^ all_set_bits
So, complement = number ^ all_set_bits.
So, we find out the number containing all the set bits and XOR with the original number to get the answer.
Reference:
1013. Pairs of Songs With Total Durations Divisible by 60
In a list of songs, the
i
-th song has a duration oftime[i]
seconds.Return the number of pairs of songs for which their total duration in seconds is divisible by
60
. Formally, we want the number of indicesi < j
with(time[i] + time[j]) % 60 == 0
.Example 1:
Input: [30,20,150,100,40] Output: 3 Explanation: Three pairs have a total duration divisible by 60: (time[0] = 30, time[2] = 150): total duration 180 (time[1] = 20, time[3] = 100): total duration 120 (time[1] = 20, time[4] = 40): total duration 60
Example 2:
Input: [60,60,60] Output: 3 Explanation: All three pairs have a total duration of 120, which is divisible by 60.
Note:
1 <= time.length <= 60000
1 <= time[i] <= 500
Approach #1: Brute force + Map. [C++]
class Solution { public: int numPairsDivisibleBy60(vector<int>& time) { int ans = 0; int len = time.size(); map<int, vector<int>> m; vector<int> duration = {60, 120, 180, 240, 300, 360, 420, 480, 540, 600, 660, 720, 780, 840, 900, 960, 1020}; for (int i = 0; i < len; ++i) m[time[i]].push_back(i); for (int i = 0; i < len; ++i) { for (int j = 0; j < duration.size(); ++j) { if (duration[j] - time[i] > 0) { int tmp = duration[j] - time[i]; if (m.count(tmp)) { int count = m[tmp].end() - upper_bound(m[tmp].begin(), m[tmp].end(), i); ans += count; } } } } return ans; } };
Approach #2: Orz.
int numPairsDivisibleBy60(vector<int>& time) { vector<int> c(60); int res = 0; for (int t : time) { res += c[(60 - t % 60) % 60]; c[t % 60] += 1; } return res; }
Analysis:
Calculate the time%60 then it will be exactly same as two sum problem.
Reference:
1014. Capacity To Ship Packages Within D Days
A conveyor belt has packages that must be shipped from one port to another within
D
days.The
i
-th package on the conveyor belt has a weight ofweights[i]
. Each day, we load the ship with packages on the conveyor belt (in the order given byweights
). We may not load more weight than the maximum weight capacity of the ship.Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within
D
days.Example 1:
Input: weights = [1,2,3,4,5,6,7,8,9,10], D = 5 Output: 15 Explanation: A ship capacity of 15 is the minimum to ship all the packages in 5 days like this: 1st day: 1, 2, 3, 4, 5 2nd day: 6, 7 3rd day: 8 4th day: 9 5th day: 10 Note that the cargo must be shipped in the order given, so using a ship of capacity 14 and splitting the packages into parts like (2, 3, 4, 5), (1, 6, 7), (8), (9), (10) is not allowed.
Example 2:
Input: weights = [3,2,2,4,1,4], D = 3 Output: 6 Explanation: A ship capacity of 6 is the minimum to ship all the packages in 3 days like this: 1st day: 3, 2 2nd day: 2, 4 3rd day: 1, 4
Example 3:
Input: weights = [1,2,3,1,1], D = 4 Output: 3 Explanation: 1st day: 1 2nd day: 2 3rd day: 3 4th day: 1, 1
Note:
1 <= D <= weights.length <= 50000
1 <= weights[i] <= 500
Approach #1: Binary search. [C++]
class Solution { public: int shipWithinDays(vector<int>& weights, int D) { int left = *max_element(weights.begin(), weights.end()); int right = 25000000; while (left < right) { int mid = (right + left) / 2; int need = 1, cur = 0; for (int i = 0; i < weights.size() && need <= D; cur += weights[i++]) { if (cur + weights[i] > mid) cur = 0, need++; } if (need > D) left = mid + 1; else right = mid; } return left; } };
Analysis:
Given the number of bags, return the minimum capacity of each bag, so that we can put items one by one into all bags.
Reference:
1015. Numbers With Repeated Digits
Given a positive integer N
, return the number of positive integers less than or equal to N
that have at least 1 repeated digit.
Example 1:
Input: 20
Output: 1
Explanation: The only positive number (<= 20) with at least 1 repeated digit is 11.
Example 2:
Input: 100
Output: 10
Explanation: The positive numbers (<= 100) with atleast 1 repeated digit are 11, 22, 33, 44, 55, 66, 77, 88, 99, and 100.
Example 3:
Input: 1000
Output: 262
Note:
1 <= N <= 10^9
Approach #1: