第 66 场双周赛

172 阅读1分钟

第 66 场双周赛

  1. 使用Go语言
  2. 排名

image.png

No.1 5922. 统计出现过一次的公共字符串

5922.png

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. 从房屋收集雨水需要的最少水桶数

5923.png

思路:

  1. 排除无解的情况
    1. 以HH开头或者结尾
    2. 三个HHH相连
  2. 有解的情况
    1. 统计一个位置能接两个屋雨水子位置,(统计之后,排除屋子,免得下次再统计)
    2. 统计一个位置接一个屋子雨水的位置
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. 网格图中机器人回家的最小代价

5924.png

提示: 机器人家在当前机器人的方位:

  1. 左下方
  2. 右上方
  3. 左上方
  4. 右下方
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. 统计农场中肥沃金字塔的数目

5925.png