|
| 1 | +intarr = [ |
| 2 | + [3, 1, 4, 2, 5], |
| 3 | + [9, 1, 3], |
| 4 | + [3, 3], |
| 5 | +] |
| 6 | + |
| 7 | +def solution(intarr): |
| 8 | + res = 0 |
| 9 | + for row in range(len(intarr)): |
| 10 | + minval, maxval = float('inf'), float('-inf') |
| 11 | + for num in intarr[row]: |
| 12 | + minval = min(minval, num) |
| 13 | + maxval = max(maxval, num) |
| 14 | + res += (maxval - minval) |
| 15 | + return res |
| 16 | + |
| 17 | +def solution2(intarr): |
| 18 | + res = 0 |
| 19 | + for row in range(len(intarr)): |
| 20 | + for i, inum in enumerate(intarr[row]): |
| 21 | + if inum == 1: |
| 22 | + continue |
| 23 | + for j in range(i+1, len(intarr[row])): |
| 24 | + jnum = intarr[row][j] |
| 25 | + if jnum == 1: |
| 26 | + continue |
| 27 | + if inum % jnum == 0: |
| 28 | + res += inum // jnum |
| 29 | + elif jnum % inum == 0: |
| 30 | + res += jnum // inum |
| 31 | + return res |
| 32 | + |
| 33 | +print(solution(intarr)) |
| 34 | +print(solution2(intarr)) |
| 35 | + |
| 36 | + |
| 37 | +from collections import deque |
| 38 | +import time |
| 39 | +queue = deque() |
| 40 | +def rate_limit(key, intervalInSec, maxLimit) -> bool: |
| 41 | + queue.append((key, time.time())) |
| 42 | + |
| 43 | + while queue and queue[0][1] < time.time() - intervalInSec: |
| 44 | + queue.popleft() |
| 45 | + |
| 46 | + count = 0 |
| 47 | + for i in range(len(queue)): |
| 48 | + if queue[i][0] == key: |
| 49 | + count += 1 |
| 50 | + if count > maxLimit: |
| 51 | + return False |
| 52 | + |
| 53 | + return True |
| 54 | + |
| 55 | + |
| 56 | +def prettifyJson(s): |
| 57 | + """ |
| 58 | + Prettify a JSON string. |
| 59 | +
|
| 60 | + Args: |
| 61 | + s (str): The JSON string to prettify. |
| 62 | +
|
| 63 | + Returns: |
| 64 | + str: The prettified JSON string. |
| 65 | + """ |
| 66 | + |
| 67 | + # Indentation level. |
| 68 | + indent = 0 |
| 69 | + |
| 70 | + # Result string. |
| 71 | + r = "" |
| 72 | + |
| 73 | + # Iterate over the characters in the JSON string. |
| 74 | + for x in s: |
| 75 | + # If we encounter a '{', we increase the indentation level. |
| 76 | + if x in ['{']: |
| 77 | + indent += 2 |
| 78 | + r += x + "\n" + indent * "*" |
| 79 | + # If we encounter a '[', we add newline character and add [ and then increase the indentation level. |
| 80 | + elif x in ['[']: |
| 81 | + # r += "\n" + indent * "*" |
| 82 | + indent += 2 |
| 83 | + r += x + "\n" + indent * "*" |
| 84 | + # If we encounter a '}' or ']', we decrease the indentation level. |
| 85 | + elif x in ['}', ']']: |
| 86 | + indent -= 2 |
| 87 | + r += "\n" + indent * "*" + x |
| 88 | + # If we encounter a ',', we add a newline and indent the next line. |
| 89 | + elif x in [',']: |
| 90 | + r += x + "\n" + indent * "*" |
| 91 | + # Otherwise, we just add the character to the output string. |
| 92 | + else: |
| 93 | + r += x |
| 94 | + |
| 95 | + return r |
| 96 | + |
| 97 | + |
| 98 | +# Example |
| 99 | +s = '{"username":"Jonas","devices":["iPhone 13 Pro","Samsung Galaxy S30"]}' |
| 100 | + |
| 101 | +# Pretty print the JSON string. |
| 102 | +print(prettifyJson(s)) |
| 103 | + |
| 104 | + |
| 105 | +class Node: |
| 106 | + def __init__(self, key, val): |
| 107 | + self.key, self.val = key, val |
| 108 | + self.prev = self.next = None |
| 109 | + |
| 110 | +# use hashmap and double linkedlist, hashmap stores key => Node |
| 111 | +# double linked list left = Least Recently Used, right = most recently used |
| 112 | +class LRUCache: |
| 113 | + def __init__(self, capacity: int): |
| 114 | + self.cap = capacity |
| 115 | + self.cache = {} # key: key => node |
| 116 | + # dummy nodes for left and right, |
| 117 | + # left = Least Recently Used, right = most recently used |
| 118 | + self.left, self.right = Node(0, 0), Node(0, 0) |
| 119 | + self.left.next, self.right.prev = self.right, self.left |
| 120 | + |
| 121 | + def remove(self, node): |
| 122 | + prev, nxt = node.prev, node.next |
| 123 | + prev.next = nxt |
| 124 | + nxt.prev = prev |
| 125 | + |
| 126 | + def insert(self, node): |
| 127 | + # insert at the most recently used |
| 128 | + prev, nxt = self.right.prev, self.right |
| 129 | + prev.next = nxt.prev = node |
| 130 | + node.next, node.prev = nxt, prev |
| 131 | + |
| 132 | + def get(self, key: int) -> int: |
| 133 | + if key in self.cache: |
| 134 | + # TODO: update to most recent |
| 135 | + self.remove(self.cache[key]) |
| 136 | + self.insert(self.cache[key]) |
| 137 | + return self.cache[key].val |
| 138 | + return -1 |
| 139 | + |
| 140 | + def put(self, key: int, value: int) -> None: |
| 141 | + # re-insert the (key, value) if it already exists (so it's most recently used) |
| 142 | + if key in self.cache: |
| 143 | + self.remove(self.cache[key]) |
| 144 | + self.cache[key] = Node(key, value) |
| 145 | + self.insert(self.cache[key]) |
| 146 | + |
| 147 | + if len(self.cache) > self.cap: |
| 148 | + # remove and delete the LRU from the hashmap |
| 149 | + lru = self.left.next |
| 150 | + self.remove(lru) |
| 151 | + del self.cache[lru.key] |
0 commit comments