1636. Sort Array by Increasing Frequency
Sort by occurences in ascending order, then sort by elements themself in descending order
1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance
Dijkstra's Algorithm Reference
Path of tree is represented by the character, initialize a node per path.
1038. Binary Search Tree to Greater Sum Tree
1110. Delete Nodes And Return Forest
1382. Balance a Binary Search Tree
1717. Maximum Score From Removing Substrings
703. Kth Largest Element in a Stream
Python Heap Queue Algorithm Document
Initialize the heap with a empty list x = []
or convert a list with heapq.heapify(x)
heap[0]
is the smallest item in the heap.
heapq.heappush(heap, item)
item
onto the heapheapq.heappop(heap)
item
from the heapheap
is empty, IndexError
is raiseditem
without popping it, use heap[0]
heapq.heappushpop(heap, item)
item
onto the heap, then pop and return the smallest item
from the heapheapq.heappush()
then heapq.heappop()
heapq.heapreplace(heap, item)
item
from the heap, then push the value item
onto the heapheap
is empty, IndexError
is raisedheapq.heappop()
then heapq.heappush()
Heap elements can also be tuples.
The sum of any array can be rewritten as follow:
for all
For a subarray with size greater than 1 to exist such that the sum of its elements is divisivble by k
, must equal to 0 for some , the reason for is because the length of subarray must be greater than or equal to 2
If we modules both sides of the above equation:
Find the and that satisfy this condition
2134. Minimum Swaps to Group All 1's Together II
Assume the window size is sliding_window_size
.
k
elements to the end of array, faster273. Integer to English Words
Split the numbers from least significant digits into chunks
How can this problem be reduced into a factorization problem?
The first part check if n
is 1 (special case).
The second part check the multiple of 2.
The third part check other prime factors.
The last part is when the remaining of n
is a prime factor, add it to the list.
str
) of original number num
by bin()
.The return value of bin()
would contain 0b
in them.
For example bin(5)
returns 0b101
solution where is the number of bits used to represent the number
num
by bit_length()
.The return value of bit_length()
is the number of bits used to represent the number.
For example bin(5)
returns 3
(0b101
takes 3 bits to represent).
solution where is the number of bits used to represent the number
2807. Insert Greatest Common Divisors in Linked List
Euclidean Algorithm
start_index
and end_index
are the indices that's ready for the next insertion.
Thus having to recalculate index again when trying to getFront()
and getRear()
.
1331. Rank Transform of an Array
Using basic Linear Search
would trigger TLE at this question.
Use Binary Search instead.
This can be used to get how many set bits are in a number, for example:
1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance
這題很重要,要會!!!
這題我是看解答的,思路如下。
先從小到大排列,第一個迴圈用來固定第一個變數,這樣就是 2sum 的題目。
再來將第二個變數設為第一個變數位置加一,第三個變數位置設為佇列尾端,然後將三數相加跟零做比較 :
比零大表示第三個變數太大,往左移;比零小表示第二個變數太小,往右移。