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
| #include <iostream> #include <vector> #include <algorithm> #include <climits> using namespace std;
class Solution_0 { public: int lengthOfLIS(vector<int>& nums) { int n = nums.size(); vector<int> f(n, 1);
for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { if (nums[i] > nums[j]) { f[i] = max(f[i], f[j] + 1); } } } int k = 0; for (int i = 0; i < n; i++) { if (f[k] < f[i]) { k = i; } }
return f[k]; } };
class Solution { public: int lengthOfLIS(vector<int>& nums) { int n = nums.size(); vector<int> f(n, 1); int res = 0;
for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { if (nums[i] > nums[j]) { f[i] = max(f[i], f[j] + 1); } } res = max(res, f[i]); }
return res; } };
int main() { Solution solution; vector<vector<int>> test_cases = { {10,9,2,5,3,7,101,18}, {0,1,0,3,2,3}, {7,7,7,7,7,7,7} };
for (auto& nums : test_cases) { cout << "Input: "; for (int num : nums) { cout << num << " "; } cout << endl; cout << "Output: " << solution.lengthOfLIS(nums) << endl; }
return 0; }
|