千里之行,始于足下

1 Algorithm(算法题)

  1. 121. 买卖股票的最佳时机

再次聊聊这道题,看到网上有人整理了股票题目的通用解法。

class Solution {
    public int maxProfit(int[] prices) {
        int n = prices.length;
        // base case: dp[-1][0] = 0, dp[-1][1] = -infinity
        // 第 1 天不持有股票,第 1 天持有股票
        int dp_i_0 = 0, dp_i_1 = Integer.MIN_VALUE;
        for (int i = 0; i < n; i++) {
            // dp[i][0] = max(dp[i-1][0], dp[i-1][1] + prices[i])
            dp_i_0 = Math.max(dp_i_0, dp_i_1 + prices[i]);
            // dp[i][1] = max(dp[i-1][1], -prices[i])
            dp_i_1 = Math.max(dp_i_1, -prices[i]);
        }
        return dp_i_0;
    }
}
  1. 122. 买卖股票的最佳时机 II

代码:

class Solution {
    public int maxProfit(int[] prices) {
        // 动态规划状态转移方程
        // dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
        // dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
        int[][] dp = new int[prices.length][2];
        dp[0][0] = 0;
        dp[0][1] = -prices[0];
        for(int i = 1; i < prices.length; i++) {
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
        }
        return dp[prices.length - 1][0];
    }
}

2 Technique/Tips(分享一个小技术)

2.1 Redis 源码阅读 —— 千里之行,始于足下,如何搭建 Redis 7.x 源码阅读环境

2.1.1 源码下载

直接从 GitHub 源码仓库下载:

git clone https://github.com/redis/redis.git

我们以最新的 tag 上去:

git checkout tags/7.0.4 -b 7.0.4

2.1.2 编译

首先确保安装了 GCC,我本地已安装。
编译:

make CFLAGS="-g -O0" 

备注:-O0 参数旨在强制编译器不要进行编译优化,避免 debug 的时候源码与实际运行代码不匹配

2.1.3 启动

拥抱 jetbrains 全家桶,使用 CLion 2023.2.2 编译好的 Redis 源码。

启动配置如下:
![](

3 Share(分享我的所见所闻)

本来第 4 部分应该是“分享一个观点”,但我觉得许多时候,人在经常性地分享观点时,会趋于建议或者劝诫,类似“知识陷阱”一样。
这让我对“分享观点”这件事情有些迟疑,基于上述的原因,我将第 4 点改为了“分享我的所见所闻”,内容比较杂乱,包含一周内读过的书、看过的博客、学习的摄影知识等等。

3.1 技术博客