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

推荐订阅源

Hacker News: Ask HN
Hacker News: Ask HN
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
Check Point Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Secure Thoughts
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
The Blog of Author Tim Ferriss
B
Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Google DeepMind News
Google DeepMind News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
A
Arctic Wolf
T
The Exploit Database - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
GbyAI
GbyAI
AWS News Blog
AWS News Blog
MongoDB | Blog
MongoDB | Blog
Y
Y Combinator Blog
Google Online Security Blog
Google Online Security Blog
T
Troy Hunt's Blog
I
InfoQ
L
LINUX DO - 热门话题
WordPress大学
WordPress大学
C
Cisco Blogs
G
GRAHAM CLULEY
The Register - Security
The Register - Security
A
About on SuperTechFans
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Schneier on Security
Schneier on Security
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
H
Hacker News: Front Page
小众软件
小众软件
雷峰网
雷峰网
The Hacker News
The Hacker News
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Tor Project blog
博客园 - 聂微东
N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
The GitHub Blog
The GitHub Blog
腾讯CDC
P
Palo Alto Networks Blog
Scott Helme
Scott Helme

博客园 - 北叶青藤

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)