Data Structure and Algorithm Summary
This post summarizes the common data structure and algorithm knowledge and questions.
This post summarizes the common data structure and algorithm knowledge and questions.
Sometimes normal recursive method will cost a lot of time and not very efficient. In these cases, the DP(Dynamic Programming) can be a good choice. The key of DP is to record the previous solutions.
If a problem has two featrues:
There are two main ways to do DP. The bottom-up is the most common DP method.
Establish a memory data structure(like a list) to store the previous values, avoiding the repeat calculation on that value.
Iterate from the bottom. This is better than the previous one because there is no recursive thus no extra cost.
I felt DP is like a 2-demensional array, which can be reduced to a 1-D array. Usually the row i means the action at step i; the column j means a similar result to the original question.
1 | dp = [[.. for i in range(m)] for j in range(n)] # Create |
dp[i] means subarray s[:i] and you must use s[i-1], return max(dp)
No.53 - Max sum subarray
1 | if dp[i - 1] > 0: |
No.300 - Longest Increasing Subsequence
No.152 - Maximum Product Subarray
dp1 contains the max product; dp2 contains the min product
dp[i] means s[:i], return dp[len(s)]
No.139 - Word Break
No.91 - Decode Ways
No.70 - Climbing Stairs
No.279 - Perfect Squares
No.62 - Unique Paths
The problem can be described as p[i, j]
No.5 - Longest Palindromic Substring
dp[i][j] means wether s[i: j + 1] is a palindromic substring
1 | for i in range(len(s)): |
1 | # This function will recursively check the left and right value of a previous palindromic string |
No.10 - Two string matching with special character
1 | if j + 1 == '*': |
No.44 - Wildcard Matching
No.322 - Change Coin
1 | for coin in coins: |
The problem can be described as a decision problem at step i
No.198 - House Robber
1 | dp[i] = max(dp[i-1], dp[i-2] + nums[i-1]) |
Summary for some exercise.
Summary for some exercise.
恭喜宝贝儿拿到微软的奥佛!!!!!
牛逼!!!!!
The structure of computer is simple. For hardware, computer includes Memory, Controller, Processor and I/O devices. For software, the basic element is operating system.
Central Processing Unit (CPU) is the brain of the computer. It receives data, execute commands and process the data.
CPU can also be divided into following parts according to their functions.
| Name | Functions |
|---|---|
| Register | Store the command code, data, and address data |
| Controller | Fetch the command, data from memory to register, and execute the I/O devices |
| Arithmetic Logic Unit (ALU) | Operate the data in register |
| Clock | Count signal |
All parts above are connected together via electricity’s signal.
The working pipeline of CPU is as follows.
Controller fetches the command from memory to registers, Program Counter stores the next address of command that needs to be executed.Controller decodes the command according to existed rules, recognizes operation category and the methods.Controller handles the action of the command. e.g. Add numbers stored in two registers, compare two numbers.Controller will fetch the data from the memory according to the address decoded from the command.There are several kinds of register in CPU.
It store the address of the unit in memory which stores the command that needs to be executed. It control the process of the program to be executed.
When the program is executed, the process is as follows.
JMP command in Assembly language), which points to the unit which stores the next command.Stack in memory, when the function is finished, the content of Program Counter will be set to the address in the top of Stack.Flag Register stores the sign(+/-/0) of the Accumulation Register.
There is a compare action when conditional/loop function appears. When the compare function is executed, subtraction action will happen, and the answer is store in the Flag Register, then the Program Counter will be updated to corresponding address.
A base register and several index registers can form an array. The array is stored continuously.
Memory has tight relationship with with CPU, CPU will fetch the command and data from disk to memory, and write back the command and data to disk.
There is several kinds of memory.
| Name | Functions |
|---|---|
| RAM | Can be read and wrote. Data will lose when power off |
| ROM | Can only be read. Data won’t lose when power off |
| Cache | It has high read and write speed. CPU will handle the cache firstly. If there is no required data, CPU will fetch data in RAM |
Memory has power, address signal, data signal, control signal.
The program stored in disk must be loaded to memory to be executed.
The disk has the following parts
| Name | Function |
|---|---|
| Disk Cache | It stores the repeated content that memory requires to read from disk. It could accelerate the read speed of memory |
| Virtual Memory | It can provide a virtual continuous memory for the program when the memory doesn’t enough space. The content in the real memory and the virtual memory will be swapped when required |
Physcially, the disk is divided into different sector, which is the unit to do the read and write actions to disk. A disk will include 512 byte normally.
Operate system could get over the difference of computer hardware except CPU.
The program just needs to call the API(Application Programming Interface) in operate system, then operate could handle the I/O devices.
Operate system includes:
[1] WeChat article
Notes of Operation System course (2021 Autumn, Tsinghua University)