golang力扣leetcode 第 293 场周赛

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

前言

4题A3题,,T4很明显用线段树,已经不会写线段树了,文末两段代码吧

第一题

2273.移除字母异位词后的结果数组

2273.移除字母异位词后的结果数组

题解

题目:给一个字符串数组,相邻字符串不能是字母异位词(字符串A用自身的字母重新排列,变成字符串B),如果是,则删除后者字符串

思路:

1.直接计算26长的字母数组,就很容易判断是不是异位词
2.用栈存,有可能第一个第二个第三个都是,那么用栈很方便在删除第二个又删除第三个

代码

func removeAnagrams(words []string) []string {
  ans := []string{words[0]}
  for _, word := range words[1:] {
    cnt := [26]int{}
    for _, b := range word {
      cnt[b-'a']++
    }
    for _, b := range ans[len(ans)-1] {
      cnt[b-'a']--
    }
    if cnt != [26]int{} { // 不是字母异位词
      ans = append(ans, word)
    }
  }
  return ans
}

第二题

2274.不含特殊楼层的最大连续楼层数

2274.不含特殊楼层的最大连续楼层数

题解

别人写的代码真优雅,淦

题目:给定楼层(bottom和top),给定一个数组,数组中的元素代表这个楼层不能用,问最长的连续可用的楼层是多长

思路:排序数组后,一次遍历计算就行

代码

func maxConsecutive(bottom int, top int, special []int) int {
  special = append(special, top+1)
  sort.Ints(special)
  left := bottom
  result := 0
  for _, v := range special {
    if left != v {
      result = max(result, v-left)
    }
    left = v + 1
  }
  return result
}
func max(i, j int) int {
  if i > j {
    return i
  }
  return j
}
func maxConsecutive(bottom, top int, a []int) (ans int) {
  a = append(a, bottom-1, top+1)
  sort.Ints(a)
  for i := 1; i < len(a); i++ {
    ans = max(ans, a[i]-a[i-1]-1)
  }
  return
}

第三题

2275.按位与结果大于零的最长组合

2275.按位与结果大于零的最长组合

题解

题目:给一个数组,数组里面的元素任意可用进行与运算,求与运算之后大于0,参与这次运算的最长元素个数

思路:

1.大于0,说明在二进制位上,某一位,是参与运算的元素都有的
2.既然如此,直接遍历所有元素,将每个元素的二进制位进行累加
3.找某位上最大即可,就说明某位上有几个元素存在该位

代码

func largestCombination(candidates []int) int {
  mp := make(map[int]int)
  ans := 0
  for _, v := range candidates {
    for i := 0; i < 32; i++ {
      if 1<<i&v > 0 {
        mp[i]++
        ans = max(ans, mp[i])
      }
    }
  }
  return ans
}
func max(i, j int) int {
  if i > j {
    return i
  }
  return j
}

第四题

2276.统计区间中的整数数目

2276.统计区间中的整数数目

题解

这种题目,一眼就是线段树,但是不搞算法竞赛ACM,学这个真心感觉没必要,性价比太低,这里直接放灵神的题解

两种做法:珂朵莉树 / 动态开点线段树(Python/Java/C++/Go)

代码

package main
import (
  "github.com/emirpasic/gods/trees/redblacktree"
)
type CountIntervals struct {
  *redblacktree.Tree
  cnt int
}
func Constructor() CountIntervals {
  return CountIntervals{redblacktree.NewWithIntComparator(), 0}
}
func (t *CountIntervals) Add(left, right int) {
  // 遍历所有被 [left,right] 覆盖到的区间(部分覆盖也算)
  for node, _ := t.Ceiling(left); node != nil && node.Value.(int) <= right; node, _ = t.Ceiling(left) {
    l, r := node.Value.(int), node.Key.(int)
    if l < left { // 合并后的新区间,其左端点为所有被覆盖的区间的左端点的最小值
      left = l
    }
    if r > right { // 合并后的新区间,其右端点为所有被覆盖的区间的右端点的最大值
      right = r
    }
    t.cnt -= r - l + 1
    t.Remove(r)
  }
  t.cnt += right - left + 1
  t.Put(right, left) // 所有被覆盖到的区间与 [left,right] 合并成一个新区间
}
func (t *CountIntervals) Count() int { return t.cnt }
type CountIntervals struct {
  left, right *CountIntervals
  l, r, cnt   int
}
func Constructor() CountIntervals {
  return CountIntervals{l: 1, r: 1e9}
}
func (o *CountIntervals) Add(l, r int) {
  if o.cnt == o.r-o.l+1 { // o 已被完整覆盖,无需执行任何操作
    return
  }
  if l <= o.l && o.r <= r { // 当前节点已被区间 [l,r] 完整覆盖,不再继续递归 [l,o.l,l.r,r]
    o.cnt = o.r - o.l + 1
    return
  }
  mid := (o.l + o.r) >> 1
  if o.left == nil { // 动态开点
    o.left = &CountIntervals{l: o.l, r: mid}
  }
  if o.right == nil { // 动态开点
    o.right = &CountIntervals{l: mid + 1, r: o.r}
  }
  if l <= mid {
    o.left.Add(l, r)
  }
  if mid < r {
    o.right.Add(l, r)
  }
  o.cnt = o.left.cnt + o.right.cnt
}
func (o *CountIntervals) Count() int {
  return o.cnt
}


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

热门文章

最新文章