Skip to content

Commit ad37caa

Browse files
committed
2312 solved.
1 parent ecf6fc9 commit ad37caa

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(D)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(D main.cpp)
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/// Source : https://leetcode.com/problems/selling-pieces-of-wood/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-06-18
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <map>
8+
9+
using namespace std;
10+
11+
12+
/// DP
13+
/// Time Complexity: O(m * n * n + n * m * m + m * n * (m + n))
14+
/// Space Complexity: O(m * n)
15+
class Solution {
16+
public:
17+
long long sellingWood(int m, int n, vector<vector<int>>& prices) {
18+
19+
// 0 - any, 1 - h is fixed, 2 - w is fixed
20+
vector<vector<vector<long long>>> dp(3, vector<vector<long long>>(m + 1, vector<long long>(n + 1, -1)));
21+
22+
map<int, map<int, long long>> H, W;
23+
for(const vector<int>& price: prices){
24+
int h = price[0], w = price[1], v = price[2];
25+
H[h][w] = max(H[h][w], (long long)v);
26+
W[w][h] = max(W[w][h], (long long)v);
27+
}
28+
29+
vector<int> h_choices;
30+
vector<vector<long long>> Hdp(m + 1, vector<long long>(n + 1, 0));
31+
for(const pair<int, map<int, long long>>& p: H){
32+
int h = p.first;
33+
h_choices.push_back(h);
34+
const map<int, long long>& items = p.second;
35+
36+
for(int W = 1; W <= n; W ++){
37+
for(const pair<int, long long>& item: items){
38+
int weight = item.first;
39+
long long value = item.second;
40+
41+
if(W >= weight)
42+
Hdp[h][W] = max(Hdp[h][W], Hdp[h][W - weight] + value);
43+
}
44+
}
45+
}
46+
47+
vector<int> w_choices;
48+
vector<vector<long long>> Wdp(n + 1, vector<long long>(m + 1, 0));
49+
for(const pair<int, map<int, long long>>& p: W){
50+
int w = p.first;
51+
w_choices.push_back(w);
52+
const map<int, long long>& items = p.second;
53+
54+
for(int W = 1; W <= m; W ++) {
55+
for (const pair<int, long long> &item: items) {
56+
int weight = item.first;
57+
long long value = item.second;
58+
59+
if(W >= weight)
60+
Wdp[w][W] = max(Wdp[w][W], Wdp[w][W - weight] + value);
61+
}
62+
}
63+
}
64+
65+
return dfs(0, m, n, h_choices, w_choices, Hdp, Wdp, dp);
66+
}
67+
68+
private:
69+
long long dfs(int state, int m, int n, const vector<int>& h_choices, const vector<int>& w_choices,
70+
const vector<vector<long long>>& Hdp, const vector<vector<long long>>& Wdp,
71+
vector<vector<vector<long long>>>& dp){
72+
73+
if(m <= 0 || n <= 0) return 0;
74+
if(dp[state][m][n] != -1) return dp[state][m][n];
75+
76+
if(state == 1) return dp[state][m][n] = Hdp[m][n];
77+
if(state == 2) return dp[state][m][n] = Wdp[n][m];
78+
79+
long long res = 0;
80+
for(int h_option: h_choices){
81+
if(h_option > m) break;
82+
res = max(res, dfs(1, h_option, n, h_choices, w_choices, Hdp, Wdp, dp) + dfs(0, m - h_option, n, h_choices, w_choices, Hdp, Wdp, dp));
83+
}
84+
for(int w_option: w_choices){
85+
if(w_option > n) break;
86+
res = max(res, dfs(2, m, w_option, h_choices, w_choices, Hdp, Wdp, dp) + dfs(0, m, n - w_option, h_choices, w_choices, Hdp, Wdp, dp));
87+
}
88+
return dp[state][m][n] = res;
89+
}
90+
};
91+
92+
93+
int main() {
94+
95+
vector<vector<int>> prices1 = {{1, 4, 2}, {2, 2, 7}, {2, 1, 3}};
96+
cout << Solution().sellingWood(3, 5, prices1) << '\n';
97+
// 19
98+
99+
vector<vector<int>> prices2 = {{3, 2, 10}, {1, 4, 2}, {4, 1, 3}};
100+
cout << Solution().sellingWood(4, 6, prices2) << '\n';
101+
// 32
102+
103+
vector<vector<int>> prices3 = {{4, 1, 18}, {4, 2, 5}, {1, 1, 20}, {3, 1, 21}};
104+
cout << Solution().sellingWood(4, 2, prices3) << '\n';
105+
// 160
106+
107+
return 0;
108+
}

readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,6 +2151,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
21512151
| 2305 | [Fair Distribution of Cookies](https://leetcode.com/problems/fair-distribution-of-cookies/) | [] | [C++](2001-2500/2305-Fair-Distribution-of-Cookies/cpp-2305/) | | |
21522152
| 2306 | [Naming a Company](https://leetcode.com/problems/naming-a-company/) | [] | [C++](2001-2500/2306-Naming-a-Company/cpp-2306/) | | |
21532153
| | | | | | |
2154+
| 2308 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - |
2155+
| | | | | | |
2156+
| 2312 | [Selling Pieces of Wood](https://leetcode.com/problems/selling-pieces-of-wood/) | [] | [C++](2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/) | | |
2157+
| | | | | | |
21542158

21552159
## 力扣中文站比赛
21562160

0 commit comments

Comments
 (0)