From d72b1c65af0806176410efab714b1ee4b4fbab4c Mon Sep 17 00:00:00 2001 From: Vidhi Sinha <72182782+vidhisinha2002@users.noreply.github.com> Date: Tue, 4 Oct 2022 20:49:30 +0530 Subject: [PATCH] Create Longest Increasing Subsequence.cpp --- .../Longest Increasing Subsequence.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 LeetCode Solutions/Longest Increasing Subsequence.cpp diff --git a/LeetCode Solutions/Longest Increasing Subsequence.cpp b/LeetCode Solutions/Longest Increasing Subsequence.cpp new file mode 100644 index 0000000..0bb7e64 --- /dev/null +++ b/LeetCode Solutions/Longest Increasing Subsequence.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int lengthOfLIS(vector& nums) { + int n = nums.size(); + + vectorLIS(n+1, 1); + + for (int i = 0; i < n; i++) + for (int j = 0; j < i; j++) + if (nums[i] > nums[j]) + LIS[i] = max(LIS[i], 1 + LIS[j]); + + int ans = 0; //return max of whole array + for (int i = 0; i < n; i++) + ans = max(ans, LIS[i]); + + return ans; + } +};