Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions README.md

Large diffs are not rendered by default.

25 changes: 8 additions & 17 deletions problems/array-of-doubled-pairs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,45 @@

## [954. Array of Doubled Pairs (Medium)](https://leetcode.com/problems/array-of-doubled-pairs "二倍数对数组")

<p>Given an array of integers <code>A</code>&nbsp;with even length, return <code>true</code> if and only if it is possible to reorder it such that <code>A[2 * i + 1] = 2 * A[2 * i]</code> for every <code>0 &lt;=&nbsp;i &lt; len(A) / 2</code>.</p>

<div>
<div>
<div>
<ol>
</ol>
</div>
</div>
</div>
<p>Given an array of integers <code>arr</code> of even length, return <code>true</code> if and only if it is possible to reorder it such that <code>arr[2 * i + 1] = 2 * arr[2 * i]</code> for every <code>0 &lt;= i &lt; len(arr) / 2</code>.</p>

<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>

<pre>
<strong>Input:</strong> A = [3,1,3,6]
<strong>Input:</strong> arr = [3,1,3,6]
<strong>Output:</strong> false
</pre>

<p><strong>Example 2:</strong></p>

<pre>
<strong>Input:</strong> A = [2,1,2,6]
<strong>Input:</strong> arr = [2,1,2,6]
<strong>Output:</strong> false
</pre>

<p><strong>Example 3:</strong></p>

<pre>
<strong>Input:</strong> A = [4,-2,2,-4]
<strong>Input:</strong> arr = [4,-2,2,-4]
<strong>Output:</strong> true
<strong>Explanation:</strong> We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
</pre>

<p><strong>Example 4:</strong></p>

<pre>
<strong>Input:</strong> A = [1,2,4,16,8,4]
<strong>Input:</strong> arr = [1,2,4,16,8,4]
<strong>Output:</strong> false
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>0 &lt;= A.length &lt;= 3 *&nbsp;10<sup>4</sup></code></li>
<li><code>A.length</code> is even.</li>
<li><code>-10<sup>5</sup> &lt;= A[i] &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= arr.length &lt;= 3 * 10<sup>4</sup></code></li>
<li><code>arr.length</code> is even.</li>
<li><code>-10<sup>5</sup> &lt;= arr[i] &lt;= 10<sup>5</sup></code></li>
</ul>

### Related Topics
Expand Down
4 changes: 2 additions & 2 deletions problems/asteroid-collision/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<pre>
<strong>Input:</strong> asteroids = [5,10,-5]
<strong>Output:</strong> [5,10]
<b>Explanation:</b> The 10 and -5 collide resulting in 10. The 5 and 10 never collide.
<b>Explanation:</b> The 10 and -5 collide resulting in 10. The 5 and 10 never collide.
</pre>

<p><strong>Example 2:</strong></p>
Expand Down Expand Up @@ -54,7 +54,7 @@
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= asteroids &lt;= 10<sup>4</sup></code></li>
<li><code>2 &lt;= asteroids &lt;= 10<sup>4</sup></code></li>
<li><code>-1000 &lt;= asteroids[i] &lt;= 1000</code></li>
<li><code>asteroids[i] != 0</code></li>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion problems/average-time-of-process-per-machine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

[Next >](../check-if-two-string-arrays-are-equivalent "Check If Two String Arrays are Equivalent")

## [1661. Average Time of Process per Machine (Easy)](https://leetcode.com/problems/average-time-of-process-per-machine "")
## [1661. Average Time of Process per Machine (Easy)](https://leetcode.com/problems/average-time-of-process-per-machine "每台机器的进程平均运行时间")


23 changes: 16 additions & 7 deletions problems/best-time-to-buy-and-sell-stock/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,38 @@

## [121. Best Time to Buy and Sell Stock (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock "买卖股票的最佳时机")

<p>Say you have an array for which the <em>i</em><sup>th</sup> element is the price of a given stock on day <em>i</em>.</p>
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p>

<p>If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.</p>
<p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that stock.</p>

<p>Note that you cannot sell a stock before you buy one.</p>
<p>Return <em>the maximum profit you can achieve from this transaction</em>. If you cannot achieve any profit, return <code>0</code>.</p>

<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>

<pre>
<strong>Input:</strong> [7,1,5,3,6,4]
<strong>Input:</strong> prices = [7,1,5,3,6,4]
<strong>Output:</strong> 5
<strong>Explanation:</strong> Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
&nbsp; Not 7-1 = 6, as selling price needs to be larger than buying price.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
</pre>

<p><strong>Example 2:</strong></p>

<pre>
<strong>Input:</strong> [7,6,4,3,1]
<strong>Input:</strong> prices = [7,6,4,3,1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> In this case, no transaction is done, i.e. max profit = 0.
<strong>Explanation:</strong> In this case, no transactions are done and the max profit = 0.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= prices.length &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= prices[i] &lt;= 10<sup>4</sup></code></li>
</ul>

### Related Topics
[[Array](../../tag/array/README.md)]
[[Dynamic Programming](../../tag/dynamic-programming/README.md)]
Expand Down
8 changes: 6 additions & 2 deletions problems/binary-tree-maximum-path-sum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,27 @@

## [124. Binary Tree Maximum Path Sum (Hard)](https://leetcode.com/problems/binary-tree-maximum-path-sum "二叉树中的最大路径和")

<p>Given the <code>root</code> of a binary tree, return <em>the maximum path sum</em>.</p>
<p>A <strong>path</strong> in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence <strong>at most once</strong>. Note that the path does not need to pass through the root.</p>

<p>For this problem, a path is defined as any node sequence from some starting node to any node in the tree along the parent-child connections. The path must contain <strong>at least one node</strong> and does not need to go through the root.</p>
<p>The <strong>path sum</strong> of a path is the sum of the node&#39;s values in the path.</p>

<p>Given the <code>root</code> of a binary tree, return <em>the maximum <strong>path sum</strong> of any path</em>.</p>

<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/13/exx1.jpg" style="width: 322px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [1,2,3]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The optimal path is 2 -&gt; 1 -&gt; 3 with a path sum of 2 + 1 + 3 = 6.
</pre>

<p><strong>Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/13/exx2.jpg" />
<pre>
<strong>Input:</strong> root = [-10,9,20,null,null,15,7]
<strong>Output:</strong> 42
<strong>Explanation:</strong> The optimal path is 15 -&gt; 20 -&gt; 7 with a path sum of 15 + 20 + 7 = 42.
</pre>

<p>&nbsp;</p>
Expand Down
7 changes: 1 addition & 6 deletions problems/binary-tree-preorder-traversal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,7 @@
</ul>

<p>&nbsp;</p>

<p><strong>Follow up:</strong></p>

<p>Recursive solution is trivial, could you do it iteratively?</p>

<p>&nbsp;</p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>

### Related Topics
[[Stack](../../tag/stack/README.md)]
Expand Down
39 changes: 16 additions & 23 deletions problems/boats-to-save-people/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,42 @@

## [881. Boats to Save People (Medium)](https://leetcode.com/problems/boats-to-save-people "救生艇")

<p>The <code>i</code>-th person has weight <code>people[i]</code>, and each boat can carry a maximum weight of <code>limit</code>.</p>
<p>You are given an array <code>people</code> where <code>people[i]</code> is the weight of the <code>i<sup>th</sup></code> person, and an <strong>infinite number of boats</strong> where each boat can carry a maximum weight of <code>limit</code>. Each boat carries at most two people at the same time, provided the sum of the weight of those people is at most <code>limit</code>.</p>

<p>Each boat carries at most 2 people at the same time, provided the sum of the&nbsp;weight of those people is at most <code>limit</code>.</p>

<p>Return the minimum number of boats to carry every given person.&nbsp; (It is guaranteed each person can be carried by a boat.)</p>
<p>Return <em>the minimum number of boats to carry every given person</em>.</p>

<p>&nbsp;</p>

<div>
<p><strong>Example 1:</strong></p>

<pre>
<strong>Input: </strong>people = <span id="example-input-1-1">[1,2]</span>, limit = <span id="example-input-1-2">3</span>
<strong>Output: </strong><span id="example-output-1">1</span>
<strong>Explanation: </strong>1 boat (1, 2)
<strong>Input:</strong> people = [1,2], limit = 3
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 boat (1, 2)
</pre>

<div>
<p><strong>Example 2:</strong></p>

<pre>
<strong>Input: </strong>people = <span id="example-input-2-1">[3,2,2,1]</span>, limit = <span id="example-input-2-2">3</span>
<strong>Output: </strong><span id="example-output-2">3</span>
<strong>Explanation</strong>: 3 boats (1, 2), (2) and (3)
<strong>Input:</strong> people = [3,2,2,1], limit = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 boats (1, 2), (2) and (3)
</pre>

<div>
<p><strong>Example 3:</strong></p>

<pre>
<strong>Input: </strong>people = <span id="example-input-3-1">[3,5,3,4]</span>, limit = <span id="example-input-3-2">5</span>
<strong>Output: </strong><span id="example-output-3">4</span>
<strong>Explanation</strong>: 4 boats (3), (3), (4), (5)</pre>
<strong>Input:</strong> people = [3,5,3,4], limit = 5
<strong>Output:</strong> 4
<strong>Explanation:</strong> 4 boats (3), (3), (4), (5)
</pre>

<p><strong>Note</strong>:</p>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;=&nbsp;people.length &lt;= 50000</code></li>
<li><code>1 &lt;= people[i] &lt;=&nbsp;limit &lt;= 30000</code></li>
<li><code>1 &lt;= people.length &lt;= 5 * 10<sup>4</sup></code></li>
<li><code>1 &lt;= people[i] &lt;= limit &lt;= 3 * 10<sup>4</sup></code></li>
</ul>
</div>
</div>
</div>

### Related Topics
[[Greedy](../../tag/greedy/README.md)]
Expand Down
76 changes: 76 additions & 0 deletions problems/building-boxes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
<!--+----------------------------------------------------------------------+-->
<!--|@author openset <openset.wang@gmail.com> |-->
<!--|@link https://github.com/openset |-->
<!--|@home https://github.com/openset/leetcode |-->
<!--+----------------------------------------------------------------------+-->

[< Previous](../find-kth-largest-xor-coordinate-value "Find Kth Largest XOR Coordinate Value")

[Next >](../find-distance-in-a-binary-tree "Find Distance in a Binary Tree")

## [1739. Building Boxes (Hard)](https://leetcode.com/problems/building-boxes "放置盒子")

<p>You have a cubic storeroom where the width, length, and height of the room are all equal to <code>n</code> units. You are asked to place <code>n</code> boxes in this room where each box is a cube of unit side length. There are however some rules to placing the boxes:</p>

<ul>
<li>You can place the boxes anywhere on the floor.</li>
<li>If box <code>x</code> is placed on top of the box <code>y</code>, then each side of the four vertical sides of the box <code>y</code> <strong>must</strong> either be adjacent to another box or to a wall.</li>
</ul>

<p>Given an integer <code>n</code>, return<em> the <strong>minimum</strong> possible number of boxes touching the floor.</em></p>

<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2021/01/04/3-boxes.png" style="width: 135px; height: 143px;" /></p>

<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> The figure above is for the placement of the three boxes.
These boxes are placed in the corner of the room, where the corner is on the left side.
</pre>

<p><strong>Example 2:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2021/01/04/4-boxes.png" style="width: 135px; height: 179px;" /></p>

<pre>
<strong>Input:</strong> n = 4
<strong>Output:</strong> 3
<strong>Explanation:</strong> The figure above is for the placement of the four boxes.
These boxes are placed in the corner of the room, where the corner is on the left side.
</pre>

<p><strong>Example 3:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2021/01/04/10-boxes.png" style="width: 271px; height: 257px;" /></p>

<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 6
<strong>Explanation:</strong> The figure above is for the placement of the ten boxes.
These boxes are placed in the corner of the room, where the corner is on the back side.</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li>
</ul>

### Related Topics
[[Math](../../tag/math/README.md)]
[[Binary Search](../../tag/binary-search/README.md)]

### Hints
<details>
<summary>Hint 1</summary>
Suppose We can put m boxes on the floor, within all the ways to put the boxes, what’s the maximum number of boxes we can put in?
</details>

<details>
<summary>Hint 2</summary>
The first box should always start in the corner
</details>
6 changes: 3 additions & 3 deletions problems/burst-balloons/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<p>You are given <code>n</code> balloons, indexed from <code>0</code> to <code>n - 1</code>. Each balloon is painted with a number on it represented by an array <code>nums</code>. You are asked to burst all the balloons.</p>

<p>If you burst the <code>i<sup>th</sup></code> balloon, you will get <code>nums[left] * nums[i] * nums[right]</code> coins. Here <code>left</code> and <code>right</code> are adjacent indices of <code>i</code>. After the burst, the <code>left</code> and <code>right</code> then becomes adjacent.</p>
<p>If you burst the <code>i<sup>th</sup></code> balloon, you will get <code>nums[i - 1] * nums[i] * nums[i + 1]</code> coins. If <code>i - 1</code> or <code>i + 1</code> goes out of bounds of the array, then treat it as if there is a balloon with a <code>1</code> painted on it.</p>

<p>Return <em>the maximum coins you can collect by bursting the balloons wisely</em>.</p>

Expand All @@ -23,8 +23,8 @@
<pre>
<strong>Input:</strong> nums = [3,1,5,8]
<strong>Output:</strong> 167
<code><strong>Explanation:
</strong></code>nums = [3,1,5,8] --&gt; [3,5,8] --&gt; [3,8] --&gt; [8] --&gt; []
<strong>Explanation:</strong>
nums = [3,1,5,8] --&gt; [3,5,8] --&gt; [3,8] --&gt; [8] --&gt; []
coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167</pre>

<p><strong>Example 2:</strong></p>
Expand Down
Loading