-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc0e53c
commit bd73ea0
Showing
3 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
--- | ||
title: 42.接雨水 | ||
date: 2024-03-20 | ||
lang: 'zh-CN' | ||
sidebar: 'auto' | ||
categories: | ||
- LeeCode | ||
tags: | ||
location: HangZhou | ||
--- | ||
|
||
# Heading | ||
|
||
[[toc]] | ||
|
||
[42.接雨水](https://leetcode.cn/problems/trapping-rain-water/description/) | ||
|
||
Tags: algorithms amazon apple bloomberg google twitter zenefits array two-pointers stack | ||
|
||
Langs: c cpp csharp dart elixir erlang golang java javascript kotlin php python python3 racket ruby rust scala swift typescript | ||
|
||
- algorithms | ||
- Hard (63.28%) | ||
- Likes: 5071 | ||
- Dislikes: - | ||
- Total Accepted: 901.1K | ||
- Total Submissions: 1.4M | ||
- Testcase Example: '[0,1,0,2,1,0,1,3,2,1,2,1]' | ||
|
||
<p>给定 <code>n</code> 个非负整数表示每个宽度为 <code>1</code> 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。</p> | ||
|
||
<p> </p> | ||
|
||
<p><strong>示例 1:</strong></p> | ||
|
||
<p><img src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/10/22/rainwatertrap.png" style="height: 161px; width: 412px;" /></p> | ||
|
||
<pre> | ||
<strong>输入:</strong>height = [0,1,0,2,1,0,1,3,2,1,2,1] | ||
<strong>输出:</strong>6 | ||
<strong>解释:</strong>上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 | ||
</pre> | ||
|
||
<p><strong>示例 2:</strong></p> | ||
|
||
<pre> | ||
<strong>输入:</strong>height = [4,2,0,3,2,5] | ||
<strong>输出:</strong>9 | ||
</pre> | ||
|
||
<p> </p> | ||
|
||
<p><strong>提示:</strong></p> | ||
|
||
<ul> | ||
<li><code>n == height.length</code></li> | ||
<li><code>1 <= n <= 2 * 10<sup>4</sup></code></li> | ||
<li><code>0 <= height[i] <= 10<sup>5</sup></code></li> | ||
</ul> | ||
|
||
<<< @/src/LeeCode/42.接雨水.js | ||
<<< @/src/LeeCode/42.接雨水1.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* @lc app=leetcode.cn id=42 lang=javascript | ||
* | ||
* [42] 接雨水 | ||
* 乱七八糟解出来的……,固定左指针寻找下一个右侧边际 | ||
*/ | ||
|
||
// @lc code=start | ||
/** | ||
* @param {number[]} height | ||
* @return {number} | ||
*/ | ||
var trap = function (height) { | ||
let i = 0 | ||
let j = 0 | ||
let sum = 0 | ||
|
||
while (i < height.length) { | ||
if (height[i] > 0) { | ||
j = i + 1 | ||
let tmpHeight = 0 | ||
let tmpMax = 0 | ||
let rightIndex = 0 | ||
while (j < height.length && height[j] < height[i]) { | ||
if (height[j] > tmpMax) { | ||
tmpMax = height[j] | ||
rightIndex = j | ||
} | ||
tmpHeight += height[j++] | ||
} | ||
if (j < height.length) { | ||
rightIndex = j | ||
} else if (rightIndex > 0) { | ||
let k = rightIndex | ||
while (k < j) { | ||
tmpHeight -= height[k++] | ||
} | ||
} | ||
|
||
if (rightIndex > 0) { | ||
sum += (rightIndex - i - 1) * Math.min(height[i], height[rightIndex]) - tmpHeight | ||
i = rightIndex | ||
continue | ||
} | ||
} | ||
i++ | ||
} | ||
return sum | ||
} | ||
// @lc code=end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* @lc app=leetcode.cn id=42 lang=javascript | ||
* | ||
* [42] 接雨水 | ||
* 双指针 | ||
*/ | ||
|
||
// @lc code=start | ||
/** | ||
* @param {number[]} height | ||
* @return {number} | ||
*/ | ||
var trap = function (height) { | ||
let left = 0 | ||
let right = height.length - 1 | ||
let leftMax = 0 | ||
let rightMax = 0 | ||
let sum = 0 | ||
while (left < right) { | ||
leftMax = Math.max(leftMax, height[left]) | ||
rightMax = Math.max(rightMax, height[right]) | ||
|
||
if (height[left] < height[right]) { | ||
sum += leftMax - height[left++] | ||
} else { | ||
sum += rightMax - height[right--] | ||
} | ||
} | ||
return sum | ||
} | ||
// @lc code=end |