Types
Algorithm
Data structure
# LC 791. Custom Sort String
# ...
= Counter(s)
s_map = ""
res for o in order:
if o in s_map:
+= (o * s_map[o])
res del s_map[o]
for s in s_map:
+= (s * s_map[s])
res # ...
import collections
= ['a','b','c','c']
s = collections.Counter(s)
cprint (c)
print (c.keys())
print (c.values())
# 451 Sort Characters By Frequency
import collections
= ['a','b','c','c']
s = collections.Counter(s).most_common()
count for item, freq in count:
print (item, freq)
#c 2
#a 1
#b 1
import collections
= ['a','b','c','c']
s = collections.defaultdict(int)
count for i in s:
+= 1
count[i]
print (count)
print (dict(count))
import collections
=[('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
s= collections.defaultdict(list)
count for k, v in s:
count[k].append(v)
print (count)
print (count.keys())
print (count.values())
print(count.items())
# LC # 554 rick Wall
import collections
87]: _counter = collections.Counter()
In [
88]: _counter
In [88]: Counter()
Out[
89]: _counter.update([1])
In [
90]: _counter
In [90]: Counter({1: 1})
Out[
91]: _counter.update([1])
In [
92]: _counter
In [92]: Counter({1: 2})
Out[
93]: _counter.update([2])
In [
94]: _counter
In [94]: Counter({1: 2, 2: 1}) Out[
OrderedDict
(
hashmap + linked list)# LC 146 LRU Cache
# There is a structure called ordered dictionary, it combines behind both hashmap and linked list. In Python this structure is called OrderedDict and in Java LinkedHashMap.
# https://docs.python.org/3/library/collections.html#collections.OrderedDict
# https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/367557/
# https://www.w3help.cc/a/202107/420653.html
"""
# OrderedDict = hashmap + linked list
# CAN make dict ordering (default dict is NOT ordering)
# Return an instance of a dict subclass that has methods specialized for rearranging dictionary order
* popitem(last=True)
The popitem() method for ordered dictionaries returns and removes a (key, value) pair. The pairs are returned in LIFO order if last is true or FIFO order if false.
* move_to_end(key, last=True)
Move an existing key to either end of an ordered dictionary. The item is moved to the right end if last is true (the default) or to the beginning if last is false. Raises KeyError if the key does not exist:
"""
#----------------------------
# example 0
#----------------------------
# default dict
34]: d = {}
In ['a'] = 'A'
...: d['b'] = 'B'
...: d['c'] = 'C'
...: d['d'] = 'D'
...: d['e'] = 'E'
...: d[
...:for k, v in d.items():
...: print (k, v)
...:
...:# NON ordering
a A
b B
c C
d D
e E
# OrderedDict
35]: from collections import OrderedDict
In [= OrderedDict()
...: d 'a'] = 'A'
...: d['b'] = 'B'
...: d['c'] = 'C'
...: d['d'] = 'D'
...: d['e'] = 'E'
...: d[
...:for k, v in d.items():
...: print (k, v)
...:
...:
# ordering !!!
a A
b B
c C
d D
e E
#----------------------------
# example 1
#----------------------------
28]: d = OrderedDict.fromkeys('abcde')
In [
29]: d
In [29]: OrderedDict([('a', None), ('b', None), ('c', None), ('d', None), ('e', None)])
Out[
30]: d.move_to_end('b')
In [
31]: "".join(d)
In [31]: 'acdeb'
Out[
32]:
In [
32]: d.move_to_end('b', last=False)
In [
33]: "".join(d)
In [33]: 'bacde'
Out[
#----------------------------
# example 2
#----------------------------
class LastUpdatedOrderedDict(OrderedDict):
'Store items in the order the keys were last added'
def __setitem__(self, key, value):
super().__setitem__(key, value)
self.move_to_end(key)
#----------------------------
# example 3
#----------------------------
from time import time
class TimeBoundedLRU:
"LRU Cache that invalidates and refreshes old entries."
def __init__(self, func, maxsize=128, maxage=30):
self.cache = OrderedDict() # { args : (timestamp, result)}
self.func = func
self.maxsize = maxsize
self.maxage = maxage
def __call__(self, *args):
if args in self.cache:
self.cache.move_to_end(args)
= self.cache[args]
timestamp, result if time() - timestamp <= self.maxage:
return result
= self.func(*args)
result self.cache[args] = time(), result
if len(self.cache) > self.maxsize:
self.cache.popitem(0)
return result
# LC 791. Custom Sort String
# V0
# IDEA : COUNTER
from collections import Counter
class Solution(object):
def customSortString(self, order, s):
= Counter(s)
s_map = ""
res for o in order:
if o in s_map:
+= (o * s_map[o])
res del s_map[o]
for s in s_map:
+= s * s_map[s]
res return res