1
00:00:00,720 --> 00:00:01,590
Hi everyone.

2
00:00:01,590 --> 00:00:03,330
Let's do this question.

3
00:00:03,360 --> 00:00:10,320
Evaluate the value of an arithmetic expression in reverse Polish notation c example.

4
00:00:10,320 --> 00:00:13,200
So this is an example of reverse Polish notation.

5
00:00:13,200 --> 00:00:13,590
All right.

6
00:00:13,590 --> 00:00:17,130
So valid operators are plus minus into and division.

7
00:00:17,130 --> 00:00:21,840
Note that division between two integers integers should truncate towards zero.

8
00:00:21,840 --> 00:00:23,490
That means you have to round it down.

9
00:00:23,490 --> 00:00:29,820
It is guaranteed that the given RPN or reverse Polish notation expression is always valid.

10
00:00:29,820 --> 00:00:35,550
That means the expression would always evaluate to a result, and there will not be any division by

11
00:00:35,550 --> 00:00:37,950
zero operation, so we don't need to check that.

12
00:00:37,950 --> 00:00:39,900
So there can be these edge cases.

13
00:00:39,900 --> 00:00:45,090
And we don't need to be worried about that because it's guaranteed that the expression will be valid.

14
00:00:45,090 --> 00:00:45,540
All right.

15
00:00:45,540 --> 00:00:51,090
The input is an array of strings where each element is either a valid operator or an integer.

16
00:00:51,090 --> 00:00:52,440
So this is an example.

17
00:00:52,440 --> 00:00:56,520
So let's take a look at this example over here to understand this question in a better manner.

18
00:00:56,520 --> 00:00:59,460
Let's say the input given is this array over here.

19
00:00:59,460 --> 00:01:04,680
So you can see over here we have a string with value one, a string with value two and a string with

20
00:01:04,680 --> 00:01:05,730
the plus operator.

21
00:01:05,730 --> 00:01:09,780
Now this means we take this one over here which is this one.

22
00:01:09,780 --> 00:01:11,910
We take this two which is the second number.

23
00:01:11,910 --> 00:01:14,100
And then we apply this operator over here.

24
00:01:14,100 --> 00:01:18,960
And this expression if this is the input the output expected is equal to three.

25
00:01:18,990 --> 00:01:22,980
Now let's take a look at one more example to understand this better.

26
00:01:22,980 --> 00:01:26,490
Let's say the array given is one, two three plus and minus.

27
00:01:26,490 --> 00:01:31,560
Now in this case we first have to evaluate this over here two three and plus.

28
00:01:31,560 --> 00:01:33,270
So we can see that this is plus.

29
00:01:33,270 --> 00:01:36,090
So the two numbers before it we have two and three.

30
00:01:36,090 --> 00:01:39,270
So this actually this part over here is two.

31
00:01:40,090 --> 00:01:40,690
Plus three.

32
00:01:41,140 --> 00:01:43,540
So this simplifies to five.

33
00:01:43,810 --> 00:01:48,490
And then it's equal to one five and minus which is one minus five.

34
00:01:48,520 --> 00:01:52,750
That's how we get one minus five over here which is equal to minus four.

35
00:01:52,780 --> 00:01:53,170
All right.

36
00:01:53,170 --> 00:01:55,000
So we have understood the question.

37
00:01:55,360 --> 00:01:58,120
Now let's take a look at a few test cases.

38
00:01:59,250 --> 00:02:02,520
Let's say we already have taken care of these two test cases.

39
00:02:02,520 --> 00:02:08,040
So if it's one, two and plus, it means one plus two, that's equal to three, one, two, three plus

40
00:02:08,040 --> 00:02:08,550
and minus.

41
00:02:08,550 --> 00:02:11,970
So two three plus this over here simplifies to five.

42
00:02:11,970 --> 00:02:14,820
And then we get it should be one minus five right.

43
00:02:14,820 --> 00:02:16,560
So this is one and then five.

44
00:02:16,560 --> 00:02:19,380
So this is equivalent to one five and minus.

45
00:02:19,380 --> 00:02:23,640
And this simplifies to one minus five which is equal to minus four.

46
00:02:23,640 --> 00:02:27,600
And if the expression expression is four three and division.

47
00:02:27,600 --> 00:02:31,140
So this is equal to four divided by three right.

48
00:02:31,560 --> 00:02:34,200
And this is equal to 1.33 etc..

49
00:02:34,200 --> 00:02:39,600
And in the question it's mentioned that in the case of division this has to be truncated towards zero.

50
00:02:39,600 --> 00:02:41,610
So that means this has to be rounded down.

51
00:02:41,610 --> 00:02:43,800
So 1.33 becomes one.

52
00:02:43,800 --> 00:02:44,340
All right.

53
00:02:44,340 --> 00:02:47,850
So again let's take a look at a bigger bigger example.

54
00:02:48,450 --> 00:02:48,900
All right.

55
00:02:48,900 --> 00:02:51,990
So let's say this is the array which is given to us.

56
00:02:51,990 --> 00:02:53,580
Now let's try to simplify this.

57
00:02:53,610 --> 00:02:55,680
We see two then three then four.

58
00:02:55,680 --> 00:02:57,240
And then we have plus over here.

59
00:02:57,240 --> 00:02:59,520
So the previous two over here is three and four.

60
00:02:59,520 --> 00:03:04,350
So this over here simplifies to three plus four which is equal to seven.

61
00:03:04,350 --> 00:03:06,630
So we have two seven and then a minus.

62
00:03:06,630 --> 00:03:09,570
So this over here simplifies to two minus seven.

63
00:03:09,570 --> 00:03:09,780
Right.

64
00:03:09,780 --> 00:03:12,330
So two minus seven gives us minus five.

65
00:03:12,330 --> 00:03:13,830
So let me just remove this.

66
00:03:13,830 --> 00:03:16,290
And I'm just replacing this with minus five.

67
00:03:16,290 --> 00:03:19,380
And then we have minus five seven and into.

68
00:03:19,380 --> 00:03:23,430
So this is minus five into seven which is equal to -35.

69
00:03:23,430 --> 00:03:27,300
So if this array was the input then we would have to return -35.

70
00:03:27,300 --> 00:03:29,790
So we have got a good understanding of the question.

71
00:03:29,790 --> 00:03:34,500
So let's go ahead and think about how we can solve this question in the next video.
