1
00:00:01,209 --> 00:00:08,400
So, we will see one more example for writing
loops, we see as slightly trickier example
2
00:00:08,400 --> 00:00:11,940
and will cover this over multiple sessions.
3
00:00:11,940 --> 00:00:14,720
So, the problem is the following.
4
00:00:14,720 --> 00:00:19,859
We have to read as sequence of numbers until
a minus 1 is read, minus 1 indicates that
5
00:00:19,859 --> 00:00:22,689
then the sequence of sent it.
6
00:00:22,689 --> 00:00:29,460
And the question is we have to output the
length of the longest contiguous increasing
7
00:00:29,460 --> 00:00:30,460
subsequence.
8
00:00:30,460 --> 00:00:34,629
So, that is a lot of words let us illustrate
it with an example.
9
00:00:34,629 --> 00:00:43,000
Let us say that the input is of the following
numbers - 9 2 4 0 3 4 6 9 2, and then finally
10
00:00:43,000 --> 00:00:44,850
a minus 1.
11
00:00:44,850 --> 00:00:50,980
And we have to output the length of the longest
contiguous increasing subsequence.
12
00:00:50,980 --> 00:00:57,620
So, let us say what do I mean by a contiguous
increasing subsequence?
13
00:00:57,620 --> 00:01:04,960
So, I say that 9 is an increasing subsequence,
then the next number is 2; 2 is less than
14
00:01:04,960 --> 00:01:05,960
9.
15
00:01:05,960 --> 00:01:13,340
So, 9 and 2 cannot be part of a subsequence,
where the numbers keep on increasing.
16
00:01:13,340 --> 00:01:20,460
So, 2 is the start of a new sequence, again
the next number is 4.
17
00:01:20,460 --> 00:01:23,100
So, 2 and 4 form an increasing sequence.
18
00:01:23,100 --> 00:01:25,979
So, you can continue increasing a sequence.
19
00:01:25,979 --> 00:01:32,110
The next number is 0, 0 is less than 4, so
break the sequence there.
20
00:01:32,110 --> 00:01:43,240
Then when you look at then succeeding numbers
0 3 4 6 9; they form an increasing sequence.
21
00:01:43,240 --> 00:01:46,710
And the last number is 2, which is lesser
than 9.
22
00:01:46,710 --> 00:01:49,750
So, the increasing sequence stops here.
23
00:01:49,750 --> 00:02:00,979
So, these are the increasing contiguous subsequences;
contiguous means together occurring adjacent
24
00:02:00,979 --> 00:02:03,229
to each other.
25
00:02:03,229 --> 00:02:11,940
So, the largest of the longest contiguous
subsequence is obviously, 0 3 4 6 9, and the
26
00:02:11,940 --> 00:02:18,550
length of that sequence is 5.
27
00:02:18,550 --> 00:02:28,800
Let us take another example 11 9 7 8 11 12
15 15 and minus 1.
28
00:02:28,800 --> 00:02:36,380
So, just to illustrate the point 11 is greater
than 9.
29
00:02:36,380 --> 00:02:41,660
So, that cannot be an increasing sequence,
9 is greater than 7, so that is another the
30
00:02:41,660 --> 00:02:49,000
increasing sequence is just 9, but then 7
8 11 12 15; these are increasing.
31
00:02:49,000 --> 00:02:54,470
And I decided to stop here even though the
next number was 15, because I am interested
32
00:02:54,470 --> 00:02:56,780
in and increasing subsequence.
33
00:02:56,780 --> 00:02:58,790
So, 15 and 15 are equal numbers.
34
00:02:58,790 --> 00:03:00,580
So, we break it ƒ.
35
00:03:00,580 --> 00:03:08,920
So, the longest increasing subsequence is
7 8 11 12 15 and its length is 5.
36
00:03:08,920 --> 00:03:15,060
So, this the longest contiguous increasing
subsequence.
37
00:03:15,060 --> 00:03:23,050
So, here is a basic property given any sequence
of numbers, we can break it into a collection
38
00:03:23,050 --> 00:03:26,610
of increasing contiguous subsequences.
39
00:03:26,610 --> 00:03:30,230
For example the numbers that the sequence
that we have seen.
40
00:03:30,230 --> 00:03:40,710
So, 9 2 4 0 3 4 6 4 6 9 2, and that the length
of the increasing, the longest increasing
41
00:03:40,710 --> 00:03:43,830
contiguous subsequence is 5.
42
00:03:43,830 --> 00:03:51,260
So, we have to write a program to do this,
given a sequence of numbers find the length
43
00:03:51,260 --> 00:03:55,190
of the longest increasing subsequence.
44
00:03:55,190 --> 00:03:58,780
So, how do we do it?
45
00:03:58,780 --> 00:04:05,850
We do it in the way that we have been writing
loops so far, like adding n numbers and finding
46
00:04:05,850 --> 00:04:07,570
this sum and so on.
47
00:04:07,570 --> 00:04:13,240
The idea was that you start from the first
number and keep on adding the numbers until
48
00:04:13,240 --> 00:04:16,460
you hit minus 1 at which point you have this
sum.
49
00:04:16,460 --> 00:04:21,590
So, the idea of this algorithm was that you
start from the first, and you keep reading,
50
00:04:21,590 --> 00:04:23,770
until certain condition happens.
51
00:04:23,770 --> 00:04:27,860
We will adopt that idea to solve our current
problem.
52
00:04:27,860 --> 00:04:33,139
So, what we need to do is to keep track of
the current increasing subsequence.
53
00:04:33,139 --> 00:04:34,919
Let us say that it is denoted by s.
54
00:04:34,919 --> 00:04:41,590
So, before we get into the code, let see how
we will do it by hand.
55
00:04:41,590 --> 00:04:49,620
So, initially the increasing subsequence is
s, and let say that it is empty.
56
00:04:49,620 --> 00:04:54,729
After you read 9, you have an increasing subsequence
which consist of exactly 1 number.
57
00:04:54,729 --> 00:04:56,990
So, s is 9.
58
00:04:56,990 --> 00:05:01,870
Now, the next number is 10; 10 is greater
than 9.
59
00:05:01,870 --> 00:05:06,599
So, you extend this.
60
00:05:06,599 --> 00:05:11,550
Read the next number 4; 4 is less than 10.
61
00:05:11,550 --> 00:05:20,729
So, 9 10 4 cannot be an increasing subsequence,
therefore you say that you break the subsequence
62
00:05:20,729 --> 00:05:24,990
there, so 9 10 is a different subsequence.
63
00:05:24,990 --> 00:05:28,449
Now you start a new subsequence which is 4.
64
00:05:28,449 --> 00:05:32,270
So, the current subsequence is just 4.
65
00:05:32,270 --> 00:05:38,180
So, 0 is less than 4, so you break it there,
the current increasing subsequence become
66
00:05:38,180 --> 00:05:41,160
0.
67
00:05:41,160 --> 00:05:44,800
Read the next number 3, 3 is greater than
0.
68
00:05:44,800 --> 00:05:52,930
See you extend the subsequence s is now 0
3 4, 4 is greater than 3.
69
00:05:52,930 --> 00:05:59,419
So, the sequence becomes 0 3 4 and so on.
70
00:05:59,419 --> 00:06:02,379
So, what are we doing here?
71
00:06:02,379 --> 00:06:08,789
We are reading the read, we are reading the
numbers integer by integer, and we are keeping
72
00:06:08,789 --> 00:06:11,590
track of the current increasing subsequence.
73
00:06:11,590 --> 00:06:14,639
So, this is part of what we want to do?
74
00:06:14,639 --> 00:06:21,219
Now, in order to decided whether we should
increase the subsequence extend it or break
75
00:06:21,219 --> 00:06:27,290
the subsequence, and start a new sequence
what we need to do is we should remember what
76
00:06:27,290 --> 00:06:30,969
was the previous number we saw, and what is
the current number that we have seen.
77
00:06:30,969 --> 00:06:37,630
So, with two variables we can say whether
the subsequence should be extended or should
78
00:06:37,630 --> 00:06:40,840
be broken at this point.
79
00:06:40,840 --> 00:06:49,150
So, if the previous number is less than the
current number, then we should increase the
80
00:06:49,150 --> 00:06:51,999
we should extend the subsequence.
81
00:06:51,999 --> 00:06:58,669
If the previous number is greater than or
equal to the current number then we should
82
00:06:58,669 --> 00:07:01,810
break the subsequence and start a new sequence.
83
00:07:01,810 --> 00:07:07,689
So, extend the sequence means add 1 to the
length of the current increasing subsequence,
84
00:07:07,689 --> 00:07:11,270
and add that number to the subsequence.
85
00:07:11,270 --> 00:07:15,569
Reset means you start a new sequence of length
1.
86
00:07:15,569 --> 00:07:21,889
So, from the current from the description
that we have seen so far, we need the following
87
00:07:21,889 --> 00:07:30,680
variables, we need C which is for the current
rate c number, p which is the previous number
88
00:07:30,680 --> 00:07:36,379
that we have seen, and length which is the
length of the current increasing sequence.
89
00:07:36,379 --> 00:07:44,029
So, we store the previous number into p, store
the current length of the increasing subsequence
90
00:07:44,029 --> 00:07:49,089
into length, and read the next number into
c.
91
00:07:49,089 --> 00:07:55,629
So, if the previous number is less than the
current number.
92
00:07:55,629 --> 00:08:01,000
So, we take the true branch in which case
we extend the sequence.
93
00:08:01,000 --> 00:08:06,090
So, the new number that we have read is part
of the of the currently increasing subsequence.
94
00:08:06,090 --> 00:08:11,819
So, increase the length of the sequence by
1 and now we move lockstep.
95
00:08:11,819 --> 00:08:13,889
So, what we do is?
96
00:08:13,889 --> 00:08:26,760
So, we are a stage where suppose we have numbers
1 2 and 3, suppose p was pointing to 1, c
97
00:08:26,760 --> 00:08:29,439
was pointing to 2.
98
00:08:29,439 --> 00:08:35,690
So, since 2 was greater than 1, we extend
the sequence.
99
00:08:35,690 --> 00:08:40,900
After extending the sequence, we have to proceed
and see what will happen with the next number.
100
00:08:40,900 --> 00:08:49,060
So, when you do that you can do the following,
I will extend the current sequence by doing
101
00:08:49,060 --> 00:08:56,370
the following, I will now set p equal to 2,
and c equal to 3.
102
00:08:56,370 --> 00:09:03,430
So, this is the idea that we will advance
both the variables by 1 number each.
103
00:09:03,430 --> 00:09:08,990
So, that it is always true that previous is
1 number behind current.
104
00:09:08,990 --> 00:09:14,961
So, I hope this idea is clear that in order
to ensure that p is 1 number behind current,
105
00:09:14,961 --> 00:09:17,090
you have to advance both p and c.
106
00:09:17,090 --> 00:09:22,750
So, assign p to c, this will advance p and
then read the next number.
107
00:09:22,750 --> 00:09:24,930
So, that will become c.
108
00:09:24,930 --> 00:09:34,700
Now what happens if c is greater than or equal
to p, then the new number is not part of the
109
00:09:34,700 --> 00:09:36,140
current increasing subsequence.
110
00:09:36,140 --> 00:09:43,080
So, you start a new sequence which is of length
1 and again do the same assign p to c, which
111
00:09:43,080 --> 00:09:46,400
is advancing the pointer and read the next
number.
112
00:09:46,400 --> 00:09:52,880
So, here is the method that we will follow
in order to keep track of the current increasing
113
00:09:52,880 --> 00:09:53,880
subsequence.
114
00:09:53,880 --> 00:09:58,790
Now what is left is to find the longest of
all the increasing subsequences that we find.