# Algorithm

This week’s LeetCode problem is 5. Longest Palindromic Substring

Given a string `s`, return the longest palindromic substring in `s`. For example, Input: s = "babad", Output: "bab"

This problem is solved using dynamic programming, the core of which lies in finding the state transfer equation. By observing that the echo string is still an echo string after removing the first and last two characters, i.e. `dp[i][j] = dp[i+1][j-1] ^ (s[i] == s[j])`, plus the initial condition when there are only one or two characters, so the state transfer equation is.

`dp[i][i] = true,dp[i][i+1] = (s[i] == s[i+1]),dp[i][j] = dp[i+1][j-1] ^ (s[i] == s[j]), j-i>1`

According to the state transfer equation, it is easy to write the code, mainly to record the start index and length of the longest echo substring in order to get that echo substring easily.

`class Solution {    public String longestPalindrome(String s) {        int length = s.length();        if (length < 2) {            return s;        }        int maxLength = 1;        int begin = 0;        boolean[][] dp = new boolean[length][length];        for (int i = 0; i < length; i++) {            dp[i][i] = true;        }        for (int right = 1; right < length; right++) {            for (int left= 0; left < right; left++) {                                if (s.charAt(left) != s.charAt(right)) {                    dp[left][right] = false;                } else {                    if (right - left < 3) {                        dp[left][right] = true;                    } else {                        dp[left][right] = dp[left+1][right-1];                    }                }                if (dp[left][right] == true && (right - left + 1 > maxLength)) {                    maxLength = right - left + 1;                    begin = left;                }            }        }        return s.substring(begin, begin+maxLength);    }}`

# Review

This week’s Review is for the following article: Don’t Panic! Your ram is fine!

Because the disk cache exists in the Linux system, its purpose is to make the system faster. When you need more memory, the disk cache will be automatically released to meet the needs of the application or process. Although you cannot turn it off, but you want to forcefully clear the current cache, you can run `echo 3 | sudo tee /proc/sys/vm/drop_caches`.

But when we use the `top` or `free` command to view the memory, we often find that the memory has been used, but this is actually because the `free` and `available` in the Linux system are different from our usual concepts Different, their comparison table is as follows:

Memory that is You’d call it Linux calls it used by applications Used Used used, but can be made available Free (or Available) Used (and Available) not used for anything Free Free

To see how much ram your applications could use without swapping, run free -m and look at the “available” column:

`\$ free -m        total    used    free    shared    buff/cache    availableMem:     1504    1491      13        0            855          792Swap:    2047       6    2041`

A healthy Linux system with more than enough memory will, after running for a while, show the following expected and harmless behavior:

• `free` memory is close to 0
• `used` memory is close to total
• `available` memory (or "free + buffers/cache") has enough room (let's say, 20%+ of total)
• `swap used` does not change

Warning signs of a genuine low memory situation that you may want to look into:

• `available` memory (or "free + buffers/cache") is close to zero
• `swap used` increases or fluctuates
• `dmesg | grep oom-killer` shows the OutOfMemory-killer at work

# Tip

A two-dimensional array in Python calculates the average value of each column by column. You need to use the numpy package and call the `numpy.average(data, axis=0)` statement to execute. When `axis=1`, calculate the average value by row , The example is as follows:

`import numpydata = [[1, 2], [3, 4]]avg = numpy.average(data, axis=0)print(avg) # [2, 3]`

# Share

Happy New Year and good luck in everything!

--

-- ## KeepNewbie_yan

A programmer. Share knowledge of programming, operating system, Linux kernel, and reading, thinking etc. Let us maintain a beginner mend and grow together!