Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram.
Example 1:
<pre>Input: heights = [2,1,5,6,2,3]
Output: 10
Explanation: The above is a histogram where width of each bar is 1.
The largest rectangle is shown in the red area, which has an area = 10 units.
</pre>
大厦题,首尾加入节点:
LeetCode 084 Largest Rectangle in Histogram
LeetCode 218 The Skyline Problem
Python代码:
1 2 3 4 5 6 7 8 9 10
deflargestRectangleArea(self, heights: List[int]) -> int: stack, res = [], 0 heights.insert(0, 0) # remember heights.append(0) for i inrange(len(heights)): while stack and heights[i] < heights[stack[-1]]: index = stack.pop() res = max(res, (i - stack[-1] - 1) * heights[index]) # remember i - stack[-1] - 1 not i - index stack.append(i) return res
defsubsets(self, nums: List[int]) -> List[List[int]]: ifnot nums: return [] res = [[]] self.dfs(nums, 0, [], res) return res
defdfs(self, nums, st, path, res): if st == len(nums): return for i inrange(st, len(nums)): path.append(nums[i]) res.append(list(path)) self.dfs(nums, i + 1, path, res) path.pop()
Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.
Example 1:
<pre>Input: head = [1,2,3,4,5], left = 2, right = 4
Output: [1,4,3,2,5]
</pre>
Example 2:
<pre>Input: head = [5], left = 1, right = 1
Output: [5]
</pre>
start, end = fake_head, head while <反转链表长度>: # delete the node moved_node, end.next = end.next, end.next.next # insert the moved_node start.next, moved_node.next = moved_node, start.next
解题步骤:
N/A
注意事项:
经典题,见LeetCode 2074 Reverse Nodes in Even Length Groups。 思路是锁定start和end节点,将end的后续节点一个个加到start直接后续
第二个循环中,right要记得减一,否则死循环
Python代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
defreverseBetween(self, head: ListNode, left: int, right: int) -> ListNode: left, right = left - 1, right - 1 fake_head = ListNode(0) fake_head.next = head it = fake_head while left > 0: it = it.next left -= 1 right -= 1 start, end = it, it.next while right > 0: moved_node, end.next = end.next, end.next.next# delete a node start.next, moved_node.next = moved_node, start.next# insert a node right -= 1# remember return fake_head.next
The nodes in the linked list are sequentially assigned to non-empty groups whose lengths form the sequence of the natural numbers (1, 2, 3, 4, ...). The length of a group is the number of nodes assigned to it. In other words,
The 1<sup>st</sup> node is assigned to the first group.
The 2<sup>nd</sup> and the 3<sup>rd</sup> nodes are assigned to the second group.
The 4<sup>th</sup>, 5<sup>th</sup>, and 6<sup>th</sup> nodes are assigned to the third group, and so on.
Note that the length of the last group may be less than or equal to 1 + the length of the second to last group.
Reverse the nodes in each group with an even length, and return theheadof the modified linked list.
Example 1:
<pre>Input: head = [5,2,6,3,9,1,7,3,8,4]
Output: [5,6,2,3,9,1,4,8,3,7]
Explanation:
The length of the first group is 1, which is odd, hence no reversal occurrs.
The length of the second group is 2, which is even, hence the nodes are reversed.
The length of the third group is 3, which is odd, hence no reversal occurrs.
The length of the last group is 4, which is even, hence the nodes are reversed.
</pre>
Example 2:
<pre>Input: head = [1,1,0,6]
Output: [1,0,1,6]
Explanation:
The length of the first group is 1. No reversal occurrs.
The length of the second group is 2. The nodes are reversed.
The length of the last group is 1. No reversal occurrs.
</pre>
Example 3:
<pre>Input: head = [2,1]
Output: [2,1]
Explanation:
The length of the first group is 1. No reversal occurrs.
The length of the last group is 1. No reversal occurrs.
</pre>
Example 4:
<pre>Input: head = [8]
Output: [8]
Explanation: There is only one group whose length is 1. No reversal occurrs.
</pre>
Constraints:
The number of nodes in the list is in the range [1, 10<sup>5</sup>].
start(group n) -> end(group n+1, head of group n+1 will become new tail after reversed) -> ...
不断将end直接后面的节点加到start直接后面
start(group n) -> NodeA (新状态) -> ... -> end(group n+1) -> NodeA (前状态) -> ...
defreverseEvenLengthGroups(self, head: Optional[ListNode]) -> Optional[ListNode]: group, cur = 2, head while cur.next: cur = self.process_one_group(cur, group) group += 1 return head
defprocess_one_group(self, tail_of_last: ListNode, n: int) -> ListNode: cur, count = tail_of_last, 0 while cur.nextand count < n: cur = cur.next count += 1 if count % 2 == 0: start, end = tail_of_last, tail_of_last.next for i inrange(count - 1): # delete the node moved_node, end.next = end.next, end.next.next # insert the moved_node start.next, moved_node.next = moved_node, start.next cur = tail_of_last for i inrange(count): cur = cur.next return cur