File tree 2 files changed +83
-0
lines changed
2 files changed +83
-0
lines changed Original file line number Diff line number Diff line change
1
+ #https://leetcode.com/problems/fruit-into-type_baskets/
2
+
3
+ class Solution (object ):
4
+ #Time Efficiency: O(N), where N is the fruit count in the trees.
5
+ #Space Efficiency: O(K), where K is the total types of fruit in the trees.
6
+ def totalFruit (self , tree ):
7
+ counter = 0 #max fruit count
8
+ start = 0 #the point where from start to i has only two types of fruit
9
+ mark = {} #we keep track of each type of fruit's index last seen
10
+ type_basket = [] #types of fruit in the basket now
11
+
12
+ for i in range (len (tree )):
13
+ t = tree [i ]
14
+ if t not in type_basket :
15
+ if len (type_basket )< 2 :
16
+ """
17
+ if this type of fruit not in type_basket
18
+ and the type_basket is not full yet
19
+ add the type to the type_basket
20
+ """
21
+ type_basket .append (t )
22
+
23
+ elif len (type_basket )== 2 :
24
+ """
25
+ if this type of fruit not in type_basket
26
+ and the type_basket has two types already
27
+ we get rid of the type with smaller last seen index
28
+ by moving the start to its index+1
29
+ so now between start and i has only two types of fruit
30
+ """
31
+ t1 = type_basket [0 ]
32
+ t2 = type_basket [1 ]
33
+ if mark [t1 ]< mark [t2 ]:
34
+ start = mark [t1 ]+ 1
35
+ type_basket [0 ] = t
36
+ else :
37
+ start = mark [t2 ]+ 1
38
+ type_basket [1 ] = t
39
+ else :
40
+ #basket should not have more than two types
41
+ print ('ERROR' )
42
+ return 0
43
+
44
+ counter = max (counter , i - start + 1 )
45
+ mark [t ] = i
46
+ return counter
Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+
3
+ """
4
+ everytime we iterate to the next char (on index i)
5
+ we move the start to the place, where all the char between the start and i are all unique
6
+ and keep undating counter til the end
7
+
8
+ how do we move the start?
9
+ we write down the index of the char we last seen
10
+ if char is seen, we set the start to (the index we seen char last time)+1
11
+ so we don't repeat char, BUT
12
+
13
+ another rule of start is not moving leftward
14
+ bc this will cause more repeating char between start and i
15
+ so we have to keep start incremental
16
+
17
+ we only iterate through the string once so
18
+ Time Efficiency is O(N)
19
+
20
+ we use a dict to keep track of the index of char so
21
+ Space Efficiency is O(N)
22
+ """
23
+
24
+ def lengthOfLongestSubstring (self , s ):
25
+ if s is None or s == '' : return 0
26
+ counter = 0 #max length of substring
27
+ start = 0 #index where all the char between the start and i are all unique
28
+ mark = {} #all the char index we last seen
29
+
30
+ for i in range (len (s )):
31
+ char_now = s [i ]
32
+ if char_now in mark :
33
+ start = max (start , mark [char_now ]+ 1 )
34
+ counter = max (counter , i - start + 1 )
35
+ mark [char_now ] = i
36
+
37
+ return counter
You can’t perform that action at this time.
0 commit comments