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

推荐订阅源

Cloudbric
Cloudbric
WordPress大学
WordPress大学
博客园 - 叶小钗
B
Blog RSS Feed
T
Tailwind CSS Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
美团技术团队
Scott Helme
Scott Helme
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Hugging Face - Blog
Hugging Face - Blog
T
Threat Research - Cisco Blogs
B
Blog
V
V2EX
Simon Willison's Weblog
Simon Willison's Weblog
I
Intezer
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threatpost
Cisco Talos Blog
Cisco Talos Blog
阮一峰的网络日志
阮一峰的网络日志
C
Cybersecurity and Infrastructure Security Agency CISA
PCI Perspectives
PCI Perspectives
雷峰网
雷峰网
The Register - Security
The Register - Security
博客园 - 【当耐特】
Google DeepMind News
Google DeepMind News
N
News and Events Feed by Topic
H
Hackread – Cybersecurity News, Data Breaches, AI and More
U
Unit 42
Security Latest
Security Latest
NISL@THU
NISL@THU
腾讯CDC
S
SegmentFault 最新的问题
小众软件
小众软件
The GitHub Blog
The GitHub Blog
月光博客
月光博客
A
Arctic Wolf
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
Netflix TechBlog - Medium
IT之家
IT之家
D
DataBreaches.Net
C
CXSECURITY Database RSS Feed - CXSecurity.com
N
News | PayPal Newsroom
L
LINUX DO - 最新话题
博客园 - 司徒正美
大猫的无限游戏
大猫的无限游戏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
J
Java Code Geeks
TaoSecurity Blog
TaoSecurity Blog
P
Privacy International News Feed

博客园 - 北叶青藤

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)