Skip to content

Commit 2751993

Browse files
committed
5.18
1 parent 5981842 commit 2751993

File tree

16 files changed

+359
-0
lines changed

16 files changed

+359
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<p>Given <em>n</em> non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.</p>
2+
3+
<p><img src="https://assets.leetcode.com/uploads/2018/10/22/rainwatertrap.png" style="width: 412px; height: 161px;" /><br />
4+
<small>The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. <strong>Thanks Marcos</strong> for contributing this image!</small></p>
5+
6+
<p><strong>Example:</strong></p>
7+
8+
<pre>
9+
<strong>Input:</strong> [0,1,0,2,1,0,1,3,2,1,2,1]
10+
<strong>Output:</strong> 6</pre>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution(object):
2+
def trap(self, height):
3+
"""
4+
:type height: List[int]
5+
:rtype: int
6+
"""
7+
left_max = [0 for _ in height]
8+
right_max = [0 for _ in height]
9+
water = [0 for _ in height]
10+
11+
for i in range(len(height)):
12+
if i - 1 >= 0:
13+
left_max[i] = max(left_max[i - 1], height[i])
14+
else:
15+
left_max[i] = height[i]
16+
17+
for i in range(len(height) - 1, -1, -1):
18+
if i < len(height) - 1:
19+
right_max[i] = max(right_max[i + 1], height[i])
20+
else:
21+
right_max[i] = height[i]
22+
23+
for i in range(len(height)):
24+
tmp = min(left_max[i], right_max[i]) - height[i]
25+
if tmp > 0:
26+
water[i] = tmp
27+
# print height
28+
# print water
29+
# print left_max
30+
# print right_max
31+
return sum(water)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<p>Given a sorted array <em>nums</em>, remove the duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that duplicates appeared at most&nbsp;<em>twice</em> and return the new length.</p>
2+
3+
<p>Do not allocate extra space for another array, you must do this by <strong>modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a></strong> with O(1) extra memory.</p>
4+
5+
<p><strong>Example 1:</strong></p>
6+
7+
<pre>
8+
Given <em>nums</em> = <strong>[1,1,1,2,2,3]</strong>,
9+
10+
Your function should return length = <strong><code>5</code></strong>, with the first five elements of <em><code>nums</code></em> being <strong><code>1, 1, 2, 2</code></strong> and <strong>3</strong> respectively.
11+
12+
It doesn&#39;t matter what you leave beyond the returned length.</pre>
13+
14+
<p><strong>Example 2:</strong></p>
15+
16+
<pre>
17+
Given <em>nums</em> = <strong>[0,0,1,1,1,1,2,3,3]</strong>,
18+
19+
Your function should return length = <strong><code>7</code></strong>, with the first seven elements of <em><code>nums</code></em> being modified to&nbsp;<strong><code>0</code></strong>, <strong>0</strong>, <strong>1</strong>, <strong>1</strong>, <strong>2</strong>, <strong>3</strong> and&nbsp;<strong>3</strong> respectively.
20+
21+
It doesn&#39;t matter what values are set beyond&nbsp;the returned length.
22+
</pre>
23+
24+
<p><strong>Clarification:</strong></p>
25+
26+
<p>Confused why the returned value is an integer but your answer is an array?</p>
27+
28+
<p>Note that the input array is passed in by <strong>reference</strong>, which means modification to the input array will be known to the caller as well.</p>
29+
30+
<p>Internally you can think of this:</p>
31+
32+
<pre>
33+
// <strong>nums</strong> is passed in by reference. (i.e., without making a copy)
34+
int len = removeDuplicates(nums);
35+
36+
// any modification to <strong>nums</strong> in your function would be known by the caller.
37+
// using the length returned by your function, it prints the first <strong>len</strong> elements.
38+
for (int i = 0; i &lt; len; i++) {
39+
&nbsp; &nbsp; print(nums[i]);
40+
}
41+
</pre>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution(object):
2+
def removeDuplicates(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
i = 0
8+
for num in nums:
9+
if i < 2 or num != nums[i - 2]:
10+
nums[i] = num
11+
i += 1
12+
return i
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<p>Given <em>n</em> non-negative integers representing the histogram&#39;s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.</p>
2+
3+
<p>&nbsp;</p>
4+
5+
<p><img src="https://assets.leetcode.com/uploads/2018/10/12/histogram.png" style="width: 188px; height: 204px;" /><br />
6+
<small>Above is a histogram where width of each bar is 1, given height = <code>[2,1,5,6,2,3]</code>.</small></p>
7+
8+
<p>&nbsp;</p>
9+
10+
<p><img src="https://assets.leetcode.com/uploads/2018/10/12/histogram_area.png" style="width: 188px; height: 204px;" /><br />
11+
<small>The largest rectangle is shown in the shaded area, which has area = <code>10</code> unit.</small></p>
12+
13+
<p>&nbsp;</p>
14+
15+
<p><strong>Example:</strong></p>
16+
17+
<pre>
18+
<strong>Input:</strong> [2,1,5,6,2,3]
19+
<strong>Output:</strong> 10
20+
</pre>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def largestRectangleArea(self, heights):
3+
"""
4+
:type heights: List[int]
5+
:rtype: int
6+
"""
7+
res = 0
8+
stack = list()
9+
heights = [0] + heights + [0]
10+
11+
for i in range(len(heights)):
12+
while stack and heights[stack[-1]] > heights[i]:
13+
top = stack.pop()
14+
res = max(res, (i - stack[-1] - 1) * heights[top])
15+
16+
stack.append(i)
17+
18+
return res
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<p>Given an array of integers, find if the array contains any duplicates.</p>
2+
3+
<p>Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.</p>
4+
5+
<p><strong>Example 1:</strong></p>
6+
7+
<pre>
8+
<strong>Input:</strong> [1,2,3,1]
9+
<strong>Output:</strong> true</pre>
10+
11+
<p><strong>Example 2:</strong></p>
12+
13+
<pre>
14+
<strong>Input: </strong>[1,2,3,4]
15+
<strong>Output:</strong> false</pre>
16+
17+
<p><strong>Example 3:</strong></p>
18+
19+
<pre>
20+
<strong>Input: </strong>[1,1,1,3,3,4,3,2,4,2]
21+
<strong>Output:</strong> true</pre>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution(object):
2+
def containsDuplicate(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: bool
6+
"""
7+
if len(nums) <= 1:
8+
return False
9+
nums.sort()
10+
for index in range(0,len(nums)-1):
11+
if nums[index] == nums[index +1]:#or nums[index] == nums[index-1]:
12+
return True
13+
return False
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<p>Given an array of integers and an integer <i>k</i>, find out whether there are two distinct indices <i>i</i> and <i>j</i> in the array such that <b>nums[i] = nums[j]</b> and the <b>absolute</b> difference between <i>i</i> and <i>j</i> is at most <i>k</i>.</p>
2+
3+
<div>
4+
<p><strong>Example 1:</strong></p>
5+
6+
<pre>
7+
<strong>Input: </strong>nums = <span id="example-input-1-1">[1,2,3,1]</span>, k = <span id="example-input-1-2">3</span>
8+
<strong>Output: </strong><span id="example-output-1">true</span>
9+
</pre>
10+
11+
<div>
12+
<p><strong>Example 2:</strong></p>
13+
14+
<pre>
15+
<strong>Input: </strong>nums = <span id="example-input-2-1">[1,0,1,1]</span>, k = <span id="example-input-2-2">1</span>
16+
<strong>Output: </strong><span id="example-output-2">true</span>
17+
</pre>
18+
19+
<div>
20+
<p><strong>Example 3:</strong></p>
21+
22+
<pre>
23+
<strong>Input: </strong>nums = <span id="example-input-3-1">[1,2,3,1,2,3]</span>, k = <span id="example-input-3-2">2</span>
24+
<strong>Output: </strong><span id="example-output-3">false</span>
25+
</pre>
26+
</div>
27+
</div>
28+
</div>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def containsNearbyDuplicate(self, nums, k):
3+
"""
4+
:type nums: List[int]
5+
:type k: int
6+
:rtype: bool
7+
"""
8+
record = dict()
9+
for i, num in enumerate(nums):
10+
# print record
11+
if record.get(num, -1) != -1:
12+
if i - record[num] <= k:
13+
return True
14+
record[num] = i
15+
return False
16+

0 commit comments

Comments
 (0)