当前位置: 首页 > news >正文

755. Pour Water

You are given an elevation map represents as an integer array heights where heights[i] representing the height of the terrain at index i. The width at each index is 1. You are also given two integers volume and kvolume units of water will fall at index k.

Water first drops at the index k and rests on top of the highest terrain or water at that index. Then, it flows according to the following rules:

  • If the droplet would eventually fall by moving left, then move left.
  • Otherwise, if the droplet would eventually fall by moving right, then move right.
  • Otherwise, rise to its current position.

Here, "eventually fall" means that the droplet will eventually be at a lower level if it moves in that direction. Also, level means the height of the terrain plus any water in that column.

We can assume there is infinitely high terrain on the two sides out of bounds of the array. Also, there could not be partial water being spread out evenly on more than one grid block, and each unit of water has to be in exactly one block.

 

Example 1:

Input: heights = [2,1,1,2,1,2,2], volume = 4, k = 3
Output: [2,2,2,3,2,2,2]
Explanation:
The first drop of water lands at index k = 3. When moving left or right, the water can only move to the same level or a lower level. (By level, we mean the total height of the terrain plus any water in that column.)
Since moving left will eventually make it fall, it moves left. (A droplet "made to fall" means go to a lower height than it was at previously.) Since moving left will not make it fall, it stays in place.
The next droplet falls at index k = 3. Since the new droplet moving left will eventually make it fall, it moves left. Notice that the droplet still preferred to move left, even though it could move right (and moving right makes it fall quicker.)
The third droplet falls at index k = 3. Since moving left would not eventually make it fall, it tries to move right. Since moving right would eventually make it fall, it moves right.
Finally, the fourth droplet falls at index k = 3. Since moving left would not eventually make it fall, it tries to move right. Since moving right would not eventually make it fall, it stays in place.

Example 2:

Input: heights = [1,2,3,4], volume = 2, k = 2
Output: [2,3,3,4]
Explanation: The last droplet settles at index 1, since moving further left would not cause it to eventually fall to a lower height.

Example 3:

Input: heights = [3,1,3], volume = 5, k = 1
Output: [4,4,4]
 1 class Solution:
 2     def pourWater(self, heights: List[int], v: int, k: int) -> List[int]:
 3         for _ in range(v):
 4             # 1. Try moving Left
 5             best_index = k
 6             for i in range(k - 1, -1, -1):
 7                 if heights[i] < heights[best_index]:
 8                     best_index = i
 9                 elif heights[i] > heights[best_index]:
10                     break
11             
12             if best_index != k:
13                 heights[best_index] += 1
14                 continue
15                 
16             # 2. Try moving Right
17             for i in range(k + 1, len(heights)):
18                 if heights[i] < heights[best_index]:
19                     best_index = i
20                 elif heights[i] > heights[best_index]:
21                     break
22             
23             # 3. If no better index found, best_index remains k
24             heights[best_index] += 1
25                 
26         return heights
27     

 

http://www.jsqmd.com/news/420762/

相关文章:

  • 2026年值得选的公司认证机构,中安质环认证口碑良好 - 工业品牌热点
  • 京东E卡回收教程,轻松变现不踩坑 - 团团收购物卡回收
  • Java核心技术深度解析:从基础到JVM,构建稳固知识体系
  • 2026年上海时尚婚纱摄影推荐,三川摄影解决消费不透明痛点 - mypinpai
  • 瑞祥商联卡回收平台大比拼,从回收方法、价格、流程分析 - 京回收小程序
  • 2026年奔驰商务现车销售五大优选:聚焦V300L高顶与威霆高顶专卖实力派 - 深度智识库
  • PGSQL 1主2从数据库架构与单节点分3库在三块磁盘理论上限畅想(未测试)
  • 四川地暖厂家哪家好:石墨烯水电气空气能地暖安装全测评,优选成都福熙 - 深度智识库
  • python实现token签名认证
  • 2026最新!AI论文软件 千笔AI VS 灵感风暴AI,本科生写作神器!
  • 开题卡住了?8个AI论文软件深度测评,本科生写论文必备工具推荐!
  • HTML前端笔记
  • 论文AI率超标要延毕?降重侠AI是 2026毕业生的保命神器,一键个位数~ - 资讯焦点
  • 打孔盘下赛道必裂?那是你没见过“倒角”艺术 - RF_RACER
  • 基于微信小程序的心理咨询预约系统2025_ohyab8bm
  • 留学机构服务哪家口碑好,圆梦未来一站式留学服务靠谱吗 - 工业品网
  • C++ thread_local 每个线程的私人小仓库
  • 基于小程序的篮球场馆预订系统2025_lpi6c06n
  • 教你如何回收京东E卡,简单又高效 - 团团收购物卡回收
  • 京东e卡如何回收?团团收平台专业回收教程 - 团团收购物卡回收
  • 基于微信小程序的网络安全知识科普平台2025_6g0b9ke4
  • 中石化加油卡回收有效途径及回收三步曲 - 京回收小程序
  • 【ALL IN AI】ai开发工程师学习
  • 恒利泰科技HT-LFCN-6400+国产替代 LFCN-6400+
  • 真的太省时间! 降AI率软件 千笔·专业降AI率智能体 VS 灵感ai,研究生专属神器!
  • 分期乐京东e卡回收技巧:让你的礼品卡不再闲置! - 团团收购物卡回收
  • 年入百万|靠 8 个维度,把自己活成了一家公司
  • 赶deadline必备! 降AIGC平台 千笔·专业降AIGC智能体 VS 灵感ai
  • 2026年2月ODO油代理商最新推荐,专注食品级原料供应服务 - 品牌鉴赏师
  • 2026盒马鲜生卡回收实用方法,新回收价格 - 京回收小程序