Category Archives: algorithm

leetcode 928

For each virus node, count how many normal it connects. By doing dfs, if a normal connects to another virus node, then branch from this normal node won’t count. In below case, there are 2 virus node [0, 5]. node 1 can connect with [1, 2, 8, 9]. 5 can connect with [6, 7]. [3,… Read More »


Assume there are 4 points in first group, labeled with 0, 1, 2, 3. 3 points in second group, labeled with A, B, C. Define dp[i][j]. Use binary to symbolize j, for example dp[2][6] = dp[2][(110)]. This means minimum cost for first 3 (0, 1, 2) points in first group connecting with B, C points in second group.… Read More »

lc1622 Fancy Sequence

at [2], mult=c add=((a+b)*c+d) From [0] to [2], adding change operation of +a, +b, *c, +d, it needs apply [0]*mult + add   Reversely, from [2] to [0], to calculate back with +a, +b, *c, +d, it needs apply ([2] – add) / mult   In all, the calculation is like when appending each value, calculate… Read More »


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 »


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 »


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: update the rightmost big for xxx100…00, it can change the digit x which is next to 100..00 See lc1611 The property of gray code, is that each two adjacent number only have 1 bit difference. For example, in decimal, if we want to update from 011… Read More »

Setup specific python version in virtualenv; setup virtualenv for PyCharm

Download the python version you want, and install. 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 »


install nginx 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 »