This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 1010. Pairs of Songs With Total Durations Divisible by 60 | |
| # Reference : https://www.youtube.com/watch?v=toYgBIaUdfM | |
| from collections import defaultdict | |
| class Solution: | |
| # Brute Force | |
| #def numPairsDivisibleBy60(self, time: List[int]) -> int: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def twoSum(self, nums: List[int], target: int) -> List[int]: | |
| d = {} # Keep track of whaterver we have see so far | |
| for i,num in enumerate(nums): # scan array one by one | |
| if target-num in d: # if the difference is in dictionary | |
| return sorted([i, d[target-num]]) # return i, and dictionary value | |
| d[num] = i # add to dictionary the current num-index pair | |
| return [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections import defaultdict | |
| from itertools import combinations | |
| class Solution: | |
| def maximalNetworkRank(self, n: int, roads: List[List[int]]) -> int: | |
| # Variables | |
| rank = 0 | |
| indegree = defaultdict(lambda: set()) | |
| nodes = [i for i in range(n)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Reference: https://www.youtube.com/watch?v=zKhbe9IOATY | |
| class Solution: | |
| def hourAngle(self, hour: int, minutes: int) -> float: | |
| # It takes 12 hours to cover all 360 degree | |
| # For each hours it covers 360/12 degrees, i.e. 30 degrees | |
| # Now there between two hours 30 degrees are covered, and | |
| # it takes 60 mins between two hours, so for each mins | |
| # it takes 30/60 degrees between two hours, i.e., 0.5 degrees |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections import defaultdict, deque | |
| class Solution: | |
| def __init__(self): | |
| self.n = 0 | |
| self.islands = defaultdict(lambda: set()) | |
| return | |
| def withinBoundary(self, node: Tuple) -> bool: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from matplotlib import pyplot as plt | |
| import numpy as np | |
| m = [ | |
| [1,1,0,1,0,0,1,1,0,0], | |
| [0,1,1,1,1,1,1,1,0,0], | |
| [0,0,1,0,1,1,0,0,0,0], | |
| [0,0,0,1,1,0,1,1,0,0], | |
| [0,0,0,1,0,0,1,0,1,0], | |
| [0,0,0,0,1,1,1,1,1,1], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def calculate(self, s: str) -> int: | |
| # Strip and replace all the spaces | |
| s = s.strip().replace(" ","") | |
| # Evaluate | |
| def update(stack: List, curr: str, operator: str): | |
| if curr: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Find pivot in a sorted but shifted/rotated array using binary search | |
| def pivot(arr, low, high): | |
| if low <= high: | |
| # Calc Mid | |
| mid = low + (high-low)//2 | |
| # Base Case: monotonic sequence e.g. [0,1,2,3] | |
| if arr[low] < arr[high]: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Leetcode 146. LRU Cache | |
| class Node: | |
| def __init__(self, key: int, value: int, prev: 'Optional[Node]' = None, next: 'Optional[Node]' = None): | |
| self.key = key | |
| self.value = value | |
| self.prev = prev | |
| self.next = next | |
| return |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Reference : https://www.youtube.com/watch?v=Sj5Z-jaE2x0 | |
| from typing import List, Set, Dict | |
| class Solution: | |
| def findMin(self, start_node: int, is_processed: set, spg: List, graph: Dict) -> int: | |
| node = start_node | |
| value = float("inf") |