题解 | #最长上升子序列(一)#

最长上升子序列(一)

http://www.nowcoder.com/practice/5f65ccbb025240bd8458eb6479c2612e

动态规划:将大问题转化为小问题,每次求解小问题的最优解,再从最优解中挑选出最符合的

#include <algorithm>
#include <iostream>
#include <vector>

int main(int argc, char *argv[]) {
  
  int count, length = 0;
  
  std::cin >> count;
  
  std::vector<int> arr(count);
  std::vector<int> dp(count, 1);
  
  for (int i = 0; i < count; i++) {
    std::cin >> arr[i];
  }
  
  for (int i = 0; i < count; ++i) {  // 从小序列到大序列
    for (int j = 0; j < i; ++j) {
      if (arr[i] > arr[j]) {
        dp[i] = std::max(dp[i], dp[j] + 1);  // 子问题的最优解
      }
    }
    length = std::max(length, dp[i]);
  }
  
  std::cout << length << std::endl;
  
  return 0;
}
全部评论

相关推荐

joecii:如果没有工资,那可能没有工资是这家公司最小的问题了
找实习记录
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务