golang力扣leetcode 第 285 场周赛

简介: golang力扣leetcode 第 285 场周赛

前言

一共四题,AC三题,第四题线段树不搞acm了根本写不出来- -,懒得复习一遍了,就写三题的题解吧

第一题

2210.统计数组中峰和谷的数量

2210.统计数组中峰和谷的数量

题解

简单题,直接模拟就行了,不过题目中说的是左边第一个不相等和右边第一个不相等,相等的要跳过再去找,所有这里用两个for找出l和r

代码

func countHillValley(nums []int) int {
  mp := make(map[int]int)
  ans := 0
  for i := 1; i <= len(nums)-1-1; i++ {
    l, r := i-1, i+1
    for l >= 0 && nums[l] == nums[i] {
      l--
    }
    for r <= len(nums)-1 && nums[r] == nums[i] {
      r++
    }
    if l >= 0 && r <= len(nums)-1 {
      if nums[i] > nums[l] && nums[i] > nums[r] {
        mp[i] = 1
        if mp[i-1] == 1 {
          continue
        }
        ans++
      } else if nums[i] < nums[l] && nums[i] < nums[r] {
        mp[i] = -1
        if mp[i-1] == -1 {
          continue
        }
        ans++
      }
    }
  }
  return ans
}

第二题

2211.统计道路上的碰撞次数

2211.统计道路上的碰撞次数

题解

两个方向相反的车相撞加2分,s车被撞加1分,其实就是每个撞车都会贡献1分,不分左右,因为RL=2,R=1,L=1,所以除了开头的LLL和结尾的RRR会跑出去,中间的都会撞,那么就统计L和R有多少个就行了

代码

func countCollisions(directions string) int {
  l, r := 0, len(directions)-1
  ans := 0
  for l <= len(directions)-1 && directions[l] == 'L' {
    l++
  }
  for r >= 0 && directions[r] == 'R' {
    r--
  }
  for i := l; i <= r; i++ {
    if directions[i] == 'L' || directions[i] == 'R' {
      ans++
    }
  }
  return ans
}

第三题

2212.射箭比赛中的最大得分

2212.射箭比赛中的最大得分

题解

两种思路

  1. dfs+回溯,贪心,想要得分高,就从分高的区域开始计算,对于每个区域,有不射和比A多射一支的选择,那么就dfs即可,注意如果到了0区域,还有多的剑,就都给0就好了
  2. 二进制枚举,一共12个区域,2^12次,全部枚举一遍即可

代码

  1. dfs+回溯
func maximumBobPoints(numArrows int, aliceArrows []int) (ans []int) {
  score, cnt := 0, 0
  ans, cntSlice := make([]int, len(aliceArrows)), make([]int, len(aliceArrows))
  var dfs func(numArrows int, idx int)
  dfs = func(numArrows int, idx int) {
    if numArrows == 0 && idx == -1 {
      if score < cnt {
        score = cnt
        copy(ans, cntSlice)
      }
    }
    if idx <= -1 || numArrows < 0 {
      return
    }
    //不选idx
    cntSlice[idx] = 0
    dfs(numArrows, idx-1)
    //选idx
    cnt += idx
    if idx == 0 {
      cntSlice[idx] = numArrows
      dfs(0, idx-1)
    } else {
      cntSlice[idx] = aliceArrows[idx] + 1
      dfs(numArrows-aliceArrows[idx]-1, idx-1)
    }
    cnt -= idx        //回溯
    cntSlice[idx] = 0 //回溯
  }
  dfs(numArrows, 11)
  return
}
  1. 二进制枚举
func maximumBobPoints(numArrows int, aliceArrows []int) (ans []int) {
  n := 1<<len(aliceArrows) - 1
  score := 0
  for i := 0; i <= n; i++ {
    cnt := 0
    out := 0
    cntSlice := make([]int, len(aliceArrows))
    for j, v := range aliceArrows {
      if i>>j&1 == 1 { //其中一个区域如果选中了
        cnt += j            //统计分数
        out += v + 1        //统计射出去的剑
        cntSlice[j] = v + 1 //比A多射一个剑
      }
    }
    if out > numArrows {
      continue
    }
    cntSlice[0] += numArrows - out //把剩下的减都给0区域
    if score < cnt {
      score = cnt
      ans = cntSlice
    }
  }
  return
}



目录
相关文章
|
2天前
|
Java
【LeetCode力扣】面试题 17.14. 最小K个数(top-k问题)
【LeetCode力扣】面试题 17.14. 最小K个数(top-k问题)
11 1
|
2天前
|
存储 算法
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
6 0
|
2天前
|
存储
Leetcode第382场周赛
```markdown 给定字符串`s`,计算按键变更次数,即使用不同键的次数,不考虑大小写差异。例如,`&quot;aAbBcC&quot;`变更了2次。函数`countKeyChanges`实现此功能。另外,求满足特定模式子集最大元素数,`maximumLength`函数使用`TreeMap`统计次数,枚举并构建子集,返回最大长度。最后,Alice和Bob玩鲜花游戏,Alice要赢需满足鲜花总数奇数、顺时针在[1,n]、逆时针在[1,m],返回满足条件的(x, y)对数,可通过奇偶性分类讨论求解。 ```
13 1
|
2天前
|
存储
Leetcode第383场周赛
在LeetCode第383场周赛中,选手完成了3道题目。第一题是关于边界上的蚂蚁,蚂蚁根据非零整数数组nums的值移动,返回蚂蚁返回边界上的次数。解题方法是计算数组累加和为0的次数。第二题涉及计算网格的区域平均强度,给定一个灰度图像和阈值,返回每个像素所属区域的平均强度。解题关键在于理解相邻像素和区域定义,并计算平均强度。第三题是恢复单词初始状态的最短时间问题,通过移除前k个字符并添加k个字符,求恢复原词所需的最短时间。解题策略是检查去除前k个字符后的子串是否能作为原词的前缀。
12 1
Leetcode第383场周赛
|
2天前
|
Go 容器 SQL
【Golang Leetcode】总目录(Day1~100)
【Golang Leetcode】总目录(Day1~100)
477 1
【Golang Leetcode】总目录(Day1~100)
|
2天前
|
Go
golang力扣leetcode 494.目标和
golang力扣leetcode 494.目标和
32 0
|
2天前
|
Go
golang力扣leetcode 剑指Offer II 114. 外星文字典
golang力扣leetcode 剑指Offer II 114. 外星文字典
21 0
|
2天前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
9 0
|
2天前
|
算法 索引
【刷题】滑动窗口精通 — Leetcode 30. 串联所有单词的子串 | Leetcode 76. 最小覆盖子串
经过这两道题目的书写,相信大家一定深刻认识到了滑动窗口的使用方法!!! 下面请大家继续刷题吧!!!
12 0