感觉T4数据有问题。希望出题可以拍一拍
第一题是很典的股票问题
class Solution {
public:
int maxProfit(vector<int>& prices) {
int res = 0;
int cur = 0;
for(int i = 0; i < prices.size(); i++){
if(prices[cur] < prices[i]){
res = max(res, prices[i] - prices[cur]);
swap(prices[cur], prices[i]);
}
cur = i;
}
return res;
}
};
第二题也是很典的矩阵DP问题
#include<bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(), (c).end()
#define rev(a) reverse((a).begin(), (a).end())
#define each(x, a) for(auto& x : a)
#define mst(a, x) memset(a, x, sizeof(a))
#define rep(i, from, to) for(int i = from;i<to;i++)
#define rrep(i, from, to) for(i128 i = from;i>=to;i--)
#define to_uni(a) a.erase(unique(begin(a), end(a)), end(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define endl "\n"
#define i128 __int128
#define ll long long
typedef pair<int, int> pii;
const int mod = 1e9 + 7;
const int dx[4]{1, 0, -1, 0}, dy[4]{0, 1, 0, -1};
const int fx[8] = {-1, -1, 0, 1, 1, 1, 0, -1}, fy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int N = 1e5 + 10;
long long dp[1005][1005];
void solve() {
int m, n;
cin >> m;
n = m;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
cin >> dp[i][j];
}
}
for (int i = 0; i <= n + 1; i++) {
dp[0][i] = 9999999;
dp[m + 1][i] = 9999999;
}
for (int i = 0; i <= m + 1; i++) {
dp[i][0] = 9999999;
dp[i][n + 1] = 9999999;
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (i == 1 && j == 1) {
continue;
} else {
dp[i][j] += min(dp[i - 1][j], dp[i][j - 1]);
}
}
}
cout << dp[m][n];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}