Skip to main content

Leetcode 207. Course Schedule. Python

207Course Schedule


There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.

  • For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.

Return true if you can finish all courses. Otherwise, return false.

 

Example 1:

Input: numCourses = 2, prerequisites = [[1,0]]
Output: true
Explanation: There are a total of 2 courses to take. 
To take course 1 you should have finished course 0. So it is possible.

Example 2:

Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
Output: false
Explanation: There are a total of 2 courses to take. 
To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.

 

Constraints:

  • 1 <= numCourses <= 2000
  • 0 <= prerequisites.length <= 5000
  • prerequisites[i].length == 2
  • 0 <= ai, bi < numCourses
  • All the pairs prerequisites[i] are unique.

 


class Solution:
    def canFinish(self, n: int, preq: List[List[int]]) -> bool:
        
        hm = { i: [] for i in range(n)}
        vs = set()
        
        for c,p in preq :
            hm[c].append(p)
            
        def dfs(c):
            if c in vs:
                return False
            if hm[c] == []:
                return True
            
            vs.add(c)
            
            for p in hm[c]:
                if not dfs(p): return False
                
            vs.remove(c)
            hm[c] = []
            
            return True
        
        for c in range(n):
            if not dfs(c): 
                return False
        return True



Explaination :



















Comments

Popular posts from this blog

May-6 2020 Challenge

  6.   Majority Element Given an array of size  n , find the majority element. The majority element is the element that appears  more than   ⌊ n/2 ⌋  times. You may assume that the array is non-empty and the majority element always exist in the array. Example 1: Input: [3,2,3] Output: 3 Example 2: Input: [2,2,1,1,1,2,2] Output: 2 Solution in Java  class Solution {     public int majorityElement(int[] num) {         int m = num[0], cnt= 1;     for (int i = 1; i < num.length; i++) {         if (cnt == 0) {             m= num[i];             cnt = 1;         } else if (num[i] == m) {             cnt++;         } else              cnt--;    }      return m;...

Leetcode 424. Longest Repeating Character Replacement. Python (Sliding Window)

  424 .  Longest Repeating Character Replacement You are given a string  s  and an integer  k . You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most  k  times. Return  the length of the longest substring containing the same letter you can get after performing the above operations .   Example 1: Input: s = "ABAB", k = 2 Output: 4 Explanation: Replace the two 'A's with two 'B's or vice versa. Example 2: Input: s = "AABABBA", k = 1 Output: 4 Explanation: Replace the one 'A' in the middle with 'B' and form "AABBBBA". The substring "BBBB" has the longest repeating letters, which is 4.   Constraints: 1 <= s.length <= 10 5 s  consists of only uppercase English letters. 0 <= k <= s.length Solution :  class Solution: def characterReplacement(self, s: str, k: int) -> int: hm = {} ans = 0 ...

Longest Substring Without Repeating Characters - Leetcode 3 - Python

Given a string s, find the length of the longest substring without repeating characters. class Solution:     def lengthOfLongestSubstring(self, s: str) -> int:         charSet = set()         left = 0         ans = 0         for right in range(len(s)):             while s[right] in charSet:                 charSet.remove(s[left])                 left+=1             charSet.add(s[right])             ans = max(ans, right-left+1)         return ans                  Explained :