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
| # Definition for a binary tree node. | |
| # class TreeNode: | |
| # def __init__(self, val=0, left=None, right=None): | |
| # self.val = val | |
| # self.left = left | |
| # self.right = right | |
| class Solution: | |
| def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]: |
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
| import re | |
| class Solution: | |
| def validIPAddress(self, queryIP: str) -> str: | |
| def is_ipv4(ip: str) -> bool: | |
| ip = ip.split(".") | |
| if len(ip) == 4: # must have 4 different parts |
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 sortedcontainers import SortedDict | |
| class Element: | |
| def __init__(self, v): | |
| self.v = v # Current value | |
| self.p = None # Previous pointer | |
| self.n = None # Next Pointer | |
| return | |
| class MaxStack: |
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=Jn4mbZVkeik | |
| from collections import OrderedDict | |
| # A class to hold the value and the counter of the key | |
| class KeyNode(): | |
| def __init__(self, val, cnt): | |
| self.val = val | |
| self.cnt = cnt |
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 : https://leetcode.com/problems/regular-expression-matching/ | |
| # Reference : https://www.youtube.com/watch?v=HAA8mgxlov8 | |
| from functools import cache | |
| class Solution: | |
| def isMatch(self, s: str, p: str) -> bool: | |
| a = len(s) | |
| b = len(p) |
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, Tuple | |
| def merge_sort(arr: List): | |
| # Sort and Merge | |
| def merge(first: Tuple, second: Tuple) -> Tuple: | |
| # Init | |
| a, n = first | |
| b, m = second |
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 TreeNode(object): | |
| def __init__(self, val): | |
| self.val = val | |
| self.left = None | |
| self.right = None | |
| 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.quora.com/How-do-you-calculate-the-sum-of-the-numbers-between-x-and-y | |
| # This formula does't depened on which one of a or b is greater | |
| def sumBetweenNums(a,b): | |
| m = abs(a-b+1) | |
| k = a+b | |
| r = (m*k)/2 | |
| return r | |
| # OR |
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://stackoverflow.com/questions/69236733/remove-n-consecutive-elements-from-array-so-that-amplitude-of-remaining-element/69237657#69237657 | |
| # remove K consecutive elements from array, so that amplitude of remaining elements is minimal | |
| from itertools import accumulate as acc | |
| from math import inf | |
| A = [3,5,1,3,9,8] | |
| B = A[::-1] | |
| K = 3 | |
| N = len(A) |
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=AuYujVj646Q | |
| class Solution: | |
| def minDistance(self, x: str, y: str) -> int: | |
| # Init | |
| n = len(x) | |
| m = len(y) | |