This file contains 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 topKFrequent(nums: List[int], k: int) -> List[int]: | |
freq_by_num = {} | |
max_freq = float("-inf") | |
for num in nums: | |
freq_by_num[num] = freq_by_num[num] + 1 if num in freq_by_num else 1 | |
max_freq = max(max_freq, freq_by_num[num]) | |
nums_by_freq = {} | |
for num, freq in freq_by_num.items(): | |
nums_by_freq[freq] = nums_by_freq[freq] + [num] if freq in nums_by_freq else [num] |
This file contains 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 find_min3(x): | |
# print(x) | |
if len(x) == 1 or x[0] < x[-1]: | |
return x[0] | |
elif x[0] == x[-1]: | |
return find_min3(x[1:]) #eliminate the duplicate | |
split_index = len(x)//2 | |
left = x[:split_index] | |
right = x[split_index:] |
This file contains 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
""" | |
Given a log file of user_id, timestamp & page visited find the 10 most common triplets, where a triplet is an occurrence of 3 pages visited sequentially by the same user. | |
1, 1, /home | |
1, 2, /shipments | |
1, 3, /labels | |
1, 4 /something | |
1, 5 /something | |
2, 4, /home | |
... |
This file contains 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
user0 = ["/start", "/pink", "/register", "/orange", "/red", "a"] | |
user1 = ["/start", "/green", "/blue", "/pink", "/register", "/orange", "/one/two"] | |
user2 = ["a", "/one", "/two"] | |
user3 = ["/pink", "/orange", "/yellow", "/plum", "/blue", "/tan", "/red", "/amber", "/HotRodPink", "/CornflowerBlue", "/LightGoldenRodYellow", "/BritishRacingGreen"] | |
user4 = ["/pink", "/orange", "/amber", "/BritishRacingGreen", "/plum", "/blue", "/tan", "/red", "/lavender", "/HotRodPink", "/CornflowerBlue", "/LightGoldenRodYellow"] | |
user5 = ["a"] | |
This file contains 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 | |
def reverse_preserve(input_str: List[str]) -> None: | |
index0 = 0 | |
index1 = len(input_str) - 1 | |
while index0 < index1: | |
left = input_str[index0] | |
right = input_str[index1] | |
if left.isalpha() and right.isalpha(): |
This file contains 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 Dict, Tuple, List | |
Letter = str | |
Type = str | |
AST = List[Tuple[Letter, 'AST']] | |
def build_ast(tokens: List[Tuple[Letter, Type]]) -> AST: | |
if len(tokens) == 0: | |
return [] |
This file contains 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 find_pairs(array1: List[int], array2: List[int], x: int, y: int) -> int: | |
return len([(i,j) for i in array1 for j in array2 if x< i*i + j*j < y]) |
This file contains 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 | |
#lengths | |
def test_max_prod(total: int, acc = None) -> List[int]: | |
if acc is None: | |
acc = [] | |
if total == 0: | |
return acc | |
else: | |
max_list = [] | |
for i in range(1, total + 1): |
This file contains 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
# 1 2 3 -> 3 | |
# 1 2 8 6 7 -> 4 | |
# 1 2 6 7 | |
# 1 2 -99 3 -97 -96 -95 4 5 => 4 | |
# 1: 1 | |
# 2: 2 | |
# -99: 1 | |
# 3: 3 |
This file contains 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
# string processing | |
# remove sequential duplicate characters from a string | |
# "aaabbccccccfffdde" -> "abcfde" | |
#"abcdefghijklmnop" | |
# b = num of boundaries | |
# n * log(n) | |
def remove_repeated(x: str) -> str: | |
if len(x) <= 1: |