Golang | Leetcode Golang题解之第497题非重叠矩形中的随机点

题目:

题解:

Go 复制代码
type Solution struct {
    rects [][]int
    sum   []int
}

func Constructor(rects [][]int) Solution {
    sum := make([]int, len(rects)+1)
    for i, r := range rects {
        a, b, x, y := r[0], r[1], r[2], r[3]
        sum[i+1] = sum[i] + (x-a+1)*(y-b+1)
    }
    return Solution{rects, sum}
}

func (s *Solution) Pick() []int {
    k := rand.Intn(s.sum[len(s.sum)-1])
    rectIndex := sort.SearchInts(s.sum, k+1) - 1
    r := s.rects[rectIndex]
    a, b, y := r[0], r[1], r[3]
    da := (k - s.sum[rectIndex]) / (y - b + 1)
    db := (k - s.sum[rectIndex]) % (y - b + 1)
    return []int{a + da, b + db}
}
相关推荐
今天头发还在吗6 小时前
【Go】:mac 环境下GoFrame安装开发工具 gf-cli——gf_darwin_arm64
macos·golang·go·gf-cli
是誰萆微了承諾13 小时前
【golang学习笔记 gin 】1.2 redis 的使用
笔记·学习·golang
ifanatic17 小时前
[每周一更]-(第159期):Go 工程师视角:容器化技术(Docker/Kubernetes)与CI/CD流程的应用场景
docker·golang·kubernetes
张烫麻辣亮。18 小时前
golang-gin包
开发语言·golang·gin
Sally璐璐18 小时前
Go正则表达式实战指南
数据库·mysql·golang
yuluo_YX18 小时前
Go Style 代码风格规范
开发语言·后端·golang
qq_1728055920 小时前
Go 自建库的使用教程与测试
开发语言·后端·golang
共享家95271 天前
优先搜索(DFS)实战
算法·leetcode·深度优先
007php0071 天前
某大厂MySQL面试之SQL注入触点发现与SQLMap测试
数据库·python·sql·mysql·面试·职场和发展·golang
flashlight_hi1 天前
LeetCode 分类刷题:2563. 统计公平数对的数目
python·算法·leetcode