-
Notifications
You must be signed in to change notification settings - Fork 85
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
Showing
41 changed files
with
1,199 additions
and
16 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
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,25 @@ | ||
<div><p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> | ||
|
||
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/queens.jpg" style="width: 600px; height: 268px;"> | ||
<pre><strong>Input:</strong> n = 4 | ||
<strong>Output:</strong> 2 | ||
<strong>Explanation:</strong> There are two distinct solutions to the 4-queens puzzle as shown. | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
|
||
<pre><strong>Input:</strong> n = 1 | ||
<strong>Output:</strong> 1 | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= n <= 9</code></li> | ||
</ul> | ||
</div> |
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,44 @@ | ||
<div><p>Given an integer array <code>nums</code>, find the contiguous subarray (containing at least one number) which has the largest sum and return <em>its sum</em>.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
|
||
<pre><strong>Input:</strong> nums = [-2,1,-3,4,-1,2,1,-5,4] | ||
<strong>Output:</strong> 6 | ||
<strong>Explanation:</strong> [4,-1,2,1] has the largest sum = 6. | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
|
||
<pre><strong>Input:</strong> nums = [1] | ||
<strong>Output:</strong> 1 | ||
</pre> | ||
|
||
<p><strong>Example 3:</strong></p> | ||
|
||
<pre><strong>Input:</strong> nums = [0] | ||
<strong>Output:</strong> 0 | ||
</pre> | ||
|
||
<p><strong>Example 4:</strong></p> | ||
|
||
<pre><strong>Input:</strong> nums = [-1] | ||
<strong>Output:</strong> -1 | ||
</pre> | ||
|
||
<p><strong>Example 5:</strong></p> | ||
|
||
<pre><strong>Input:</strong> nums = [-100000] | ||
<strong>Output:</strong> -100000 | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li> | ||
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li> | ||
</ul> | ||
|
||
<p> </p> | ||
<strong>Follow up:</strong> If you have figured out the <code>O(n)</code> solution, try coding another solution using the <strong>divide and conquer</strong> approach, which is more subtle.</div> |
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,25 @@ | ||
<div><p>Given an <code>m x n</code> <code>matrix</code>, return <em>all elements of the</em> <code>matrix</code> <em>in spiral order</em>.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/spiral1.jpg" style="width: 242px; height: 242px;"> | ||
<pre><strong>Input:</strong> matrix = [[1,2,3],[4,5,6],[7,8,9]] | ||
<strong>Output:</strong> [1,2,3,6,9,8,7,4,5] | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/spiral.jpg" style="width: 322px; height: 242px;"> | ||
<pre><strong>Input:</strong> matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] | ||
<strong>Output:</strong> [1,2,3,4,8,12,11,10,9,5,6,7] | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>m == matrix.length</code></li> | ||
<li><code>n == matrix[i].length</code></li> | ||
<li><code>1 <= m, n <= 10</code></li> | ||
<li><code>-100 <= matrix[i][j] <= 100</code></li> | ||
</ul> | ||
</div> |
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,29 @@ | ||
<div><p>Given an array of non-negative integers <code>nums</code>, you are initially positioned at the <strong>first index</strong> of the array.</p> | ||
|
||
<p>Each element in the array represents your maximum jump length at that position.</p> | ||
|
||
<p>Determine if you are able to reach the last index.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
|
||
<pre><strong>Input:</strong> nums = [2,3,1,1,4] | ||
<strong>Output:</strong> true | ||
<strong>Explanation:</strong> Jump 1 step from index 0 to 1, then 3 steps to the last index. | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
|
||
<pre><strong>Input:</strong> nums = [3,2,1,0,4] | ||
<strong>Output:</strong> false | ||
<strong>Explanation:</strong> You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index. | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li> | ||
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li> | ||
</ul> | ||
</div> |
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,26 @@ | ||
<div><p>Given an array of <code>intervals</code> where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>, merge all overlapping intervals, and return <em>an array of the non-overlapping intervals that cover all the intervals in the input</em>.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
|
||
<pre><strong>Input:</strong> intervals = [[1,3],[2,6],[8,10],[15,18]] | ||
<strong>Output:</strong> [[1,6],[8,10],[15,18]] | ||
<strong>Explanation:</strong> Since intervals [1,3] and [2,6] overlaps, merge them into [1,6]. | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
|
||
<pre><strong>Input:</strong> intervals = [[1,4],[4,5]] | ||
<strong>Output:</strong> [[1,5]] | ||
<strong>Explanation:</strong> Intervals [1,4] and [4,5] are considered overlapping. | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= intervals.length <= 10<sup>4</sup></code></li> | ||
<li><code>intervals[i].length == 2</code></li> | ||
<li><code>0 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>4</sup></code></li> | ||
</ul> | ||
</div> |
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,47 @@ | ||
<div><p>Given a set of <em>non-overlapping</em> intervals, insert a new interval into the intervals (merge if necessary).</p> | ||
|
||
<p>You may assume that the intervals were initially sorted according to their start times.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
|
||
<pre><strong>Input:</strong> intervals = [[1,3],[6,9]], newInterval = [2,5] | ||
<strong>Output:</strong> [[1,5],[6,9]] | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
|
||
<pre><strong>Input:</strong> intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8] | ||
<strong>Output:</strong> [[1,2],[3,10],[12,16]] | ||
<strong>Explanation:</strong> Because the new interval <code>[4,8]</code> overlaps with <code>[3,5],[6,7],[8,10]</code>.</pre> | ||
|
||
<p><strong>Example 3:</strong></p> | ||
|
||
<pre><strong>Input:</strong> intervals = [], newInterval = [5,7] | ||
<strong>Output:</strong> [[5,7]] | ||
</pre> | ||
|
||
<p><strong>Example 4:</strong></p> | ||
|
||
<pre><strong>Input:</strong> intervals = [[1,5]], newInterval = [2,3] | ||
<strong>Output:</strong> [[1,5]] | ||
</pre> | ||
|
||
<p><strong>Example 5:</strong></p> | ||
|
||
<pre><strong>Input:</strong> intervals = [[1,5]], newInterval = [2,7] | ||
<strong>Output:</strong> [[1,7]] | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>0 <= intervals.length <= 10<sup>4</sup></code></li> | ||
<li><code>intervals[i].length == 2</code></li> | ||
<li><code>0 <= intervals[i][0] <= intervals[i][1] <= 10<sup>5</sup></code></li> | ||
<li><code>intervals</code> is sorted by <code>intervals[i][0]</code> in <strong>ascending</strong> order.</li> | ||
<li><code>newInterval.length == 2</code></li> | ||
<li><code>0 <= newInterval[0] <= newInterval[1] <= 10<sup>5</sup></code></li> | ||
</ul> | ||
</div> |
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,20 @@ | ||
<div><p>Given a string <code>s</code> consists of some words separated by spaces, return <em>the length of the last word in the string. If the last word does not exist, return </em><code>0</code>.</p> | ||
|
||
<p>A <strong>word</strong> is a maximal substring consisting of non-space characters only.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
<pre><strong>Input:</strong> s = "Hello World" | ||
<strong>Output:</strong> 5 | ||
</pre><p><strong>Example 2:</strong></p> | ||
<pre><strong>Input:</strong> s = " " | ||
<strong>Output:</strong> 0 | ||
</pre> | ||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= s.length <= 10<sup>4</sup></code></li> | ||
<li><code>s</code> consists of only English letters and spaces <code>' '</code>.</li> | ||
</ul> | ||
</div> |
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,22 @@ | ||
<div><p>Given a positive integer <code>n</code>, generate an <code>n x n</code> <code>matrix</code> filled with elements from <code>1</code> to <code>n<sup>2</sup></code> in spiral order.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/spiraln.jpg" style="width: 242px; height: 242px;"> | ||
<pre><strong>Input:</strong> n = 3 | ||
<strong>Output:</strong> [[1,2,3],[8,9,4],[7,6,5]] | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
|
||
<pre><strong>Input:</strong> n = 1 | ||
<strong>Output:</strong> [[1]] | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= n <= 20</code></li> | ||
</ul> | ||
</div> |
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,34 @@ | ||
<div><p>The set <code>[1, 2, 3, ..., n]</code> contains a total of <code>n!</code> unique permutations.</p> | ||
|
||
<p>By listing and labeling all of the permutations in order, we get the following sequence for <code>n = 3</code>:</p> | ||
|
||
<ol> | ||
<li><code>"123"</code></li> | ||
<li><code>"132"</code></li> | ||
<li><code>"213"</code></li> | ||
<li><code>"231"</code></li> | ||
<li><code>"312"</code></li> | ||
<li><code>"321"</code></li> | ||
</ol> | ||
|
||
<p>Given <code>n</code> and <code>k</code>, return the <code>k<sup>th</sup></code> permutation sequence.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
<pre><strong>Input:</strong> n = 3, k = 3 | ||
<strong>Output:</strong> "213" | ||
</pre><p><strong>Example 2:</strong></p> | ||
<pre><strong>Input:</strong> n = 4, k = 9 | ||
<strong>Output:</strong> "2314" | ||
</pre><p><strong>Example 3:</strong></p> | ||
<pre><strong>Input:</strong> n = 3, k = 1 | ||
<strong>Output:</strong> "123" | ||
</pre> | ||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= n <= 9</code></li> | ||
<li><code>1 <= k <= n!</code></li> | ||
</ul> | ||
</div> |
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,24 @@ | ||
<div><p>Given the <code>head</code> of a linked list, rotate the list to the right by <code>k</code> places.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg" style="width: 600px; height: 254px;"> | ||
<pre><strong>Input:</strong> head = [1,2,3,4,5], k = 2 | ||
<strong>Output:</strong> [4,5,1,2,3] | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/roate2.jpg" style="width: 472px; height: 542px;"> | ||
<pre><strong>Input:</strong> head = [0,1,2], k = 4 | ||
<strong>Output:</strong> [2,0,1] | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li>The number of nodes in the list is in the range <code>[0, 500]</code>.</li> | ||
<li><code>-100 <= Node.val <= 100</code></li> | ||
<li><code>0 <= k <= 2 * 10<sup>9</sup></code></li> | ||
</ul> | ||
</div> |
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,44 @@ | ||
<div><p>A robot is located at the top-left corner of a <code>m x n</code> grid (marked 'Start' in the diagram below).</p> | ||
|
||
<p>The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).</p> | ||
|
||
<p>How many possible unique paths are there?</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
<img src="https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png" style="width: 400px; height: 183px;"> | ||
<pre><strong>Input:</strong> m = 3, n = 7 | ||
<strong>Output:</strong> 28 | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
|
||
<pre><strong>Input:</strong> m = 3, n = 2 | ||
<strong>Output:</strong> 3 | ||
<strong>Explanation:</strong> | ||
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner: | ||
1. Right -> Down -> Down | ||
2. Down -> Down -> Right | ||
3. Down -> Right -> Down | ||
</pre> | ||
|
||
<p><strong>Example 3:</strong></p> | ||
|
||
<pre><strong>Input:</strong> m = 7, n = 3 | ||
<strong>Output:</strong> 28 | ||
</pre> | ||
|
||
<p><strong>Example 4:</strong></p> | ||
|
||
<pre><strong>Input:</strong> m = 3, n = 3 | ||
<strong>Output:</strong> 6 | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= m, n <= 100</code></li> | ||
<li>It's guaranteed that the answer will be less than or equal to <code>2 * 10<sup>9</sup></code>.</li> | ||
</ul> | ||
</div> |
Oops, something went wrong.