You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#Graph of courses pointing from prerequisites to next courses
10
-
dependants= {i:[] foriinrange(numCourses)}
1
+
# Question(#210): Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses.
2
+
# Difficulty: Medium
3
+
# What do I learn from this question? How to implement a general topological sort with cycle checking
#initialize letterCount, localLeft and globalLeft to 0
14
-
letterCount=localLeft=globalLeft=0
15
-
16
-
#For every letter in s
17
-
fori, currentinenumerate(s):
18
-
19
-
#If the letter is in the map, subtract 1 from it and increment letterCount if its count is >= 0
20
-
#This way letterCount records the number of letter's we're looking for that have been found
21
-
ifcurrentinlookingFor:
22
-
lookingFor[current] -=1
23
-
iflookingFor[current] >=0: letterCount+=1
24
-
25
-
#While our letterCount is the same as the len of t
26
-
#Note this block of code will only execute when we've found a substring with all letters in t
27
-
whileletterCount==len(t):
28
-
29
-
#If our (current index - localLeft + 1) is less than minlen
30
-
#In other words if the length of the substring we've localLeftust found is less than the minimum one we know, we'll update minlen and globalLeft
31
-
#Note localLeft denotes the leftmost index of our candidate substring and minlen denotes the global leftmost index that results in the shortest substring
#If the leftmost item is in the map, add 1 to it and decrement letterCount if its still > 0
35
-
#This essentially means we now need to look for this letter again since we're shifting an index that includes a letter we want
36
-
#We decrement letter count if its value in the map is non-zero to signify we're missing a letter, since letterCount is no longer the same length as t, we also exit the while loop now
37
-
ifs[localLeft] inlookingFor:
38
-
lookingFor[s[localLeft]] +=1
39
-
iflookingFor[s[localLeft]] >0: letterCount-=1
40
-
41
-
#Keep shifting the localLeft pointer right to find smaller and smaller windows
42
-
localLeft+=1
43
-
44
-
ifminlen>len(s): return""
1
+
# Question: Given a string and a list of target letters, find the shortest continious string that has all those letters
2
+
# Solution/Pattern: The intuition here is to try and find the first substring that contains all letters and then try shrinking
3
+
# it's left boundary until we lose a character that was required. We contntinue to do this for all characters in
4
+
# the string, hence creating a 'sliding window'
5
+
# Difficulty: Hard
6
+
45
7
46
-
#Return the substring starting from the globalLeft pointer and ending at + minlen
47
-
returns[globalLeft:globalLeft+minlen]
48
-
49
-
defmain():
50
-
print(minWindow('ADOBECODEBANC', 'ABC'))
8
+
defminWindow(self, s: str, t: str) ->str:
9
+
10
+
# Initialize the starting point of our starting window
11
+
start=0
12
+
shortest=len(s) +1
13
+
# We store the count of each letter needed in a hashmap
14
+
lookingFor=collections.Counter(t)
15
+
# We also store the total number of chars we have left to find
16
+
charsNeeded=len(t)
17
+
shortestString=""
18
+
19
+
forend, vinenumerate(s):
20
+
# If this char is one we were looking for (one that's present in our target) thenwe need
21
+
# to decrement it's count by one indicating we've seen it.
22
+
# After we've decremented the count we need to check if we've seen it enough times, if the the
23
+
# count of the character is less than 0 that means that it's a extra char occuring more times than in t
24
+
# However, if after decrementing we have a number greater than or equal to 0 then we can say this was one
25
+
# of the targets we were lookingFor and decrement our chars needed count
26
+
ifvinlookingFor:
27
+
lookingFor[v] -=1
28
+
iflookingFor[v] >=0: charsNeeded-=1
51
29
52
-
main()
30
+
# Now, if charsNeeded == 0, we can start shrinking our window from the left
31
+
# since we don't need any more chars, we know the current string we have from start to end contains
32
+
# all the characters present in our target, so we can check if it's length is less than the the one we currently have
33
+
# if it's length is less, we update the min length we've found so far and then update the shortest string we've found
34
+
# Every time we shrink the window we have to check if we're removing a character we actually needed and then update charsNeeded
35
+
# if we removed a character that was required. Finally we shrink the window from the left by incrementing start
| Number of Connected Components in an Undirected Graph | 4 | 323 | Medium | Depth First Search |[Python](Python/Connected_Components_In_Undirected_Graph.py)|
99
+
| Number Of Islands | 1 |[200](https://leetcode.com/problems/number-of-islands/)| Medium | Depth First Search |[Python](Python/Number_Of_Islands.py) / [C](C/Number_Of_Islands.c)|
100
+
| Flood Fill |2|[733](https://leetcode.com/problems/flood-fill/)| Easy | Depth First Search |[Python](Python/Flood_Fill.py)|
| Number of Connected Components in an Undirected Graph | 4 |[323](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/)| Medium | Depth First Search |[Python](Python/Connected_Components_In_Undirected_Graph.py)|
0 commit comments