LeetCode-18

1668. 最大重复子字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution {
public:
int maxRepeating(string sequence, string word) {
int len1 = sequence.size();
int len2 = word.size();
int maxk = 0, k = 0;
for(int i = 0; i < len1;) {
bool flag = true;
int next = i+1;
bool flag1 = false;
for(int j = 0; j < len2; j++) {
if(sequence[i+j] != word[j]) {
flag = false;
break;
}
if(!flag1 && j != 0 && sequence[i+j] == word[0]) {
next = i+j;
flag1=true;
}
}
// cout << i << " " << k << " " << maxk << endl;
if(flag) {
k++;
i += len2;
} else {
maxk = max(k, maxk);
if(k == 0) {
i+=1;
} else {
i = i-len2+1;
}
k = 0;
}
// cout << i << endl;
}
return max(maxk, k);
}
};

笨方法,从右向左找,适当回溯

754. 到达终点数字

解法1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
int reachNumber(int target) {
target = abs(target);
int n = (sqrt(8.0*target+1)-1)/2; //8.0,防止int溢出
int sum = (n+1)*n/2;
if(sum == target) {
return n;
}
int diff = target-sum;
if((n % 2 == 1 && diff % 2 == 0) || (n % 2 == 0 && diff % 2 == 1)) {
n += 1;
} else if(diff %2 == 1) {
n += 2;
} else {
n += 3;
}
return n;
}
};
阅读更多

LeetCode-17

1662. 检查两个字符串数组是否相等

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
bool arrayStringsAreEqual(vector<string>& word1, vector<string>& word2) {
return join(move(word1)) == join(move(word2));
}
string join(vector<string>&& word) {
string s;
int len = word.size();
if(len <= 0) return s;
for(int i = 0; i < len-1; i++) {
s += word[i];
}
s+=word[len-1];
return s;
}
};

实现一个join函数就好了

481. 神奇字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
int magicalString(int n) {
int bit = 3;
int count = 1;
bool q[100005] = {false};
int queue_front = 0;
int queue_rear = 0;
bool cur=1;
bool gen=0;
while(bit < n) {
bit += cur+1;
q[queue_front++] = gen;
if(cur) {
q[queue_front++] = gen;
}
gen=1-gen;
count+=gen?cur+gen:0;
cur = q[queue_rear++];
}
return count -(bit>n && gen);
}
};

关键在于想清楚如何生成这个神奇字符串,题目中说,s的前几个字符是12211
1生成1,s=1
2生成22,因为前一个1生成了1,这个2不能也生成1,s=122
2生成11,因为前一个2生成了2,这个2不能也生成2,s=12211
1生成2,前一个2生成了1,这个1就只能生成2了,s=122112
1生成1,s=1221121
2生成22,s=122112122

阅读更多

6.824-lab1-Mapreduce

Intro

实验目的

  • 实现一个MapReduce调度器(Coordinator)

准备工作

  1. 下载源码
1
git clone git://g.csail.mit.edu/6.824-golabs-2021 6.824
阅读更多

显卡驱动踩坑

nvidia显卡驱动踩坑

  • 之前更新了一次linux的内核,导致内核的显卡冲突了,hdmi没有输出,nvidia-smi命令也没了
  • 重装了nvidia的显卡,发现默认使用核显,动画非常卡
  • 后来屏蔽了linux自带的驱动nouveau,hdmi终于有输出了,显卡驱动的daemon也可以使用了(nvidia-smi输出正常),但是自带的显示器却不能用了

解决方法

  • /etc/X11/xorg.conf文件不知道什么原因出现了错误,把这个文件内容清空,就好了
  • 重启后发现这个文件里面本身就没东西

shell的使用

shell fuction

函数定义

  • 定义1
xxx.sh
1
2
3
function funcName() {
# do sth
}
  • 定义2
xxx.sh
1
2
3
4
f2() {
value=$(($1+$2+1))
echo $1 "+" $2 "=" $value
}
阅读更多

LeetCode-16

934. 最短的桥

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Solution {
public:

int indexMap[105][105] = {0}; //岛屿点,对应一个岛
int n;
int edgex[105*105] = {0};
int edgey[105*105] = {0};
int edgei[105*105] = {0};
int edgej[105*105] = {0};
int edgecount = 0;
int edgeicount = 0;
int shortestBridge(vector<vector<int>>& grid) {
n = grid.size();
int islandCount = 0;
int p1x,p1y;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(grid[i][j] == 1 && indexMap[i][j] == 0) {
++islandCount;
dfs(grid, i, j, islandCount);
}
}
}
int min=INT_MAX;
for(int i = 0; i < edgecount; i++) {
for(int j = 0; j < edgeicount; j++) {
int path = abs(edgex[i]-edgei[j]) + abs(edgey[i] - edgej[j]) - 1;
if(min >= path) {
min = path;
}
}
}
return min;
}
void dfs(vector<vector<int>>& grid, int x, int y, int index) {
if(x < 0 || y < 0 || x >= n || y >= n) return;
if(indexMap[x][y] != 0 || grid[x][y] != 1) return;
indexMap[x][y] = index;
bool flag = (y-1 >= 0 && grid[x][y-1] == 0) || (y+1 < n && grid[x][y+1] == 0) || (x+1 < n && grid[x+1][y] == 0) || (x-1 >= 0 && grid[x-1][y] == 0);
dfs(grid, x, y-1, index);
dfs(grid, x, y+1, index);
dfs(grid, x+1, y, index);
dfs(grid, x-1, y, index);
if(flag) {
if(indexMap[x][y]==1) {
edgex[edgecount]=x;
edgey[edgecount]=y;
edgecount++;
} else if(indexMap[x][y]==2) {
edgei[edgeicount]=x;
edgej[edgeicount]=y;
edgeicount++;
}
}
}
};

和之前写的一道题有点像,827. 最大人工岛
827. 最大人工岛我先dfs找到所有连通子图和包围岛的0点,然后找这些点中有无同时包围多个岛的,把他们的面积加起来取最大值

这道题也可以使用相同的方法,找到每个岛屿的边界点,然后计算边界点的距离(只有两个岛,两个岛之间肯定是可以连通的,且不管使用那条途径,最短距离一定是 $ abs(x_1 - x_2) + abs(y_1-y_2)-1 $)

看答案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Solution {
public:
void dfs(int x, int y, vector<vector<int>>& grid, queue<pair<int, int>> &qu) {
if (x < 0 || y < 0 || x >= grid.size() || y >= grid[0].size() || grid[x][y] != 1) {
return;
}
qu.emplace(x, y);
grid[x][y] = -1;
dfs(x - 1, y, grid, qu);
dfs(x + 1, y, grid, qu);
dfs(x, y - 1, grid, qu);
dfs(x, y + 1, grid, qu);
}

int shortestBridge(vector<vector<int>>& grid) {
int n = grid.size();
vector<vector<int>> dirs = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};

for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
queue<pair<int, int>> qu;
dfs(i, j, grid, qu);
int step = 0;
while (!qu.empty()) {
int sz = qu.size();
for (int i = 0; i < sz; i++) {
auto [x, y] = qu.front();
qu.pop();
for (int k = 0; k < 4; k++) {
int nx = x + dirs[k][0];
int ny = y + dirs[k][1];
if (nx >= 0 && ny >= 0 && nx < n && ny < n) {
if (grid[nx][ny] == 0) {
qu.emplace(nx, ny);
grid[nx][ny] = -1;
} else if (grid[nx][ny] == 1) {
return step;
}
}
}
}
step++;
}
}
}
}
return 0;
}
};
阅读更多

projects

fedml:

  • deployment and modification to combine with Hyperledger fabric
  • repo

fabric:

  • deployment and combination with fedml
  • repo

Yaml-Requests:

  • read your yaml config file to generate python function
  • repo
阅读更多

fedml-4.与fabric通信

  • 直接采用“偷梁换柱”的模式,把修改后的代码复制到pip安装的位置

pip install的位置

通过python -m site命令查找包的安装路径

1
2
3
4
5
6
7
8
9
10
11
12
sys.path = [
'/usr/share/python3',
'/usr/lib/python310.zip',
'/usr/lib/python3.10',
'/usr/lib/python3.10/lib-dynload',
'/home/tt/.local/lib/python3.10/site-packages',
'/usr/local/lib/python3.10/dist-packages',
'/usr/lib/python3/dist-packages',
]
USER_BASE: '/home/tt/.local' (exists)
USER_SITE: '/home/tt/.local/lib/python3.10/site-packages' (exists)
ENABLE_USER_SITE: True

思考:需要修改哪些代码?

  • 修改Aggregator。Aggregator的作用是(1)保存各个节点上传的本地模型;(2)对本地模型进行aggregate操作。
    • 分离aggregator的功能,分为本地Aggregator和链上Aggregator,本地Aggregator不保存模型,将收到的模型转发给区块链,聚集操作时先向区块链取模型,再进行聚集操作
  • 修改FedMLServerManager
    • 在适当位置调用http接口,适当根据逻辑需要修改其它代码
  • 修改ClientMasterManager
    • 在适当位置调用http接口,适当根据逻辑需要修改其它代码
  • 修改message_define,文件中定义了C/S之间相互通信的名称,参数名称
    • 对其适当增删,达到C/S间协同的目的
阅读更多

fedml-3.Runner源码阅读

wandb 的使用

配置config.yaml

  • enable - true
  • wandb key
  • priject name
1
2
3
4
5
6
tracking_args:
log_file_dir: ./log
enable_wandb: true #enable
wandb_key: e3be1b9a8ab45f14a6ff454009bc7ca07b8792ba #key
wandb_project: fedml_mnist_test #project name
wandb_name: fedml_torch_fedavg_mnist_lr

运行

阅读更多