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
| nums = [0,1,0,2,1,0,1,3,2,1,2,1] | |
| n = len(nums) | |
| # Function to get max to the left | |
| # of each index of an array | |
| def maxToLeft(arr): | |
| # Nothing left for first index | |
| maxLeft = [-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
| # Reference : https://www.youtube.com/watch?v=IOMjN6r7ju8 | |
| class Solution: | |
| def maxProduct(self, nums: List[int]) -> int: | |
| # NOTE: The crux of the solution is | |
| # to look at the array from both the ends. | |
| # That is in forward and in reverse order. | |
| # Also one more important concept is | |
| # anything multiplied to 0 will loose |
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
| nums = [1,4,3,2,2,1,9] | |
| m = len(nums) | |
| # Next Smallest | |
| next_smallest = [-1] * m | |
| stack = [] | |
| for i in range(m): | |
| if stack: | |
| while stack and stack[-1][1] > nums[i]: |
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 math import inf | |
| class Solution: | |
| def maxSubArray(self, nums: List[int]) -> int: | |
| curr_sum = -inf | |
| curr_max = -inf | |
| for num in nums: | |
| curr_sum = max(curr_sum + num, num) | |
| curr_max = max(curr_max, curr_sum) | |
| return curr_max |
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
| # A function which returns path from root to the given target | |
| def getPath(curr, target, path: List): | |
| # if current node is not none | |
| if curr: | |
| # Add current node to the path | |
| path.append(curr) | |
| # if current node is target return path |
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 for union find with path compression | |
| class UnionFind: | |
| def __init__(self, n): | |
| self.n = n | |
| self.roots = [i for i in range(n)] | |
| self.ranks = [0 for _ in range(n)] | |
| return | |
| # find with path compression | |
| def find(self, x) -> 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 simplifyPath(self, path: str) -> str: | |
| # Reconstuct path to suit the logic | |
| # The curx is to maintain "/" at the end | |
| # of the path, and the logic revolve around | |
| # it. | |
| # Add "/" to last if not already there |
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 divide(self, dividend: int, divisor: int) -> int: | |
| # -2147483648 through 2147483647 | |
| MIN_VAL = -2147483648 | |
| MAX_VAL = 2147483647 | |
| # If divisor is 1 or -1 | |
| if abs(divisor) == 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
| # Reference : https://www.youtube.com/watch?v=EKZhEN9P2-I | |
| import heapq | |
| class Solution: | |
| def maxEvents(self, events: List[List[int]]) -> int: | |
| # Sort the events in ascending order of their start time | |
| events.sort(key = lambda x: x[0]) |
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 typing import List | |
| class TrieNode: | |
| def __init__(self): | |
| self.next = dict() # pointer to next node, starting with a key char | |
| self.end = False # by default false | |
| class Trie: | |
| def __init__(self): |