Imagine a satellite map with land '1' and ocean '0'.
You need to count distinct landmasses. Think of it like infection control: each new unvisited '1' starts a new outbreak cluster — mark all connected cells and count clusters.
def numIslands(grid: list[list[str]]) -> int:
ifnot grid:
return0
rows, cols = len(grid), len(grid[0])
islands = 0def dfs(r, c):
# Base case: out of bounds or waterif r < 0or r >= rows or c < 0or c >= cols or grid[r][c] == '0':
return# Mark as visited (sink the island)
grid[r][c] = '0'# Explore 4 directions
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for r in range(rows):
for c in range(cols):
if grid[r][c] == '1': # Found unvisited land
islands += 1# New island!
dfs(r, c) # Sink entire islandreturn islands
# Trace on small example:# grid = [["1","1","0"],["0","1","0"],["0","0","1"]]# (0,0) = '1' → islands=1, DFS sinks (0,0),(0,1),(1,1)# (2,2) = '1' → islands=2, DFS sinks (2,2)# Result: 2
时间复杂度 / Time: O(M×N) — visit each cell once
空间复杂度 / Space: O(M×N) — recursion stack in worst case (all land)
BFS 版本 / BFS Alternative
from collections import deque
def numIslands_bfs(grid):
rows, cols = len(grid), len(grid[0])
islands = 0for r in range(rows):
for c in range(cols):
if grid[r][c] == '1':
islands += 1
grid[r][c] = '0'
queue = deque([(r, c)])
while queue:
row, col = queue.popleft()
for dr, dc in [(1,0),(-1,0),(0,1),(0,-1)]:
nr, nc = row + dr, col + dc
if0 <= nr < rows and0 <= nc < cols and grid[nr][nc] == '1':
grid[nr][nc] = '0'# Mark immediately to avoid duplicate queue entries
queue.append((nr, nc))
return islands
BFS vs DFS 选哪个?/ Which to choose?
DFS:代码更简洁;深度大时有栈溢出风险
BFS:无递归栈溢出;更适合"最短路径"变体
对本题:两者等价,DFS 更简洁
举一反三 / Pattern Block Preview (1/13)
题目
变化点 Twist
#695 Max Area (next)
不只是数个数,还要统计最大面积
#994 Rotting Oranges
BFS + 时间层 (multi-source BFS)
#207 Course Schedule
有向图 + 环检测 (cycle detection)
#127 Word Ladder
BFS 最短路径(字符变换)
核心模版不变,关键是识别:这是连通性问题还是最短路径问题?
The template stays the same — just identify: connectivity vs. shortest path?
Q: You're a Staff Engineer. Your VP wants a feature shipped in the next sprint, but you assess it will introduce significant tech debt and a security vulnerability. The PM has already committed a date to customers. What do you do?
A fierce price war erupted among AI providers in July. Google launched Gemini 3.6 Flash, 3.5 Flash-Lite, and 3.5 Flash Cyber; xAI released Grok 4.5; OpenAI reportedly released GPT-5.6 variants. Output token costs reportedly dropped from $25–$50 to $4–$6 per 1M tokens — an over 80% reduction.
对开发者的意义 / What it means for devs:
过去"AI 太贵,实验一下就好"的心态正在消失。许多原本因成本放弃的 production AI 功能,现在的成本已经可以接受。
The "AI is too expensive for production" mindset is fading. Features once shelved for cost reasons are now economically viable.
2026 年 7 月 10 日,欧盟《人工智能法案》(AI Act) 正式进入执法阶段。法案以风险分级为核心:高风险 AI 系统(如医疗、执法、就业决策)需通过合规审查;所有 AI 聊天机器人须明确披露其 AI 身份。
On July 10, 2026, the EU AI Act officially entered its enforcement phase. Core principle: risk-based tiers. High-risk AI (medical, law enforcement, employment decisions) must pass compliance reviews. All AI chatbots must disclose they are AI.
对工程师的实际影响 / Practical impact for engineers:
在欧洲部署的 AI 系统需新增"AI 披露"UI
高风险场景需要可解释性日志和人工审核接口
GDPR + AI Act 双重合规成为欧洲 AI 产品标配
In Europe: add AI disclosure UI, log model decisions for explainability, and build human-review hooks for high-risk flows. GDPR + AI Act compliance is now baseline.
MIT/Stanford: Self-Correction Beats Raw Model Size
MIT 和 Stanford 研究人员发表研究指出:对于复杂数学和逻辑推理任务,模型在推理过程中自我纠错的能力比模型参数规模更关键。这一发现支持了"o 系列"和"思维链"推理模型的设计方向。
Researchers from MIT and Stanford found that a model's ability to self-correct during reasoning is a more critical factor for hard math/logic tasks than sheer model size. This validates the design direction of "thinking" models like the o-series and Chain-of-Thought variants.
工程启示 / Engineering takeaway:
在 Agent 系统中,给模型"反思"步骤可能比升级模型更有效
小模型 + 多次推理迭代 > 大模型 + 单次推理(某些场景)
In agent pipelines, adding a reflection/self-critique step may outperform upgrading to a larger model.