Skip to content

A: new #787

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
merged 1 commit into from
May 6, 2020
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
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,21 @@ LeetCode Problems' Solutions

| # | Title | Solution | Difficulty |
| :-: | - | - | :-: |
| <span id="1425">1425</span> | [Constrained Subset Sum](https://leetcode.com/problems/constrained-subset-sum "带限制的子序列和") | [Go](problems/constrained-subset-sum) | Hard |
| <span id="1439">1439</span> | [Find the Kth Smallest Sum of a Matrix With Sorted Rows](https://leetcode.com/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows "有序矩阵中的第 k 个最小数组和") | [Go](problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows) | Hard |
| <span id="1438">1438</span> | [Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit](https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit "绝对差不超过限制的最长连续子数组") | [Go](problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit) | Medium |
| <span id="1437">1437</span> | [Check If All 1's Are at Least Length K Places Away](https://leetcode.com/problems/check-if-all-1s-are-at-least-length-k-places-away "是否所有 1 都至少相隔 k 个元素") | [Go](problems/check-if-all-1s-are-at-least-length-k-places-away) | Medium |
| <span id="1436">1436</span> | [Destination City](https://leetcode.com/problems/destination-city "旅行终点站") | [Go](problems/destination-city) | Easy |
| <span id="1435">1435</span> | [Create a Session Bar Chart](https://leetcode.com/problems/create-a-session-bar-chart) 🔒 | [MySQL](problems/create-a-session-bar-chart) | Easy |
| <span id="1434">1434</span> | [Number of Ways to Wear Different Hats to Each Other](https://leetcode.com/problems/number-of-ways-to-wear-different-hats-to-each-other "每个人戴不同帽子的方案数") | [Go](problems/number-of-ways-to-wear-different-hats-to-each-other) | Hard |
| <span id="1433">1433</span> | [Check If a String Can Break Another String](https://leetcode.com/problems/check-if-a-string-can-break-another-string "检查一个字符串是否可以打破另一个字符串") | [Go](problems/check-if-a-string-can-break-another-string) | Medium |
| <span id="1432">1432</span> | [Max Difference You Can Get From Changing an Integer](https://leetcode.com/problems/max-difference-you-can-get-from-changing-an-integer "改变一个整数能得到的最大差值") | [Go](problems/max-difference-you-can-get-from-changing-an-integer) | Medium |
| <span id="1431">1431</span> | [Kids With the Greatest Number of Candies](https://leetcode.com/problems/kids-with-the-greatest-number-of-candies "拥有最多糖果的孩子") | [Go](problems/kids-with-the-greatest-number-of-candies) | Easy |
| <span id="1430">1430</span> | [Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree](https://leetcode.com/problems/check-if-a-string-is-a-valid-sequence-from-root-to-leaves-path-in-a-binary-tree) 🔒 | [Go](problems/check-if-a-string-is-a-valid-sequence-from-root-to-leaves-path-in-a-binary-tree) | Medium |
| <span id="1429">1429</span> | [First Unique Number](https://leetcode.com/problems/first-unique-number) 🔒 | [Go](problems/first-unique-number) | Medium |
| <span id="1428">1428</span> | [Leftmost Column with at Least a One](https://leetcode.com/problems/leftmost-column-with-at-least-a-one) 🔒 | [Go](problems/leftmost-column-with-at-least-a-one) | Medium |
| <span id="1427">1427</span> | [Perform String Shifts](https://leetcode.com/problems/perform-string-shifts) 🔒 | [Go](problems/perform-string-shifts) | Easy |
| <span id="1426">1426</span> | [Counting Elements](https://leetcode.com/problems/counting-elements "数元素") 🔒 | [Go](problems/counting-elements) | Easy |
| <span id="1425">1425</span> | [Constrained Subsequence Sum](https://leetcode.com/problems/constrained-subsequence-sum "带限制的子序列和") | [Go](problems/constrained-subsequence-sum) | Hard |
| <span id="1424">1424</span> | [Diagonal Traverse II](https://leetcode.com/problems/diagonal-traverse-ii "对角线遍历 II") | [Go](problems/diagonal-traverse-ii) | Medium |
| <span id="1423">1423</span> | [Maximum Points You Can Obtain from Cards](https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards "可获得的最大点数") | [Go](problems/maximum-points-you-can-obtain-from-cards) | Medium |
| <span id="1422">1422</span> | [Maximum Score After Splitting a String](https://leetcode.com/problems/maximum-score-after-splitting-a-string "分割字符串的最大得分") | [Go](problems/maximum-score-after-splitting-a-string) | Easy |
Expand Down
60 changes: 60 additions & 0 deletions problems/check-if-a-string-can-break-another-string/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
<!--+----------------------------------------------------------------------+-->
<!--|@author openset <[email protected]> |-->
<!--|@link https://github.com/openset |-->
<!--|@home https://github.com/openset/leetcode |-->
<!--+----------------------------------------------------------------------+-->

[< Previous](../max-difference-you-can-get-from-changing-an-integer "Max Difference You Can Get From Changing an Integer")

[Next >](../number-of-ways-to-wear-different-hats-to-each-other "Number of Ways to Wear Different Hats to Each Other")

## [1433. Check If a String Can Break Another String (Medium)](https://leetcode.com/problems/check-if-a-string-can-break-another-string "检查一个字符串是否可以打破另一个字符串")

<p>Given two strings: <code>s1</code> and <code>s2</code> with the same&nbsp;size, check if some&nbsp;permutation of string <code>s1</code> can break&nbsp;some&nbsp;permutation of string <code>s2</code> or vice-versa (in other words <code>s2</code> can break <code>s1</code>).</p>

<p>A string <code>x</code>&nbsp;can break&nbsp;string <code>y</code>&nbsp;(both of size <code>n</code>) if <code>x[i] &gt;= y[i]</code>&nbsp;(in alphabetical order)&nbsp;for all <code>i</code>&nbsp;between <code>0</code> and <code>n-1</code>.</p>

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

<pre>
<strong>Input:</strong> s1 = &quot;abc&quot;, s2 = &quot;xya&quot;
<strong>Output:</strong> true
<strong>Explanation:</strong> &quot;ayx&quot; is a permutation of s2=&quot;xya&quot; which can break to string &quot;abc&quot; which is a permutation of s1=&quot;abc&quot;.
</pre>

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

<pre>
<strong>Input:</strong> s1 = &quot;abe&quot;, s2 = &quot;acd&quot;
<strong>Output:</strong> false
<strong>Explanation:</strong> All permutations for s1=&quot;abe&quot; are: &quot;abe&quot;, &quot;aeb&quot;, &quot;bae&quot;, &quot;bea&quot;, &quot;eab&quot; and &quot;eba&quot; and all permutation for s2=&quot;acd&quot; are: &quot;acd&quot;, &quot;adc&quot;, &quot;cad&quot;, &quot;cda&quot;, &quot;dac&quot; and &quot;dca&quot;. However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
</pre>

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

<pre>
<strong>Input:</strong> s1 = &quot;leetcodee&quot;, s2 = &quot;interview&quot;
<strong>Output:</strong> true
</pre>

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

<ul>
<li><code>s1.length == n</code></li>
<li><code>s2.length == n</code></li>
<li><code>1 &lt;= n &lt;= 10^5</code></li>
<li>All strings consist of lowercase English letters.</li>
</ul>

### Related Topics
[[Greedy](../../tag/greedy/README.md)]
[[String](../../tag/string/README.md)]

### Hints
<details>
<summary>Hint 1</summary>
Sort both strings and then check if one of them can break the other.
</details>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
<!--+----------------------------------------------------------------------+-->
<!--|@author openset <[email protected]> |-->
<!--|@link https://github.com/openset |-->
<!--|@home https://github.com/openset/leetcode |-->
<!--+----------------------------------------------------------------------+-->

[< Previous](../first-unique-number "First Unique Number")

[Next >](../kids-with-the-greatest-number-of-candies "Kids With the Greatest Number of Candies")

## [1430. Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree (Medium)](https://leetcode.com/problems/check-if-a-string-is-a-valid-sequence-from-root-to-leaves-path-in-a-binary-tree "")



### Related Topics
[[Tree](../../tag/tree/README.md)]

### Hints
<details>
<summary>Hint 1</summary>
Depth-first search (DFS) with the parameters: current node in the binary tree and current position in the array of integers.
</details>

<details>
<summary>Hint 2</summary>
When reaching at final position check if it is a leaf node.
</details>
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
<!--+----------------------------------------------------------------------+-->
<!--|@author openset <[email protected]> |-->
<!--|@link https://github.com/openset |-->
<!--|@home https://github.com/openset/leetcode |-->
<!--+----------------------------------------------------------------------+-->

[< Previous](../destination-city "Destination City")

[Next >](../longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit "Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit")

## [1437. Check If All 1's Are at Least Length K Places Away (Medium)](https://leetcode.com/problems/check-if-all-1s-are-at-least-length-k-places-away "是否所有 1 都至少相隔 k 个元素")

<p>Given an array <code>nums</code> of 0s and 1s and an integer <code>k</code>, return <code>True</code> if all 1&#39;s are at least <code>k</code> places away from each other, otherwise return <code>False</code>.</p>

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

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/04/15/sample_1_1791.png" style="width: 214px; height: 90px;" /></strong></p>

<pre>
<strong>Input:</strong> nums = [1,0,0,0,1,0,0,1], k = 2
<strong>Output:</strong> true
<strong>Explanation:</strong> Each of the 1s are at least 2 places away from each other.
</pre>

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

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/04/15/sample_2_1791.png" style="width: 160px; height: 86px;" /></strong></p>

<pre>
<strong>Input:</strong> nums = [1,0,0,1,0,1], k = 2
<strong>Output:</strong> false
<strong>Explanation: </strong>The second 1 and third 1 are only one apart from each other.</pre>

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

<pre>
<strong>Input:</strong> nums = [1,1,1,1,1], k = 0
<strong>Output:</strong> true
</pre>

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

<pre>
<strong>Input:</strong> nums = [0,1,0,1], k = 1
<strong>Output:</strong> true
</pre>

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

<ul>
<li><code>1 &lt;= nums.length &lt;= 10^5</code></li>
<li><code>0 &lt;= k &lt;= nums.length</code></li>
<li><code>nums[i]</code>&nbsp;is&nbsp;<code>0</code>&nbsp;or&nbsp;<code>1</code></li>
</ul>

### Related Topics
[[Array](../../tag/array/README.md)]

### Hints
<details>
<summary>Hint 1</summary>
Each time you find a number 1, check whether or not it is K or more places away from the next one. If it's not, return false.
</details>
73 changes: 73 additions & 0 deletions problems/constrained-subsequence-sum/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
<!--+----------------------------------------------------------------------+-->
<!--|@author openset <[email protected]> |-->
<!--|@link https://github.com/openset |-->
<!--|@home https://github.com/openset/leetcode |-->
<!--+----------------------------------------------------------------------+-->

[< Previous](../diagonal-traverse-ii "Diagonal Traverse II")

[Next >](../counting-elements "Counting Elements")

## [1425. Constrained Subsequence Sum (Hard)](https://leetcode.com/problems/constrained-subsequence-sum "带限制的子序列和")

<p>Given an integer array&nbsp;<code>nums</code>&nbsp;and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence&nbsp;of that array such that for every&nbsp;two <strong>consecutive</strong> integers in the subsequence,&nbsp;<code>nums[i]</code>&nbsp;and&nbsp;<code>nums[j]</code>, where&nbsp;<code>i &lt; j</code>, the condition&nbsp;<code>j - i &lt;= k</code>&nbsp;is satisfied.</p>

<p>A&nbsp;<em>subsequence</em>&nbsp;of an array is&nbsp;obtained by deleting some number of elements (can be&nbsp;zero) from the array, leaving the remaining elements in their original order.</p>

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

<pre>
<strong>Input:</strong> nums = [10,2,-10,5,20], k = 2
<strong>Output:</strong> 37
<b>Explanation:</b> The subsequence is [10, 2, 5, 20].
</pre>

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

<pre>
<strong>Input:</strong> nums = [-1,-2,-3], k = 1
<strong>Output:</strong> -1
<b>Explanation:</b> The subsequence must be non-empty, so we choose the largest number.
</pre>

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

<pre>
<strong>Input:</strong> nums = [10,-2,-10,-5,20], k = 2
<strong>Output:</strong> 23
<b>Explanation:</b> The subsequence is [10, -2, -5, 20].
</pre>

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

<ul>
<li><code>1 &lt;= k &lt;= nums.length &lt;= 10^5</code></li>
<li><code>-10^4&nbsp;&lt;= nums[i] &lt;= 10^4</code></li>
</ul>

### Related Topics
[[Dynamic Programming](../../tag/dynamic-programming/README.md)]

### Hints
<details>
<summary>Hint 1</summary>
Use dynamic programming.
</details>

<details>
<summary>Hint 2</summary>
Let dp[i] be the solution for the prefix of the array that ends at index i, if the element at index i is in the subsequence.
</details>

<details>
<summary>Hint 3</summary>
dp[i] = nums[i] + max(0, dp[i-k], dp[i-k+1], ..., dp[i-1])
</details>

<details>
<summary>Hint 4</summary>
Use a heap with the sliding window technique to optimize the dp.
</details>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<p><em>(Recall that a binary search tree&nbsp;is a binary tree where for every <font face="monospace">node</font>, any descendant of <code>node.left</code> has a value <code>&lt;</code>&nbsp;<code>node.val</code>, and any descendant of <code>node.right</code> has a value <code>&gt;</code>&nbsp;<code>node.val</code>.&nbsp; Also recall that a preorder traversal&nbsp;displays the value of the&nbsp;<code>node</code> first, then traverses <code>node.left</code>, then traverses <code>node.right</code>.)</em></p>

<p>&nbsp;</p>
<p>It&#39;s guaranteed that for the given test cases there is always possible to find a binary search tree with the given requirements.</p>

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

Expand All @@ -26,13 +26,13 @@
</pre>

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

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

<ol>
<ul>
<li><code>1 &lt;= preorder.length &lt;= 100</code></li>
<li><code>1 &lt;= preorder[i]&nbsp;&lt;= 10^8</code></li>
<li>The values of <code>preorder</code> are distinct.</li>
</ol>
</ul>

### Related Topics
[[Tree](../../tag/tree/README.md)]
28 changes: 28 additions & 0 deletions problems/counting-elements/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
<!--+----------------------------------------------------------------------+-->
<!--|@author openset <[email protected]> |-->
<!--|@link https://github.com/openset |-->
<!--|@home https://github.com/openset/leetcode |-->
<!--+----------------------------------------------------------------------+-->

[< Previous](../constrained-subsequence-sum "Constrained Subsequence Sum")

[Next >](../perform-string-shifts "Perform String Shifts")

## [1426. Counting Elements (Easy)](https://leetcode.com/problems/counting-elements "数元素")



### Related Topics
[[Array](../../tag/array/README.md)]

### Hints
<details>
<summary>Hint 1</summary>
Use hashset to store all elements.
</details>

<details>
<summary>Hint 2</summary>
Loop again to count all valid elements.
</details>
14 changes: 14 additions & 0 deletions problems/create-a-session-bar-chart/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
<!--+----------------------------------------------------------------------+-->
<!--|@author openset <[email protected]> |-->
<!--|@link https://github.com/openset |-->
<!--|@home https://github.com/openset/leetcode |-->
<!--+----------------------------------------------------------------------+-->

[< Previous](../number-of-ways-to-wear-different-hats-to-each-other "Number of Ways to Wear Different Hats to Each Other")

[Next >](../destination-city "Destination City")

## [1435. Create a Session Bar Chart (Easy)](https://leetcode.com/problems/create-a-session-bar-chart "")


7 changes: 7 additions & 0 deletions problems/create-a-session-bar-chart/mysql_schemas.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Create table If Not Exists Sessions (session_id int, duration int);
Truncate table Sessions;
insert into Sessions (session_id, duration) values ('1', '30');
insert into Sessions (session_id, duration) values ('2', '199');
insert into Sessions (session_id, duration) values ('3', '299');
insert into Sessions (session_id, duration) values ('4', '580');
insert into Sessions (session_id, duration) values ('5', '1000');
Loading