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 | |
class Solution: | |
def isPalindrome(self, x): | |
''' | |
:type x: int | |
:rtype: bool | |
''' | |
if 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
# you can sort one time queue like below | |
# you can sort dynamic queue using priority queue - for larger n | |
def waiting_time(**n: dict): | |
"""Optimal queue wait time. | |
>>> waiting_time(t1=15, t2=20, t3=10) | |
35 | |
""" | |
waiting_time = 0 | |
wait_list = {} |
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
# See: https://stackoverflow.com/a/55235583 | |
import concurrent.futures as futures | |
import queue | |
from threading import BoundedSemaphore | |
class ThreadPoolBatchedExecutor(futures.ThreadPoolExecutor): | |
def __init__(self, limit, *args, **kwargs): | |
super().__init__(*args, **kwargs) |
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
"""Kotpees Card Game.""" | |
import enum | |
import random | |
from typing import NamedTuple, Tuple, Union, Optional | |
class Person(str): | |
... |
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
#!/bin/bash | |
# See https://github.com/docker-library/mongo/pull/63 | |
docker run --rm --volumes-from my-mongo-server mongo unlink "/data/db/mongod.lock" | |
docker run --rm --volumes-from my-mongo-server mongo --repair |
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 non_repeat_substring(string): | |
from collections import Counter | |
max_ctr = Counter() | |
left_idx = 0 | |
distinct = 0 | |
max_distinct = float('-inf') | |
for right_idx, r_char in enumerate(string): | |
if not max_ctr[r_char]: | |
distinct += 1 | |
max_ctr[r_char] += 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
from collections import Counter | |
def fruits_into_baskets(fruits): | |
max_ctr = Counter() | |
left_idx = 0 | |
distinct = 0 | |
max_k = 2 | |
ct = float('-inf') | |
for right_idx, r_char in enumerate(fruits): | |
if not max_ctr[r_char]: |
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 longest_substring_with_k_distinct(chars, k): | |
left_idx = 0 | |
distinct_ct = 0 | |
max_ln = -1 | |
counter = defaultdict(int) | |
for right_idx, char in enumerate(chars): | |
# print("max_ln={}".format(max_ln)) | |
# print( |
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 smallest_subarray_with_given_sum(s, arr): | |
min_ct = float('inf') | |
t_head = head_idx = 0 | |
running_sum = 0 | |
t_sum = 0 | |
for tail_idx, num in enumerate(arr): | |
running_sum += num | |
while running_sum >= s: | |
min_ct = min(min_ct, tail_idx - head_idx+1) | |
running_sum -= arr[head_idx] |
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 max_sub_array_of_size_k(k, arr): | |
max_sum = -1 | |
running_sum = start_idx = 0 | |
for tail_idx, num in enumerate(arr): | |
running_sum += num | |
if tail_idx - start_idx == k: | |
running_sum -= arr[start_idx] | |
start_idx += 1 | |
max_sum = (running_sum and max(max_sum, running_sum)) or max_sum | |
return max_sum |