Skip to content

Commit f709e6e

Browse files
committed
1981 added.
1 parent 2136038 commit f709e6e

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-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.20)
2+
project(C)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(C main.cpp)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/// Source : https://leetcode.com/problems/minimize-the-difference-between-target-and-chosen-elements/
2+
/// Author : liuyubobobo
3+
/// Time : 2021-08-21
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
11+
/// Memoization
12+
/// Time Complexity: O(R * C * target)
13+
/// Space Complexity: O(R * target)
14+
class Solution {
15+
16+
private:
17+
int R, C;
18+
19+
public:
20+
int minimizeTheDifference(vector<vector<int>>& mat, int target) {
21+
22+
for(vector<int>& row: mat) sort(row.begin(), row.end());
23+
24+
R = mat.size(), C = mat[0].size();
25+
vector<vector<int>> dp(R, vector<int>(target + 1, -1));
26+
return dfs(mat, 0, target, dp);
27+
}
28+
29+
private:
30+
int dfs(const vector<vector<int>>& mat, int r, int t,
31+
vector<vector<int>>& dp){
32+
33+
if(r == R) return t;
34+
if(dp[r][t] != -1) return dp[r][t];
35+
36+
int res = INT_MAX;
37+
for(int j = 0; j < C; j ++){
38+
if(t >= mat[r][j])
39+
res = min(res, dfs(mat, r + 1, t - mat[r][j], dp));
40+
else{
41+
int tres = t - mat[r][j];
42+
for(int i = r + 1; i < R; i ++)
43+
tres -= mat[i][0];
44+
res = min(res, -tres);
45+
}
46+
}
47+
return dp[r][t] = res;
48+
}
49+
};
50+
51+
52+
int main() {
53+
54+
vector<vector<int>> mat1 = {
55+
{1,2,3},{4,5,6},{7,8,9}
56+
};
57+
cout << Solution().minimizeTheDifference(mat1, 13) << endl;
58+
// 0
59+
60+
vector<vector<int>> mat2 = {
61+
{1},{2},{3}
62+
};
63+
cout << Solution().minimizeTheDifference(mat2, 100) << endl;
64+
// 94
65+
66+
vector<vector<int>> mat3 = {
67+
{1,2,9,8,7}
68+
};
69+
cout << Solution().minimizeTheDifference(mat3, 6) << endl;
70+
// 1
71+
72+
return 0;
73+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
17151715
| 1978 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - |
17161716
| 1979 | [Find Greatest Common Divisor of Array](https://leetcode.com/problems/find-greatest-common-divisor-of-array/) | [] | [C++](1501-2000/1979-Number-of-Ways-to-Separate-Numbers/cpp-1979/) | | |
17171717
| 1980 | [Find Unique Binary String](https://leetcode.com/problems/find-unique-binary-string/) | [] | [C++](1501-2000/1980-Find-Unique-Binary-String/cpp-1980/) | | |
1718+
| 1981 | [Minimize the Difference Between Target and Chosen Elements](https://leetcode.com/problems/minimize-the-difference-between-target-and-chosen-elements/) | [] | [C++](1501-2000/1981-Minimize-the-Difference-Between-Target-and-Chosen-Elements/cpp-1981/) | | |
17181719
| | | | | | |
17191720

17201721
## 力扣中文站比赛

0 commit comments

Comments
 (0)