Open vscode with command code

  1. sudo chown -R yourUsername /usr/local/bin
  2. Open Vscode and press cmd + shift + p
  3. Search for install code and press Enter

Go build binary failed - pull from private failed

在 go 專案下要 build bin 檔的時候出現 error :

34.21 go: xxxx.com/qson/libraries/qlib@v0.2.73: unrecognized import path "xxxx.com/yy/libraries/qlib": https fetch: Get "https://xxxx.com/yy/libraries/lib?go-get=1": dial tcp 122.17.60.101:443: connect: no route to host
------
Dockerfile:34
--------------------
  33 |     COPY go.* .
  34 | >>> RUN --mount=type=secret,id=netrc,dst=/root/.netrc \
  35 | >>>     --mount=type=cache,target=/go/pkg/mod \
  36 | >>>     --mount=type=cache,target=/root/.cache/go-build \
  37 | >>>     go install github.com/swaggo/swag/cmd/swag@latest && \
  38 | >>>     go install github.com/t-yuki/gocover-cobertura@latest && \
  39 | >>>     go install gotest.tools/gotestsum@latest && \
  40 | >>>     go install google.golang.org/protobuf/cmd/protoc-gen-go@latest && \
  41 | >>>     go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest && \
  42 | >>>     go mod download
  43 |     
--------------------
ERROR: failed to solve: process "/bin/sh -c go install github.com/swaggo/swag/cmd/swag@latest &&     go install github.com/t-yuki/gocover-cobertura@latest &&     go install gotest.tools/gotestsum@latest &&     go install google.golang.org/protobuf/cmd/protoc-gen-go@latest &&     go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest &&     go mod download" did not complete successfully: exit code: 1

這是由於 go.mod 中有一個私有模組,但 Docker 容器在 go mod download 的時候連不到內部伺服器,所以整個步驟失敗。

Solution

  1. 在可以連到內部伺服器的主機上先: go mod vendor,然後 Dockerfile 改成:
     COPY go.* .
     COPY vendor/ vendor/
     RUN go mod vendor && \
         go install ...
    

    這樣 build 時不需要再上網抓私有套件。

  2. 把 go mod download 刪掉

什麼是 go mod vendor

  • 平常 Go build 會去網路下載依賴(go mod download),這時如果有私有模組、外網斷線,就會失敗。用 go mod vendor 把依賴先放進 vendor/ 後,Go build 可以直接從 vendor/ 讀取,不必再連網抓套件,適合離線建置或有內部私有依賴的情況。
  • Go 1.14+ 開始,go build、go install 會自動偵測 vendor/ 並使用它(除非用 -mod=readonly 或 -mod=mod 強制不用)。
  • 缺點:vendor 資料夾可能很大、若有新版本需要重新 go mod vendor

Group descriptors corrupted

systemctl start xx.service 啟動某個 daemon 的時候,出現下面錯誤:

EXT4-fs (sde3): ext4_check_descriptors: Checksum for group 0 failed (9679!=59862)
EXT4-fs (sde3): group descriptors corrupted!

這表示 /dev/sde3 分區的 ext4 superblock 或 group descriptor 出現校驗錯誤(corruption)。

修復方式

執行

umount /dev/sde3  # 如果已掛載
e2fsck -f /dev/sde3

這會掃描並修復損壞的 metadata(會詢問是否修復,建議按 y) 如果你看到類似 “Inode bitmap checksums invalid” 等錯誤,也會自動修!

Leetcode 1498. Number of Subsequences That Satisfy the Given Sum Condition

題目描述

You are given an array of integers nums and an integer target.

Return the number of non-empty subsequences of nums such that the sum of the minimum and maximum element on it is less or equal to target. Since the answer may be too large, return it modulo 10^9 + 7.

範例

Example 1:

Input: nums = [3,5,6,7], target = 9
Output: 4
Explanation: There are 4 subsequences that satisfy the condition.
[3] -> Min value + max value <= target (3 + 3 <= 9)
[3,5] -> (3 + 5 <= 9)
[3,5,6] -> (3 + 6 <= 9)
[3,6] -> (3 + 6 <= 9)

Example 2:

Input: nums = [3,3,6,8], target = 10
Output: 6
Explanation: There are 6 subsequences that satisfy the condition. (nums can have repeated numbers).
[3] , [3] , [3,3], [3,6] , [3,6] , [3,3,6]

### Example 3:

Input: nums = [2,3,3,4,6,7], target = 12
Output: 61
Explanation: There are 63 non-empty subsequences, two of them do not satisfy the condition ([6,7], [7]).
Number of valid subsequences (63 - 2 = 61).

Constraints:

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^6
  • 1 <= target <= 10^6

解法

two poiner

  1. sort (由小到大)
  2. two pointer 一個從左一個從右邊找到 arr[i] + arr[j] <= k
  3. 如果 arr[i] + arr[j] <= k 代表最小值還能增加,移動左指針,反之移動右指針,讓 arr[i] + arr[j] 變小
  4. 計算這個區間中的 subsequence 有多少,加到答案裡面
func numSubseq(nums []int, target int) int {
    const MOD = 1_000_000_007
    sort.Ints(nums)

    n := len(nums)
    pow2 := make([]int, n+1)
    pow2[0] = 1
    for i:=1; i<=n; i++ {
        pow2[i] = pow2[i-1]*2 % MOD
    }

    res := 0
    l, r := 0, n-1
    for l <= r {
        if nums[l] + nums[r] <= target {
            res += pow2[r-l]
            res %= MOD
            l++
        } else {
            r--
        }
    }

    return res
}

計算子集合

從 nums[l+1] 到 nums[r] 這段長度為 r - l 中挑選任意個元素(可以不挑),總共會有 2^(r - l) 個子集合(不包含 nums[l])

為啥不包含 nums[l] ?

這些子集合的前提是我們已經固定選了 nums[l](因為我們要它當「最小值」)。固定選 nums[l](最小值),然後從 nums[l+1..r] 這段中挑選 0~(r-l) 個元素,這 2^(r - l) 個子序列都是合法的。

為啥不能包含 nums[l] ?

  1. 如果你把 nums[l] 也當成「可選或不選」的一部分,那就會錯,因為會重複計算那些不包含 nums[l] 的子序列
  2. 那些子序列可能會有最小值更大的元素,這違反我們當前判斷是以 nums[l] 為「最小值」的條件

Error loading key "/root/.ssh/repo_key" error in libcrypto

遇到錯誤 Error loading key “/root/.ssh/repo_key”: error in libcrypto ,卡了非常久,試過 chatgpt 建議的各種方法。 最後發現是 private key 檔案後必須要有 newline…