Skip to content

Commit 260a4d5

Browse files
committed
2263 solved.
1 parent 01ddc4b commit 260a4d5

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
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(cpp_2263)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(cpp_2263 main.cpp)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/// Source : https://leetcode.com/problems/make-array-non-decreasing-or-non-increasing/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-06-09
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <algorithm>
8+
9+
using namespace std;
10+
11+
12+
/// DP + Basic Optimization
13+
/// Time Complexity: O(n * maxv)
14+
/// Space Complexity: O(n * maxv)
15+
class Solution {
16+
17+
public:
18+
int convertArray(vector<int>& nums) {
19+
20+
int n = nums.size(), maxv = *max_element(nums.begin(), nums.end());
21+
22+
int res1 = solve(n, nums, maxv);
23+
24+
reverse(nums.begin(), nums.end());
25+
int res2 = solve(n, nums, maxv);
26+
27+
return min(res1, res2);
28+
}
29+
30+
private:
31+
int solve(int n, const vector<int>& nums, int maxv){
32+
33+
vector<int> dp(maxv + 1), premin(maxv + 1);
34+
for(int v = 0; v <= maxv; v ++){
35+
dp[v] = abs(nums[0] - v);
36+
if(v) premin[v] = min(premin[v - 1], dp[v]);
37+
else premin[v] = dp[v];
38+
}
39+
40+
for(int i = 1; i < n; i ++){
41+
vector<int> tdp(maxv + 1);
42+
for(int v = 0; v <= maxv; v ++)
43+
tdp[v] = premin[v] + abs(nums[i] - v);
44+
45+
premin[0] = tdp[0];
46+
for(int v = 1; v <= maxv; v ++) premin[v] = min(premin[v - 1], tdp[v]);
47+
dp = tdp;
48+
}
49+
return *min_element(dp.begin(), dp.end());
50+
}
51+
};
52+
53+
54+
int main() {
55+
56+
vector<int> nums1 = {3,2,4,5,0};
57+
cout << Solution().convertArray(nums1) << '\n';
58+
// 4
59+
60+
vector<int> nums2 = {2,2,3,4};
61+
cout << Solution().convertArray(nums2) << '\n';
62+
// 0
63+
64+
vector<int> nums3 = {0};
65+
cout << Solution().convertArray(nums3) << '\n';
66+
// 0
67+
68+
vector<int> nums4 = {0,2,8,0,3};
69+
cout << Solution().convertArray(nums4) << '\n';
70+
// 8
71+
72+
vector<int> nums5 = {4,2,6,7};
73+
cout << Solution().convertArray(nums5) << '\n';
74+
// 2
75+
76+
return 0;
77+
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2106,7 +2106,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
21062106
| 2260 | [Minimum Consecutive Cards to Pick Up](https://leetcode.com/problems/minimum-consecutive-cards-to-pick-up/) | [] | [C++](2001-2500/2260-Minimum-Consecutive-Cards-to-Pick-Up/cpp-2260/) | | |
21072107
| 2261 | [K Divisible Elements Subarrays](https://leetcode.com/problems/k-divisible-elements-subarrays/) | [] | [C++](2001-2500/2261-K-Divisible-Elements-Subarrays/cpp-2261/) | | |
21082108
| 2262 | [Total Appeal of A String](https://leetcode.com/problems/total-appeal-of-a-string/) | [] | [C++](2001-2500/2262-Total-Appeal-of-A-String/cpp-2262/) | | |
2109-
| | | | | | |
2109+
| 2263 | [Make Array Non-decreasing or Non-increasing](https://leetcode.com/problems/make-array-non-decreasing-or-non-increasing/) | [] | [C++](2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/) | | |
21102110
| 2264 | [Largest 3-Same-Digit Number in String](https://leetcode.com/problems/largest-3-same-digit-number-in-string/) | [] | [C++](2001-2500/2264-Largest-3-Same-Digit-Number-in-String/cpp-2264/) | | |
21112111
| 2265 | [Count Nodes Equal to Average of Subtree](https://leetcode.com/problems/count-nodes-equal-to-average-of-subtree/) | [] | [C++](2001-2500/2265-Count-Nodes-Equal-to-Average-of-Subtree/cpp-2265/) | | |
21122112
| 2266 | [Count Number of Texts](https://leetcode.com/problems/count-number-of-texts/) | [] | [C++](2001-2500/2266-Count-Number-of-Texts/cpp-2266/) | | |

0 commit comments

Comments
 (0)