第 66 场双周赛
- 使用Go语言
- 排名
No.1 5922. 统计出现过一次的公共字符串
func countWords(words1 []string, words2 []string) int {
counts1 := make(map[string]int)
counts2 := make(map[string]int)
for i := 0; i < len(words1); i++ {
counts1[words1[i]]++
}
for i := 0; i < len(words2); i++ {
counts2[words2[i]]++
}
sum := 0
for k, v := range counts1 {
if v > 1 {
continue
}
if counts2[k] == 1 {
sum++
}
}
return sum
}
No.2 5923. 从房屋收集雨水需要的最少水桶数
思路:
- 排除无解的情况
- 以HH开头或者结尾
- 三个HHH相连
- 有解的情况
- 统计一个位置能接两个屋雨水子位置,(统计之后,排除屋子,免得下次再统计)
- 统计一个位置接一个屋子雨水的位置
func minimumBuckets(street string) int {
if strings.Contains(street, "HHH")||!strings.Contains(street,"."){
return -1
}
if strings.HasPrefix(street, "HH")||strings.HasSuffix(street,"HH"){
return -1
}
var data = []byte(street)
sum := 0
for i := 1; i < len(data)-1; i++ {
if data[i] == '.' {
if data[i-1] == 'H' && data[i+1] == 'H' {
sum++
data[i-1] = '.'
data[i+1] = '.'
}
}
}
for i := 0; i < len(data); i++ {
if data[i]=='H'{
sum++
}
}
return sum
}
No.3 5924. 网格图中机器人回家的最小代价
提示: 机器人家在当前机器人的方位:
- 左下方
- 右上方
- 左上方
- 右下方
class Solution {
public int minCost(int[] startPos, int[] homePos, int[] rowCosts, int[] colCosts) {
int cost = 0;
if (startPos[0] < homePos[0]) {
for (int i = startPos[0]; i < homePos[0]; i++) {
cost += rowCosts[i + 1];
}
} else {
for (int i = homePos[0]; i < startPos[0]; i++) {
cost += rowCosts[i];
}
}
if (startPos[1] < homePos[1]) {
for (int i = startPos[1]; i < homePos[1]; i++) {
cost += colCosts[i + 1];
}
} else {
for (int i = homePos[1]; i < startPos[1]; i++) {
cost += colCosts[i];
}
}
return cost;
}
}
No.4 5925. 统计农场中肥沃金字塔的数目