Hi there 👋

A programmer who is pursuing a better life.

Dynamic Programming

Definition Dynamic Programming (DP) is defined as a technique that solves some particular type of problems in Polynomial Time. Dynamic Programming solutions are faster than the exponential brute method and can be easily proved their correctness. Dynamic programming works on following principles: Characterize structure of optimal solution, i.e. build a mathematical model of the solution. Recursively define the value of the optimal solution. Using bottom-up approach, compute the value of the optimal solution for each possible sub-problems....

March 4, 2024 · 5 min · oneday

Read-Black Tree

Read-Black Tree A Red-Black tree (Abbreviated as “R-B Tree”) is a balanced binary search tree. It is to solve the complexity of ordinary binary search trees in the process of data update, caused by degradation problems. The height of the red-black tree is approximately log n, so it is approximately balanced. The time complexity of the insertion, deletion, and search operations is O(logn). Defination The root node is black Each leaf node is a black empty node (NIL) which is the leaf node does not store data No adjacent nodes can be red at the same time which is red nodes are separated by black nodes Every node, and all paths from it to its reachable leaf nodes, contain the same number of black nodes Insertion Inserted nodes must be red....

February 5, 2024 · 8 min · oneday

Docker proxy

Set up proxy for dockerd The “docker pull” command is executed by the dockerd daemon. The dockerd daemon is managed by systemd. Therefore, if you need to use the HTTP/HTTPS proxy when executing the “docker pull” command, you need to configure it through systemd. 1.Create configuration folder for dockerd. sudo mkdir -p /etc/systemd/system/docker.service.d 2.Create http-proxy.conf file under the folder, add follow content sudo vim /etc/systemd/system/docker.service.d/http-proxy.conf [Service] Environment="HTTP_PROXY=http://proxy.example.com:8080/" Environment="HTTPS_PROXY=http://proxy.example.com:8080/" Environment="NO_PROXY=localhost,127.0.0.1,.example.com" 3.Restart docker sudo systemctl daemon-reload sudo systemctl restart docker Set up proxy for docker container During the container running phase, if you need to use HTTP/HTTPS proxy, you can change the docker client configuration or specify environment variables....

February 1, 2024 · 2 min · oneday

Golang:GPM调度器

Go语言的GPM调度器是什么? Go语言天然支持高并发,原因是内部有协程(goroutine)加持,可以在一个进程中启动成千上万个协程 并发模型 常见的并发模型有七种: 线程与锁 函数式编程 Clojure之道 actor 通讯顺序进程(CSP) 数据级并行 Lambda架构 出自《七周七并发模型》 CSP CSP,全称Communicating Sequential Processes,意为通讯顺序进程,它是七大并发模型中的一种, 它的核心观念是将两个并发执行的实体通过通道channel连接起来,所有的消息都通过channel传输 GPM调度模型 GPM代表了三个角色,分别是Goroutine、Processor、Machine Goroutine:由go关键字创建的执行体,它对应一个结构体g,结构体里保存了goroutine的堆栈信息 Machine:表示操作系统的线程 Processor:表示处理器,有了它才能建立G、M的联系 Goroutine Goroutine使用go关键词创建的执行单元(协程),协程是不为操作系统所知的,它由编程语言层面实现, 上下文切换不需要经过内核态,再加上协程占用的内存空间极小,所以有着非常大的发展潜力 go func() {}() 复制代码在Go语言中,Goroutine由一个名为runtime.go的结构体表示,该结构体非常复杂,有40多个成员变量, 主要存储执行栈、状态、当前占用的线程、调度相关的数据 type g struct { stack struct { lo uintptr hi uintptr } // 栈内存:[stack.lo, stack.hi) stackguard0 uintptr stackguard1 uintptr _panic *_panic _defer *_defer m *m // 当前的 m sched gobuf stktopsp uintptr // 期望 sp 位于栈顶,用于回溯检查 param unsafe.Pointer // wakeup 唤醒时候传递的参数 atomicstatus uint32 goid int64 preempt bool // 抢占信号,stackguard0 = stackpreempt 的副本 timer *timer // 为 time....

January 11, 2024 · 1 min · oneday

Golang:程序预期输出

代码1: func calc(index string, a, b int) int { ret := a + b fmt.Println(index, a, b, ret) return ret } func main() { a := 1 b := 2 defer calc("1", a, calc("10", a, b)) a = 0 defer calc("2", a, calc("20", a, b)) b = 1 } 输出: 10 1 2 3 20 0 2 2 2 0 2 2 1 1 3 4 解释: defer 在定义的时候会计算好调用函数的参数,所以先顺序执行两个defer里面的calc,然后再倒序执行defer 代码2: func main() { runtime....

January 11, 2024 · 1 min · oneday