Advertisement
Josif_tepe

Untitled

Oct 31st, 2023
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     vector<int> v;
  4.     int n;
  5.     int dp[2505];
  6.     int rec(int at) {
  7.         if(dp[at] != -1) {
  8.             return dp[at];
  9.         }
  10.        
  11.         int result = 1;
  12.         for(int i = at + 1; i < n; i++) {
  13.             if(v[at] < v[i]) {
  14.                 result = max(result, rec(i) + 1);
  15.             }
  16.         }
  17.         return dp[at] = result;
  18.     }
  19.     int lengthOfLIS(vector<int> nums) {
  20.         v = nums;
  21.         n = (int) v.size();
  22.         int result = 0;
  23.         for(int i = 0; i < n; i++) {
  24.             dp[i] = -1;
  25.         }
  26.         for(int i = 0; i < n; i++) {
  27.             result = max(result, rec(i));
  28.         }
  29.         return result;
  30.     }
  31. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement