▶실버2
문제
풀이
전형적인 DP 문제.
가장 긴 증가하는 부분 수열 문제와 유사하다.
다음과 같은 형식으로 풀었다.
코드
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstring>
#include <map>
using namespace std;
int box[1001];
int dp[1001];
int main(void) {
ios::sync_with_stdio(NULL);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> box[i];
dp[i] = 1;
}
int ans = 1;
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (box[j] < box[i]) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
ans = max(ans, dp[i]);
}
cout << ans << endl;
}
'Algorithm > Baekjoon' 카테고리의 다른 글
1080_행렬 (0) | 2023.03.07 |
---|---|
1213_팰린드롬 만들기 (0) | 2023.03.05 |
1789_수들의 합 (0) | 2023.03.02 |
1911_흙길 보수하기 (0) | 2023.02.28 |
3190_뱀 (0) | 2023.02.10 |