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
| def encode(s): | |
| n = len(s) | |
| res = [] | |
| i = 0 | |
| while i < n: | |
| ctr = 1 | |
| while i < n - 1 and s[i] == s[i + 1]: | |
| ctr += 1 | |
| i += 1 | |
| if ctr == 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
| def pow(a, b): | |
| curr = a | |
| res = 1 | |
| while b: | |
| if b & 1: | |
| res *= curr | |
| b = b >> 1 | |
| curr *= curr | |
| return res |
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 Node: | |
| def __init__(self, val = None): | |
| self.left = None | |
| self.right = None | |
| self.val = val | |
| self.end = False | |
| class TrieMap: | |
| def __init__(self): | |
| self.root = Node() |
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 Node: | |
| def __init__(self): | |
| self.child = {} | |
| self.end = False | |
| class Trie: | |
| def __init__(self): | |
| self.root = Node() | |
| def insert(self, s): |
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 | |
| def radical(n): | |
| primes = [True] * (n + 1) | |
| primes[0] = primes[1] = False | |
| for i in range(n + 1): | |
| if primes[i]: | |
| j = 2 | |
| while i * j <= n: | |
| primes[i * j] = False |
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
| def inverse(x, f, k = 15): | |
| beg = 0 | |
| end = 1 | |
| while f(end) < x: | |
| beg += 1 | |
| end += 1 | |
| while k: | |
| mid = (beg + end) / 2 | |
| if f(mid) > x: | |
| end = mid |
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
| def genSparse(arr): | |
| n = len(arr) | |
| k = len("{:b}".format(n)) | |
| table = [[float('inf')] * k for _ in range(n)] | |
| for l in range(k): | |
| for i in range(n): | |
| if l == 0: | |
| table[i][l] = arr[i] | |
| else: | |
| a = table[i][l - 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 Node: | |
| def __init__(self, start, end, min = float('inf')): | |
| self.start = start | |
| self.end = end | |
| self.left = None | |
| self.right = None | |
| self.min = min | |
| class SegTree: | |
| def __init__(self, nums): |
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 Node: | |
| def __init__(self, val = None, ctr = 1, left = None, right = None): | |
| self.val = val | |
| self.ctr = ctr | |
| self.left = left | |
| self.right = right | |
| class BST: | |
| def __init__(self): | |
| self.root = None |
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 math | |
| def mobius(n): | |
| ctr = 0 | |
| k = int(math.sqrt(n)) + 1 | |
| primes = [True] * k | |
| primes[0], primes[1] = False, False | |
| for i in range(k): | |
| if primes[i]: | |
| j = 2 |