1
00:00:00,660 --> 00:00:01,680
Welcome back.

2
00:00:01,680 --> 00:00:06,750
Let's now go ahead and code the optimum solution which we discussed in the previous video.

3
00:00:06,750 --> 00:00:08,910
Now this is the previous function that we've written.

4
00:00:08,910 --> 00:00:10,320
So let's just have it over there.

5
00:00:10,320 --> 00:00:13,770
Now I'm going to write the other function the optimum solution over here.

6
00:00:13,770 --> 00:00:15,870
And let's just call it max area.

7
00:00:16,880 --> 00:00:20,120
And this function again is going to take in the array.

8
00:00:22,190 --> 00:00:25,520
All right, so let me just make some space over here.

9
00:00:26,090 --> 00:00:26,330
Right.

10
00:00:26,330 --> 00:00:28,070
So we have the previous function above.

11
00:00:28,100 --> 00:00:32,810
Now in this function again we are also going to have area initialized to zero.

12
00:00:32,810 --> 00:00:34,760
So let's say area is equal to zero.

13
00:00:35,060 --> 00:00:40,730
And then we are going to set I to zero and j to the last index.

14
00:00:40,730 --> 00:00:40,940
Right.

15
00:00:40,940 --> 00:00:43,880
So let's say let I is equal to zero.

16
00:00:44,240 --> 00:00:47,600
And let's say let j is equal to array dot length.

17
00:00:49,140 --> 00:00:50,220
Minus one.

18
00:00:50,520 --> 00:00:53,130
So we are having the maximum width at this point.

19
00:00:53,130 --> 00:00:53,520
Right.

20
00:00:53,520 --> 00:00:56,940
And again we have seen that area is equal to width into height.

21
00:00:56,940 --> 00:01:02,130
So now we are going through loop through until I is less than J.

22
00:01:02,130 --> 00:01:02,370
Right.

23
00:01:02,370 --> 00:01:08,400
So if that is getting crossed then we have already compared all the possibilities and we don't need

24
00:01:08,400 --> 00:01:09,060
to go further.

25
00:01:09,060 --> 00:01:10,560
So this is going to be a while loop.

26
00:01:10,560 --> 00:01:14,520
Now we are going to find the height at each instance.

27
00:01:14,520 --> 00:01:20,430
And then we are going to find the new area and check whether that new area is greater than what we have

28
00:01:20,430 --> 00:01:22,650
currently stored into our area variable.

29
00:01:22,650 --> 00:01:24,090
So let's go ahead and do that.

30
00:01:24,090 --> 00:01:29,250
So let's say let height is equal to math dot min.

31
00:01:29,700 --> 00:01:34,890
And it's going to be the minimum height between array at I and array at J.

32
00:01:34,890 --> 00:01:35,490
Right.

33
00:01:38,820 --> 00:01:39,360
All right.

34
00:01:39,360 --> 00:01:40,950
So this is going to be the height.

35
00:01:40,950 --> 00:01:44,070
And then the new area let new area.

36
00:01:45,570 --> 00:01:51,840
Is equal to height into j minus I.

37
00:01:55,130 --> 00:01:55,550
All right.

38
00:01:55,550 --> 00:01:57,650
So that will give us the new area.

39
00:01:57,650 --> 00:02:06,140
And then we are going to say area is equal to math dot max between the current area and the new area.

40
00:02:06,140 --> 00:02:07,640
What we just now computed.

41
00:02:07,640 --> 00:02:08,090
Right.

42
00:02:08,090 --> 00:02:12,410
So this will give the maximum area to be stored to the area variable.

43
00:02:12,440 --> 00:02:13,010
All right.

44
00:02:13,010 --> 00:02:15,170
And then we just have to increment right.

45
00:02:15,170 --> 00:02:20,030
So again we have to decide whether we move I or whether we move J.

46
00:02:20,150 --> 00:02:26,720
Now as we discussed we have to move whichever among I or J is pointing to the lesser height.

47
00:02:26,720 --> 00:02:27,080
Right.

48
00:02:27,080 --> 00:02:27,950
So let's do that.

49
00:02:27,950 --> 00:02:31,730
So if array at at uh let's say I.

50
00:02:32,990 --> 00:02:34,640
If that is lesser.

51
00:02:35,340 --> 00:02:36,570
Then arrange.

52
00:02:36,690 --> 00:02:39,360
If that is the case, then we have to move I.

53
00:02:39,390 --> 00:02:39,630
Right.

54
00:02:39,630 --> 00:02:42,060
So if that is the case then we say I plus plus.

55
00:02:42,300 --> 00:02:46,530
Now if that is not the case then we move J right.

56
00:02:46,530 --> 00:02:47,970
So we move j to the left.

57
00:02:47,970 --> 00:02:49,860
So we say j minus minus.

58
00:02:49,860 --> 00:02:53,280
So we have to decide over here whether to move I or J.

59
00:02:53,880 --> 00:02:57,630
And this will keep looping as long as I is less than j.

60
00:02:57,630 --> 00:03:02,490
And then once we are out of this we just need to return area and we are done with our solution.

61
00:03:02,490 --> 00:03:04,680
Now let's call this function.

62
00:03:04,680 --> 00:03:05,010
All right.

63
00:03:05,010 --> 00:03:10,800
So again over here we have already called this for the previous function which we have written.

64
00:03:10,800 --> 00:03:12,330
Now let me just comment this.

65
00:03:12,330 --> 00:03:14,430
Now we are going to call it for this new function.

66
00:03:14,430 --> 00:03:14,790
Right.

67
00:03:14,790 --> 00:03:18,060
So let's do that over here max area.

68
00:03:18,060 --> 00:03:23,130
And again let's pass in this over here 91234 and ten.

69
00:03:23,130 --> 00:03:26,610
We have seen that we should get the answer as 36 in this case right.

70
00:03:26,610 --> 00:03:27,810
So let's run it.

71
00:03:28,430 --> 00:03:31,280
And you can see we are getting the correct answer, which is 36.

72
00:03:31,310 --> 00:03:35,720
Now let's also pass in the array which we had when we discussed it.

73
00:03:35,720 --> 00:03:42,050
So let's pass that array over here we have three, seven, five, six, eight and four.

74
00:03:42,080 --> 00:03:43,310
Now let's run it.

75
00:03:43,430 --> 00:03:50,420
And you can see yes we are getting the answer as 21 which is the case where we take this seven and this

76
00:03:50,420 --> 00:03:50,660
eight.

77
00:03:50,660 --> 00:03:50,870
Right.

78
00:03:50,870 --> 00:03:52,790
So seven into three gives us 21.

79
00:03:52,790 --> 00:03:54,590
So yes this function is also working.

80
00:03:54,590 --> 00:04:00,980
Now let's take a sample input and walk through the code and also take a look at the space and time complexity

81
00:04:00,980 --> 00:04:06,140
which and again remember in the interview, walking through your code with a sample input is a very

82
00:04:06,140 --> 00:04:06,830
important step.

83
00:04:06,830 --> 00:04:08,780
So let's go ahead and walk through the code now.
