Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
split-array-with-same-average.cpp 1.08 KB
Copy Edit Raw Blame History
kamyu authored 2018-03-26 22:04 +08:00 . Create split-array-with-same-average.cpp
// Time: O(n^4)
// Space: O(n^3)
class Solution {
public:
bool splitArraySameAverage(vector<int>& A) {
const int n = A.size();
const int sum = accumulate(A.cbegin(), A.cend(), 0);
if (!possible(n, sum)) {
return false;
}
vector<unordered_set<int>> sums(n / 2 + 1);
sums[0].emplace(0);
for (const auto& num: A) { // O(n) times
for (int i = n / 2; i >= 1; --i) { // O(n) times
for (const auto& prev : sums[i - 1]) { // O(1) + O(2) + ... O(n/2) = O(n^2) times
sums[i].emplace(prev + num);
}
}
}
for (int i = 1; i <= n / 2; ++i) {
if (sum * i % n == 0 &&
sums[i].count(sum * i / n)) {
return true;
}
}
return false;
}
private:
bool possible(int n, int sum) {
for (int i = 1; i <= n / 2; ++i) {
if (sum * i % n == 0) {
return true;
}
}
return false;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

Search