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
| seg = {} | |
| def makeSeg(arr, i, j): | |
| if (i, j) in seg: | |
| return seg[(i, j)] | |
| if i == j: | |
| seg[(i, j)] = arr[i] | |
| return arr[i] | |
| mid = (i + j) // 2 | |
| curr = min(makeSeg(arr, i, mid), makeSeg(arr, mid + 1, j)) |
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 functools import cmp_to_key | |
| n = int(input()) | |
| costs = [] | |
| for _ in range(n): | |
| c, x = input().split() | |
| costs.append((int(c), int(x))) | |
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
| t = int(input()) | |
| def numWords(s, i, words, cache): | |
| n = len(s) | |
| if i >= n: | |
| return 0 | |
| if i in cache: | |
| return cache[i] | |
| maxwords = 0 | |
| for j in range(i + 1, n + 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 MyHashMap: | |
| def __init__(self): | |
| self.size = 107 | |
| self.arr = [[] for i in range(self.size)] | |
| def hfn(self, key): | |
| key = ((key >> 16) ^ key) * 0x45d9f3b | |
| key = ((key >> 16) ^ key) * 0x45d9f3b | |
| key = (key >> 16) ^ key |
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 heapq | |
| from collections import defaultdict | |
| class Solution: | |
| def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: | |
| dist = [[0 if cell == 0 else float('inf') for cell in row] for row in mat] | |
| heap = [] | |
| deleted = defaultdict(int) | |
| m = len(mat) | |
| n = len(mat[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
| n = 5 | |
| curr = [1] | |
| for i in range(n): | |
| print(" " * (n - i - 1), end="") | |
| print(" ".join("{}".format(x) for x in curr)) | |
| curr = [0] + curr + [0] | |
| curr = [curr[j] + curr[j + 1] for j in range(i + 2)] |
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 heapq | |
| def arrayManipulation(n, queries): | |
| curr = {} | |
| for a, b, k in queries: | |
| curr[a] = curr.get(a, 0) + k | |
| curr[b + 1] = curr.get(b + 1, 0) - k | |
| heap = list(curr.items()) | |
| ln = len(heap) | |
| heapq.heapify(heap) |
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 nthUglyNumber(self, n): | |
| N = 10000 | |
| uglies = {} | |
| uglies[1] = True | |
| factors = [([2], 2), ([3], 3), ([5], 5)] | |
| while len(factors) > 0: | |
| factor, p = factors.pop(0) | |
| uglies[p] = True | |
| for nn in [2, 3, 5]: | |
| np = p * nn |
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 maxProfit(self, prices): | |
| valIndex = {} | |
| maxProfit = 0 | |
| for i, price in enumerate(prices): | |
| if price not in valIndex: | |
| valIndex[price] = [i, i] | |
| else: | |
| valIndex[price][1] = i | |
| newprices = sorted(valIndex.keys()) | |
| n = len(newprices) |
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 minJumps(arr): | |
| n = len(arr) | |
| paths = [[0]] | |
| visited = set() | |
| valIndexes = {} | |
| for i in range(n): | |
| item = arr[i] | |
| valIndexes[item] = valIndexes.get(item, []) + [i] | |
| while len(paths) > 0: | |
| path = paths.pop(0) |