Category Archives: algorithm

lc1639

Considering words = [“acca”,”bbbb”,”caca”], target = “aba” dp[1][2] means the result for target “ab” and words=[“acc”, “bbb”, “cac”] To calculate dp[i][j]: 1. dp[i][j] = dp[i][j – 1]. This means when words has is 1 letter small, the result to cover target[0,…,i]. In this case, dp[2][3] = dp[2][2] 2. In words array, for j column, when there is x… Read More »

lc1648

This problem needs to aggregate to (num, count) tree map. Always stick to top 2 elements. After processed first element, aggregate the count of first element to second element. For example, 77744444, orders = 10. For TreeMap, it has (7, 3), (4, 5) It can be added to answer by below numbers: 7 7 7 … Read More »

lc1641

Assume a, e, i, o, u variables mean the number of elements which starts with a, e, i, o, u. Take below as example, length is 5 now. There are a number of elements which starts with a, e number of elements which starts with e, etc. When extend the length to 6. When adding… Read More »

leetcode 1605

The point is to assign each rowSum value to each cell in each row, assign each colCum value to each cell in each row. For example, need to assign 6 to m[0][1], m[1][1], m[2][1]. Check this video for detail process.  

gray code

This is the gray code. The changing rule can be: rule1: update the rightmost bit rule2: for xxx100…00, it can change the digit x which is next to 100..00 See lc1611 For example, 1001000 has 2 ways of change: 1. rule1 -> 1001001 2. rule2 -> 1011000 Another example, 1111 has 2 ways of change: 1. rule1 ->… Read More »

Setup specific python version in virtualenv; setup virtualenv for PyCharm

Download the python version you want, and install. https://www.python.org/downloads/ In cmd, type python3.8 Find the python3.8 Library path. Create python3.8 virtualenv virtualenv -p /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 venv Activate venv source venv/bin/activate Check python version python –version In PyCharm, select the created virtual env In not existed in Project Interpreter, then click “Show All”, click ‘+’ to add a new… Read More »

nginx

install nginx https://www.techrepublic.com/article/how-to-run-nginx-as-a-docker-container/ client_header_timeout, time when all header packets arrives nginx. client_body_timeout, time when nginx hasn’t received a body packet, then timeout. NOT receiving whole body time. Kinda like read timeout. send_timeout, similar to client_body_timeout. when nginx sends response to client, the time when nginx hasn’t received an ACK. keepalive_timeout, connection becomes to idle when… Read More »

lc1546

For example, each pair is labeled as [l1, r1]. Because the pairs sequence is ordered by r. So, we can just iterate it. For current pair, if the l is overlapped with previous pair, then don’t consider it. This is a greedy strategy.

lc1563

dp[i][j] is the result between stone i, and stone j. If left is greater, then dp[from][from+delta] = sum(i, from+delta) + dp[i][from+delta] Else right part If both left and right are same, then choose the one which dp has larger value

lc1558

There are actually 2 types of OPs. One is to make all digit multiplying by 2, this can be shared by all numbers together. Another is adding one. This can’t be shared. So result would be count how many one digit, plus the highest bit count.