Weekly Contest 129
1020. Partition Array Into Three Parts With Equal Sum
Given an array
A
of integers, returntrue
if and only if we can partition the array into three non-empty parts with equal sums.Formally, we can partition the array if we can find indexes
i+1 < j
with(A[0] + A[1] + ... + A[i] == A[i+1] + A[i+2] + ... + A[j-1] == A[j] + A[j-1] + ... + A[A.length - 1])
Example 1:
Input: [0,2,1,-6,6,-7,9,1,2,0,1] Output: true Explanation: 0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1
Example 2:
Input: [0,2,1,-6,6,7,9,-1,2,0,1] Output: false
Example 3:
Input: [3,3,6,5,-2,2,5,1,-9,4] Output: true Explanation: 3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4
Note:
3 <= A.length <= 50000
-10000 <= A[i] <= 10000
Approach #1:
class Solution { public: bool canThreePartsEqualSum(vector<int>& A) { int len = A.size(); vector<int> sums(len, 0); unordered_map<int, int> m_; sums[0] = A[0]; m_[sums[0]] = 0; for (int i = 1; i < len; ++i) { sums[i] = sums[i-1] + A[i]; m_[sums[i]] = i; } for (int i = 0; i < len-2; ++i) { int s, m, e; if (m_.find(2*sums[i]) != m_.end() && m_.find(3*sums[i]) != m_.end()) { s = i, m = m_[2*sums[i]], e = m_[3*sums[i]]; if (s < m && m < e && e == len-1) return true; } } return false; } };
1022. Smallest Integer Divisible by K
Given a positive integer
K
, you need find the smallest positive integerN
such thatN
is divisible byK
, andN
only contains the digit 1.Return the length of
N
. If there is no suchN
, return -1.
Example 1:
Input: 1 Output: 1 Explanation: The smallest answer is N = 1, which has length 1.Example 2:
Input: 2 Output: -1 Explanation: There is no such positive integer N divisible by 2.Example 3:
Input: 3 Output: 3 Explanation: The smallest answer is N = 111, which has length 3.
Note:
1 <= K <= 10^5
Approach #1:
class Solution { public: int smallestRepunitDivByK(int K) { if (K % 2 == 0) return -1; int last = 0; int temp = 0; for (int i = 1; i <= K; ++i) { temp = last; temp = temp * 10 + 1; temp = temp % K; if (temp % K == 0) return i; last = temp; } return -1; } };
1021. Best Sightseeing Pair
Given an array
A
of positive integers,A[i]
represents the value of thei
-th sightseeing spot, and two sightseeing spotsi
andj
have distancej - i
between them.The score of a pair (
i < j
) of sightseeing spots is (A[i] + A[j] + i - j)
: the sum of the values of the sightseeing spots, minus the distance between them.Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6] Output: 11 Explanation: i = 0, j = 2,
A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
Approach #1:
class Solution { public: int maxScoreSightseeingPair(vector<int>& A) { int res = 0, cur = 0; for (int a : A) { res = max(res, cur + a); cur = max(cur, a) - 1; } return res; } };
1023. Binary String With Substrings Representing 1 To N
Given a binary string
S
(a string consisting only of '0' and '1's) and a positive integerN
, return true if and only if for every integer X from 1 to N, the binary representation of X is a substring of S.
Example 1:
Input: S = "0110", N = 3 Output: true
Example 2:
Input: S = "0110", N = 4 Output: false
Note:
1 <= S.length <= 1000
1 <= N <= 10^9
Approach #1:
class Solution { public: bool queryString(string S, int N) { vector<bool> seen(N, false); for (int i = 0; i < S.length(); ++i) { for (auto j = i, num = 0; num <= N && j < S.length(); ++j) { num = (num << 1) + S[j] - '0'; if (num > 0 && num <= N) seen[num-1] = true; } } return all_of(seen.begin(), seen.end(), [](bool s) { return s; }); } };