-
Notifications
You must be signed in to change notification settings - Fork 130
Add: new #196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Add: new #196
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
56 changes: 56 additions & 0 deletions
problems/k-closest-points-to-origin/README.md
This file contains hidden or 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,56 @@ | ||
<!--|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 |--> | ||
<!--+----------------------------------------------------------------------+--> | ||
|
||
## 973. K Closest Points to Origin (Easy) | ||
|
||
<p>We have a list of <code>points</code> on the plane. Find the <code>K</code> closest points to the origin <code>(0, 0)</code>.</p> | ||
|
||
<p>(Here, the distance between two points on a plane is the Euclidean distance.)</p> | ||
|
||
<p>You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)</p> | ||
|
||
<p> </p> | ||
|
||
<div> | ||
<p><strong>Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input: </strong>points = <span id="example-input-1-1">[[1,3],[-2,2]]</span>, K = <span id="example-input-1-2">1</span> | ||
<strong>Output: </strong><span id="example-output-1">[[-2,2]]</span> | ||
<strong>Explanation: </strong> | ||
The distance between (1, 3) and the origin is sqrt(10). | ||
The distance between (-2, 2) and the origin is sqrt(8). | ||
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin. | ||
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]]. | ||
</pre> | ||
|
||
<div> | ||
<p><strong>Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input: </strong>points = <span id="example-input-2-1">[[3,3],[5,-1],[-2,4]]</span>, K = <span id="example-input-2-2">2</span> | ||
<strong>Output: </strong><span id="example-output-2">[[3,3],[-2,4]]</span> | ||
(The answer [[-2,4],[3,3]] would also be accepted.) | ||
</pre> | ||
|
||
<p> </p> | ||
|
||
<p><strong>Note:</strong></p> | ||
|
||
<ol> | ||
<li><code>1 <= K <= points.length <= 10000</code></li> | ||
<li><code>-10000 < points[i][0] < 10000</code></li> | ||
<li><code>-10000 < points[i][1] < 10000</code></li> | ||
</ol> | ||
</div> | ||
</div> | ||
|
||
|
||
### Related Topics | ||
[[Sort](https://github.com/openset/leetcode/tree/master/tag/sort/README.md)] | ||
[[Math](https://github.com/openset/leetcode/tree/master/tag/math/README.md)] | ||
[[Divide and Conquer](https://github.com/openset/leetcode/tree/master/tag/divide-and-conquer/README.md)] |
67 changes: 67 additions & 0 deletions
problems/largest-perimeter-triangle/README.md
This file contains hidden or 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,67 @@ | ||
<!--|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 |--> | ||
<!--+----------------------------------------------------------------------+--> | ||
|
||
## 976. Largest Perimeter Triangle (Easy) | ||
|
||
<p>Given an array <code>A</code> of positive lengths, return the largest perimeter of a triangle with <strong>non-zero area</strong>, formed from 3 of these lengths.</p> | ||
|
||
<p>If it is impossible to form any triangle of non-zero area, return <code>0</code>.</p> | ||
|
||
<p> </p> | ||
|
||
<ol> | ||
</ol> | ||
|
||
<div> | ||
<p><strong>Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input: </strong><span id="example-input-1-1">[2,1,2]</span> | ||
<strong>Output: </strong><span id="example-output-1">5</span> | ||
</pre> | ||
|
||
<div> | ||
<p><strong>Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input: </strong><span id="example-input-2-1">[1,2,1]</span> | ||
<strong>Output: </strong><span id="example-output-2">0</span> | ||
</pre> | ||
|
||
<div> | ||
<p><strong>Example 3:</strong></p> | ||
|
||
<pre> | ||
<strong>Input: </strong><span id="example-input-3-1">[3,2,3,4]</span> | ||
<strong>Output: </strong><span id="example-output-3">10</span> | ||
</pre> | ||
|
||
<div> | ||
<p><strong>Example 4:</strong></p> | ||
|
||
<pre> | ||
<strong>Input: </strong><span id="example-input-4-1">[3,6,2,3]</span> | ||
<strong>Output: </strong><span id="example-output-4">8</span> | ||
</pre> | ||
|
||
<p> </p> | ||
|
||
<p><strong>Note:</strong></p> | ||
|
||
<ol> | ||
<li><code>3 <= A.length <= 10000</code></li> | ||
<li><code>1 <= A[i] <= 10^6</code></li> | ||
</ol> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
|
||
### Related Topics | ||
[[Array](https://github.com/openset/leetcode/tree/master/tag/array/README.md)] | ||
[[Math](https://github.com/openset/leetcode/tree/master/tag/math/README.md)] |
88 changes: 88 additions & 0 deletions
problems/odd-even-jump/README.md
This file contains hidden or 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,88 @@ | ||
<!--|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 |--> | ||
<!--+----------------------------------------------------------------------+--> | ||
|
||
## 975. Odd Even Jump (Hard) | ||
|
||
<p>You are given an integer array <code>A</code>. From some starting index, you can make a series of jumps. The (1st, 3rd, 5th, ...) jumps in the series are called <em>odd numbered jumps</em>, and the (2nd, 4th, 6th, ...) jumps in the series are called <em>even numbered jumps</em>.</p> | ||
|
||
<p>You may from index <code>i</code> jump forward to index <code><font face="monospace">j</font></code> (with <code>i < j</code>) in the following way:</p> | ||
|
||
<ul> | ||
<li>During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index <font face="monospace">j</font> such that <code>A[i] <= A[j]</code> and <code>A[j]</code> is the smallest possible value. If there are multiple such indexes <code><font face="monospace">j</font></code>, you can only jump to the <strong>smallest</strong> such index <code><font face="monospace">j</font></code>.</li> | ||
<li>During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index <font face="monospace">j</font> such that <code>A[i] >= A[j]</code> and <code>A[j]</code> is the largest possible value. If there are multiple such indexes <code><font face="monospace">j</font></code>, you can only jump to the <strong>smallest</strong> such index <code><font face="monospace">j</font></code>.</li> | ||
<li>(It may be the case that for some index <code><font face="monospace">i</font>,</code> there are no legal jumps.)</li> | ||
</ul> | ||
|
||
<p>A starting index is <em>good</em> if, starting from that index, you can reach the end of the array (index <code>A.length - 1</code>) by jumping some number of times (possibly 0 or more than once.)</p> | ||
|
||
<p>Return the number of good starting indexes.</p> | ||
|
||
<p> </p> | ||
|
||
<p><strong>Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input: </strong><span id="example-input-1-1">[10,13,12,14,15]</span> | ||
<strong>Output: </strong><span id="example-output-1">2</span> | ||
<strong>Explanation: </strong> | ||
From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. | ||
From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. | ||
From starting index i = 3, we can jump to i = 4, so we've reached the end. | ||
From starting index i = 4, we've reached the end already. | ||
In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. | ||
</pre> | ||
|
||
<div> | ||
<p><strong>Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input: </strong><span id="example-input-2-1">[2,3,1,1,4]</span> | ||
<strong>Output: </strong><span id="example-output-2">3</span> | ||
<strong>Explanation: </strong> | ||
From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: | ||
|
||
During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. | ||
|
||
During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. | ||
|
||
During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. | ||
|
||
We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. | ||
|
||
In a similar manner, we can deduce that: | ||
From starting index i = 1, we jump to i = 4, so we reach the end. | ||
From starting index i = 2, we jump to i = 3, and then we can't jump anymore. | ||
From starting index i = 3, we jump to i = 4, so we reach the end. | ||
From starting index i = 4, we are already at the end. | ||
In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. | ||
</pre> | ||
|
||
<div> | ||
<p><strong>Example 3:</strong></p> | ||
|
||
<pre> | ||
<strong>Input: </strong><span id="example-input-3-1">[5,1,3,4,2]</span> | ||
<strong>Output: </strong><span id="example-output-3">3</span> | ||
<strong>Explanation: </strong> | ||
We can reach the end from starting indexes 1, 2, and 4. | ||
</pre> | ||
</div> | ||
</div> | ||
|
||
<p> </p> | ||
|
||
<p><strong>Note:</strong></p> | ||
|
||
<ol> | ||
<li><code>1 <= A.length <= 20000</code></li> | ||
<li><code>0 <= A[i] < 100000</code></li> | ||
</ol> | ||
|
||
|
||
### Related Topics | ||
[[Stack](https://github.com/openset/leetcode/tree/master/tag/stack/README.md)] | ||
[[Dynamic Programming](https://github.com/openset/leetcode/tree/master/tag/dynamic-programming/README.md)] |
37 changes: 37 additions & 0 deletions
problems/subarray-sums-divisible-by-k/README.md
This file contains hidden or 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,37 @@ | ||
<!--|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 |--> | ||
<!--+----------------------------------------------------------------------+--> | ||
|
||
## 974. Subarray Sums Divisible by K (Medium) | ||
|
||
<p>Given an array <code>A</code> of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by <code>K</code>.</p> | ||
|
||
<p> </p> | ||
|
||
<div> | ||
<p><strong>Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input: </strong>A = <span id="example-input-1-1">[4,5,0,-2,-3,1]</span>, K = <span id="example-input-1-2">5</span> | ||
<strong>Output: </strong><span id="example-output-1">7</span> | ||
<strong>Explanation: </strong>There are 7 subarrays with a sum divisible by K = 5: | ||
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3] | ||
</pre> | ||
|
||
<p> </p> | ||
|
||
<p><strong>Note:</strong></p> | ||
|
||
<ol> | ||
<li><code>1 <= A.length <= 30000</code></li> | ||
<li><code>-10000 <= A[i] <= 10000</code></li> | ||
<li><code>2 <= K <= 10000</code></li> | ||
</ol> | ||
</div> | ||
|
||
|
||
### Related Topics | ||
[[Array](https://github.com/openset/leetcode/tree/master/tag/array/README.md)] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.