注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
整理了 Block (Square) SDE Coding 轮非常经典的三道多问递进题(Number of Black Blocks、In-Memory BIN Database、Connect Four),把每一问的思路演进、边界坑点和完整 Go 代码分享出来,希望能帮到正在准备 Block 面试的同学,觉得有帮助的话求加点大米!
---
一、Number of Black Blocks(2×2 黑块统计,3 问递进)
题目与递进要求
- Q1(计数): 给定 m × n 网格(2 ≤ m, n ≤ 10^5)和 k 个不重复黑格坐标(0 ≤ k ≤ min(10^5, m×n)),统计所有 2×2 子块中恰好包含 0, 1, 2, 3, 4 个黑格的数量,返回长度为 5 的数组 ans[0..4]。
- Q2(反查覆盖): 改成对每个黑格,返回覆盖它的所有合法 2×2 子块(用左上角坐标表示)。
- Q3(泛化尺寸): 子块定义变化(例如扩展到任意 size × size),在已有代码基础上继续重构。
解题思路
Go 实现- type Game struct {
- columns [][]string
- width int
- winAt int
- }
- func Init(width, winAt int) [/i]Game {
- return &Game{
- columns: make([][]string, width),
- width: width,
- winAt: winAt,
- }
- }
- func (g [i]Game) Drop(col int, color string) bool {
- g.columns[col] = append(g.columns[col], color)
- return g.hasWin(col)
- }
- func (g [/i]Game) hasWin(col int) bool {
- height := len(g.columns[col])
- color := g.columns[col][height-1]
- // 1. 纵向检查(只需向下)
- count := 0
- for i := height - 1; i >= 0 && g.columns[col][i] == color; i-- {
- count++
- }
- if count >= g.winAt {
- return true
- }
- // 2. 横向检查(向右 + 向左)
- count = 0
- for i := col; i < len(g.columns); i++ {
- if len(g.columns[i]) < height || g.columns[i][height-1] != color {
- break
- }
- count++
- }
- for i := col - 1; i >= 0; i-- {
- if len(g.columns[i]) < height || g.columns[i][height-1] != color {
- break
- }
- count++
- }
- return count >= g.winAt
- }
复制代码
---
备考小结
Block Coding 轮的核心特点是 多问渐进 + 写可运行的 clean code:第一问通常非常简单,重点在于提前把数据结构抽象干净(如把 blocksOf 抽成独立 helper、用 Trie 存每个字段的 、按列存棋盘),后面加需求时改动最小、不容易写乱。祝大家面试顺利,顺便求点大米! |