自定义scrollbar

自定义scrollbar, 包括宽度,颜色和圆角

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// custom scrollbar
::-webkit-scrollbar {
/* for vertical scrollbars */
width: 8px;
/* for horizontal scrollbars */
height: 8px;
border-radius: 8px;
}
::-webkit-scrollbar-track {
background: #f1f1f1;
border-radius: 8px;
}
::-webkit-scrollbar-thumb {
background: #c1c1c1;
border-radius: 8px;
&:hover {
background: #8c8c8c;
}
}

css

Flexible Grid View

参考Flutter的GridView,在Web环境下用JS实现一个自适应的GridView

基本思路是利用react-virtualized-auto-sizer获取当前容器大小,再根据crossAxisCount和childAspectRatio计算布局

ffmpeg note

ffmpeg note

  • To Convert video to 720p

    1
    ffmpeg -i input.mp4 -s 1280x720 -b:v 1024k -bufsize 1024k output.avi
  • To set the video bitrate of the output file to 64 kbit/s:

    1
    ffmpeg -i input.avi -b:v 64k -bufsize 64k output.avi
  • To force the frame rate of the output file to 24 fps:

    1
    ffmpeg -i input.avi -r 24 output.avi
  • To remove audio

    1
    2
    3
    # -c:v copy copies the video stream.
    # -an removes the audio stream.
    ffmpeg -i input.mp4 -c:v copy -an output.mp4
  • To cut mp3

1
ffmpeg -i input.mp3 -ss 00:00:00 -t 00:00:01 -acodec copy output.mp3

前端性能优化方案

前端性能优化方案

  • 减少DOM的访问次数,可以将DOM缓存到变量中;

  • 减少重绘和回流,任何会导致重绘和回流的操作都应减少执行,可将多次操作合并为一次;

  • 尽量采用事件委托的方式进行事件绑定,避免大量绑定导致内存占用过多;

  • css层级尽量扁平化,避免过多的层级嵌套,尽量使用特定的选择器来区分;

  • 动画尽量使用CSS3动画属性来实现,开启GPU硬件加速;

  • 图片在加载前提前指定宽高或者脱离文档流,可避免加载后的重新计算导致的页面回流;

  • css文件在标签中引入,js文件在标签中引入,优化关键渲染路径;

  • 加速或者减少HTTP请求,使用CDN加载静态资源,合理使用浏览器强缓存和协商缓存,小图片可以使用Base64来代替,合理使用浏览器的预取指令prefetch和预加载指令preload;

  • 压缩混淆代码,删除无用代码,代码拆分来减少文件体积;

  • 小图片使用雪碧图,图片选择合适的质量、尺寸和格式,避免流量浪费。使用picdiet压缩图片,使用ffmpeg压缩视频

birthday problem

birthday problem

生日问题是指在随机选择的一群人当中有两人的生日相同的概率。如果一个房间里有23个或23个以上的人,那么至少有两个人的生日相同的概率要大于50%;对于60或者更多的人,这种概率要大于99%。

该问题的计算公式就是 从365个元素中取出n个元素的排列 除以 365的n次方

通用计算方式如下,即计算拥有all个数量属性取值的个体count 个时,出现属性值相同的概率,这里考虑到值会比较大,所以使用BigInt

1
2
3
4
5
6
7
8
9
10
11
12
13
const calc = (count, all) => {
let c = count
let result = 1n
while (c > 0n) {
c -= 1n
result *= (all - c)
}
return `${Number(1000n - result * 1000n / (all ** count)) / 10}%`
}
calc(23n, 365n) // "50.8%"
calc(60n, 365n) // "99.5%"

Custom systemd service in Linux

custom systemd service

  • service file in /lib/systemd/system
1
2
3
4
5
6
7
8
9
10
11
12
[Unit]
Description=ss local server
After=network.target network-online.target systemd-networkd.service NetworkManager.service connman.service
[Service]
Type=simple
RemainAfterExit=yes
ExecStart=/usr/bin/ss-local
ExecStop=/bin/kill -s QUIT $MAINPID
[Install]
WantedBy=multi-user.target
  • operations
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    # make change take effect
    sudo systemctl daemon-reload
    # start service
    sudo service ss-local start
    # stop service
    sudo service ss-local stop
    # view log
    journalctl -xe

Shortest subarray with sum at least k

在LeetCode刷算法题,记录一下一道比较难的题目和至少为 K 的最短子数组

问题描述

返回 A 的最短的非空连续子数组的长度,该子数组的和至少为 K 。

如果没有和至少为 K 的非空子数组,返回 -1 。

其中:

  • 1 <= A.length <= 50000
  • -10 ^ 5 <= A[i] <= 10 ^ 5
  • 1 <= K <= 10 ^ 9

最先想到的是依照数值长度进行遍历,但时间复杂度为O(n^2)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* @param {number[]} A
* @param {number} K
* @return {number}
*/
const shortestSubarray = (A, K) => {
let preSum = 0
let count = 1
while (count <= A.length) {
preSum += A[count - 1]
let sum = preSum
if (sum >= K) return count
for (let i = 1; i + count <= A.length; i ++) {
sum = sum - A[i - 1] + A[i + count - 1]
if (sum >= K) return count
}
count += 1
}
return -1
}

之后参考了网上的资料,可以使用Sliding Window的方法,复杂度可以下降到O(N)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* @param {number[]} A
* @param {number} K
* @return {number}
*/
const shortestSubarray = (A, K) => {
if (!A.length) return -1
let S = [0]
for (let i = 0; i < A.length; i++) {
S[i + 1] = S[i] + A[i]
}
const queue = []
let ans = A.length + 1
for (let j = 0; j < A.length + 1; j++) {
while (queue.length && S[j] <= S[queue[queue.length - 1]]) {
queue.pop()
}
while(queue.length && S[j] >= S[queue[0]] + K) {
ans = Math.min(ans, j - queue.shift())
}
queue.push(j)
}
return ans < A.length + 1 ? ans : -1
}

Some bash scripts

setInterval in bash

1
while true; do ls -l; sleep 2; done

count files fastly

1
find . -type f | wc -l

find files that large than 1 GiB

1
find . -type f -size +1G

test if file exist

1
2
3
4
5
6
myFile="/home/lxw/abc"
if [ ! -f "$myFile" ]; then
touch "$myFile"
else
echo "skip"
fi

kill process

find all dart process and kill them all

1
2
3
4
# find
ps -aux | grep dart
# find && kill
pgrep -f dart | xargs kill

Wifi

1
2
3
4
5
# connect to wifi
sudo nmcli d wifi connect iPhone password 11345678
# show saved connections
ls -sail /etc/NetworkManager/system-connections/