Need continuous some smaller/larger
value? Use heap
max or min as you need.
when you have something circular
, most of the time you can have one variable
and another can be calculated
using size
, current value
and doing %
LRUCache
can be implemented
- Hashmap + Doubly Linked List
- Python Dict/OrderDict
- HashMap + List
At least I've tested them for fun :p
(k := next(iter(d)), d.pop(k))
will remove the leftmost (first) item (if it exists) from a dict
object.
And if you want to remove the right most/recent value from the dict
d.popitem()
From python 3.7 dict
guarantees that order will be kept
as they inserted, and popitem
will use LIFO order but we need FIFO type system. so we need OrderedDict
which have popIten(last = T/F)
for this req.
One thing, next(iter(dict))
will return the first key of the dict
leetcode problem 287. Find the Duplicate Number has 7 solving approach. WOW!!!
Floyd's Tortoise and Hare
is used to detect linked list cycle.
time.perf_counter()
always returns the float value of time in seconds. while pref_counter_ns()
always gives the integer value of time in nanoseconds.
t1_start = perf_counter()
t1_stop = perf_counter()
print("Elapsed time:", t1_stop, t1_start)
Substring a String
string[start:end:step]
string[2:6]
string[-1]
tring[-1]
string[-5:]
string[1:-4]
Amortized time
is the way to express the time complexity when an algorithm has the very bad time complexity only once in a while
besides the time complexity that happens most of time. Good example would be an ArrayList
which is a data structure that contains an array and can be extended.
Started keeping online notes
!