Skip to content

Commit c053710

Browse files
Update dynamic-programming.md
1 parent b3db5d2 commit c053710

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

dynamic-programming.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -612,11 +612,20 @@ We have rod of length 5 with pieces value: 2, 5, 7, 8 for
612612
/* [3, 10, 2, 1, 20] = [3, 10, 20] = 3
613613
[3, 2] = [3] or [2] = 1
614614
[50, 3, 10, 7, 40, 50] = [3, 7, 40, 50] or [3, 10, 40, 50] = 4 */
615-
// N squared solution is very easy to have
615+
// N squared solution is very easy to have using dp, dp[i] = min of dp[j]+1 where j < i and arr[j] < arr[i]
616616

617-
/* NlogN approach: Length of st denotes LIS formed by including
618-
currently encountered element and maintaining an analogous LIS
619-
behavior. */
617+
/* Greedy approach:
618+
[3, 10, 2, 1, 20]
619+
itterate and form increasing lists at the end of itteration we will have
620+
list1: [3, 10, 20] list2: [2, 20] list3: [1, 20] so list1 is the answer.
621+
622+
directly implementing above logic can be costly, there's an observation in it.
623+
- We only care about the size of list not the actual list.
624+
- To keep maintaining the list we need to just maintain relative order
625+
so can we maintain all the lists in one list (denoted using set) ? length of list is the answer
626+
627+
final algo: itterate, if set contain element atleast that then remove it and add the element.
628+
*/
620629
int lengthOfLIS(vector<int>& nums)
621630
{
622631
set<int> st;

0 commit comments

Comments
 (0)