惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
D
Docker
F
Fortinet All Blogs
MongoDB | Blog
MongoDB | Blog
月光博客
月光博客
罗磊的独立博客
N
Netflix TechBlog - Medium
Y
Y Combinator Blog
博客园 - 司徒正美
T
Tailwind CSS Blog
C
Check Point Blog
V
V2EX
Microsoft Azure Blog
Microsoft Azure Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Proofpoint News Feed
L
LangChain Blog
D
DataBreaches.Net
酷 壳 – CoolShell
酷 壳 – CoolShell
有赞技术团队
有赞技术团队
腾讯CDC
Last Week in AI
Last Week in AI
Jina AI
Jina AI
博客园 - Franky
量子位

博客园 - 北叶青藤

2096. Step-By-Step Directions From a Binary Tree Node to Another Find path from root to a target node in Binary Tree When Dijkstra Algorithm Should be Use? 1188. Design Bounded Blocking Queue 1115. Print FooBar Alternately 1114. Print in Order 1242. Web Crawler Multithreaded Python Multi-threading bot ip Log Rate Limiter Same Word of HTML Labels Most Frequent Call Chain remove prefix in a words list 1102. Path With Maximum Minimum Value Property Booking Optimizer minimum number 755. Pour Water Keyword Tagging in Reviews with Overlapping Matches Retryer Function Implementation 1125. Smallest Sufficient Team Split stay Task scheduling problem 滑雪问题 845. Longest Mountain in Array 723. Candy Crush 1539. Kth Missing Positive Number 1650. Lowest Common Ancestor of a Binary Tree III 424. Longest Repeating Character Replacement 843. Guess the Word 551. Student Attendance Record I
Print the terrain
北叶青藤 · 2026-02-23 · via 博客园 - 北叶青藤

"You are given an array like [5, 4, 3, 2, 1, 3, 4, 0, 3, 4]

Part 1:
Print a terrain where each number represents the height of a column at that index.

image

 1 def print_terrain(heights):
 2     if not heights:
 3         return
 4 
 5     max_height = max(heights)
 6 
 7     # Iterate from the highest point down to 1
 8     for level in range(max_height, 0, -1):
 9         row = ""
10         for h in heights:
11             if h >= level:
12                 row += "+"
13             else:
14                 row += " "
15         print(row)
16 
17     # Print the base layer (level 0)
18     print("+" * len(heights) + " <--- base layer")
19 
20 # The data
21 data = [5, 4, 3, 2, 1, 3, 4, 0, 3, 4]
22 print_terrain(data)

Part 2:
Imagine we drop a certain amount of water at a certain column. The water can flow in whichever direction makes sense. Print the terrain after all the water has fallen.

dumpWater(terrain, waterAmount=8, column=1)

Should render
+
++WWWW+ +
+++WW++ ++
++++W++ ++
+++++++W++
++++++++++ <--- base layer"

 1 def dump_water(heights, water_amount, column):
 2     # Use a separate list to track water so we can render '+' and 'W' separately
 3     water = [0] * len(heights)
 4     
 5     for _ in range(water_amount):
 6         curr = column
 7         
 8         # 1. Try to move Left
 9         best_left = curr
10         for i in range(curr - 1, -1, -1):
11             if heights[i] + water[i] < heights[best_left] + water[best_left]:
12                 best_left = i
13             elif heights[i] + water[i] > heights[best_left] + water[best_left]:
14                 break # Hit a wall
15         
16         if best_left < curr:
17             water[best_left] += 1
18             continue # Water unit settled to the left
19 
20         # 2. Try to move Right
21         best_right = curr
22         for i in range(curr + 1, len(heights)):
23             if heights[i] + water[i] < heights[best_right] + water[best_right]:
24                 best_right = i
25             elif heights[i] + water[i] > heights[best_right] + water[best_right]:
26                 break # Hit a wall
27         
28         # Regardless of if it moved right or stayed at 'curr', increment
29         water[best_right] += 1
30 
31     return water
32 
33 def render_water_terrain(heights, water):
34     max_h = max(h + w for h, w in zip(heights, water))
35     
36     for level in range(max_h, 0, -1):
37         row = ""
38         for i in range(len(heights)):
39             if level <= heights[i]:
40                 row += "+"
41             elif level <= heights[i] + water[i]:
42                 row += "W"
43             else:
44                 row += " "
45         print(row)
46     print("+" * len(heights) + " <--- base layer")
47 
48 # Execution
49 terrain = [5, 4, 3, 2, 1, 3, 4, 0, 3, 4]
50 water_dist = dump_water(terrain, water_amount=8, column=1)
51 render_water_terrain(terrain, water_dist)