forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimize-the-difference-between-target-and-chosen-elements.cpp
112 lines (108 loc) · 3.39 KB
/
minimize-the-difference-between-target-and-chosen-elements.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Time: O(max_t * m * n), max_t is max_target
// Space: O(max_t)
// optimized from solution2 (using bitset), runtime: 32 ms
class Solution {
public:
int minimizeTheDifference(vector<vector<int>>& mat, int target) {
static const int MAX_TARGET = 800;
int chosen_min = 0;
for (const auto& row : mat) {
chosen_min += *min_element(cbegin(row), cend(row));
}
if (chosen_min >= target) {
return chosen_min - target;
}
bitset<(2 * MAX_TARGET - 2) + 1> bs(1); // total < 2*target - 1
for (const auto& row : mat) {
bitset<(2 * MAX_TARGET - 2) + 1> new_bs;
for (const auto& x : row) {
new_bs |= bs << x;
}
bs = move(new_bs);
}
int result = size(bs);
for (int total = chosen_min; total < size(bs); ++total) {
if (!bs[total]) {
continue;
}
result = min(result, abs(target - total));
if (total > target) {
break;
}
}
return result;
}
};
// Time: O(t * m * n), t is target
// Space: O(t)
// optimized from solution_tle (not using unordered_set), runtime: 192 ms
class Solution2 {
public:
int minimizeTheDifference(vector<vector<int>>& mat, int target) {
int chosen_min = 0;
for (const auto& row : mat) {
chosen_min += *min_element(cbegin(row), cend(row));
}
if (chosen_min >= target) {
return chosen_min - target;
}
vector<uint8_t> dp(2 * target - chosen_min);
dp[0] = true;
for (const auto& row : mat) {
vector<uint8_t> new_dp(2 * target - chosen_min);
for (int total = 0; total < size(dp); ++total) {
if (!dp[total]) {
continue;
}
for (const auto& x : row) {
if ((total + x) - target < target - chosen_min) {
new_dp[total + x] = 1;
}
}
}
dp = move(new_dp);
}
int result = numeric_limits<int>::max();
for (int total = chosen_min; total < size(dp); ++total) {
if (!dp[total]) {
continue;
}
result = min(result, abs(target - total));
if (total > target) {
break;
}
}
return result;
}
};
// Time: O(t * m * n), t is target
// Space: O(t)
class Solution_TLE {
public:
int minimizeTheDifference(vector<vector<int>>& mat, int target) {
int chosen_min = 0;
for (const auto& row : mat) {
chosen_min += *min_element(cbegin(row), cend(row));
}
if (chosen_min >= target) {
return chosen_min - target;
}
unordered_set<int> dp = {0};
for (const auto& row : mat) {
unordered_set<int> new_dp;
for (const auto& total : dp) {
for (const auto& x : row) {
if ((total + x) - target < target - chosen_min) {
new_dp.emplace(total + x);
}
}
}
dp = move(new_dp);
}
int result = numeric_limits<int>::max();
for (const auto& total : dp) {
result = min(result, abs(target - total));
}
return result;
}
};