Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
minimum-cost-to-merge-stones.cpp 1.98 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2019-03-03 16:06 +08:00 . Create minimum-cost-to-merge-stones.cpp
// Time: O(n^3)
// Space: O(n^3)
// memoization (top-down dp)
class Solution {
private:
template <typename A, typename B, typename C>
struct TupleHash {
size_t operator()(const tuple<A, B, C>& p) const {
size_t seed = 0;
A a; B b; C c;
tie(a, b, c) = p;
seed ^= std::hash<A>{}(a) + 0x9e3779b9 + (seed<<6) + (seed>>2);
seed ^= std::hash<B>{}(b) + 0x9e3779b9 + (seed<<6) + (seed>>2);
seed ^= std::hash<C>{}(c) + 0x9e3779b9 + (seed<<6) + (seed>>2);
return seed;
}
};
using Lookup = unordered_map<tuple<int, int, int>, int, TupleHash<int, int, int>>;
public:
int mergeStones(vector<int>& stones, int K) {
if ((stones.size() - 1) % (K - 1)) {
return -1;
}
Lookup lookup;
vector<int> prefix(stones.size() + 1, 0);
partial_sum(stones.cbegin(), stones.cend(),
next(prefix.begin()), plus<int>());
int result = dp(prefix, K, 0, stones.size() - 1, 1, &lookup);
return result != numeric_limits<int>::max() ? result : 0;
}
private:
int dp(const vector<int>& prefix, int K, int i, int j, int k,
Lookup *lookup) {
if (lookup->count(make_tuple(i, j, k))) {
return (*lookup)[make_tuple(i, j, k)];
}
int result = 0;
if (i == j) {
result = k == 1 ? 0 : numeric_limits<int>::max();
} else {
if (k == 1) {
result = dp(prefix, K, i, j, K, lookup) +
prefix[j + 1] - prefix[i];
} else {
result = numeric_limits<int>::max();
for (int mid = i; mid < j; mid += K - 1) {
result = min(result, dp(prefix, K, i, mid, 1, lookup) +
dp(prefix, K, mid + 1, j, k - 1, lookup));
}
}
}
(*lookup)[make_tuple(i, j, k)] = result;
return result;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助