Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add: new #765

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
awesee merged 1 commit into master from develop
Feb 17, 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
5 changes: 5 additions & 0 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ LeetCode Problems' Solutions

| # | Title | Solution | Difficulty |
| :-: | - | - | :-: |
| <span id="1354">1354</span> | [Construct Target Array With Multiple Sums](https://leetcode.com/problems/construct-target-array-with-multiple-sums "多次求和构造目标数组") | [Go](problems/construct-target-array-with-multiple-sums) | Hard |
| <span id="1353">1353</span> | [Maximum Number of Events That Can Be Attended](https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended "最多可以参加的会议数目") | [Go](problems/maximum-number-of-events-that-can-be-attended) | Medium |
| <span id="1352">1352</span> | [Product of the Last K Numbers](https://leetcode.com/problems/product-of-the-last-k-numbers "最后 K 个数的乘积") | [Go](problems/product-of-the-last-k-numbers) | Medium |
| <span id="1351">1351</span> | [Count Negative Numbers in a Sorted Matrix](https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix "统计有序矩阵中的负数") | [Go](problems/count-negative-numbers-in-a-sorted-matrix) | Easy |
| <span id="1350">1350</span> | [Students With Invalid Departments](https://leetcode.com/problems/students-with-invalid-departments) 🔒 | [MySQL](problems/students-with-invalid-departments) | Easy |
| <span id="1349">1349</span> | [Maximum Students Taking Exam](https://leetcode.com/problems/maximum-students-taking-exam "参加考试的最大学生数") | [Go](problems/maximum-students-taking-exam) | Hard |
| <span id="1348">1348</span> | [Tweet Counts Per Frequency](https://leetcode.com/problems/tweet-counts-per-frequency "推文计数") | [Go](problems/tweet-counts-per-frequency) | Medium |
| <span id="1347">1347</span> | [Minimum Number of Steps to Make Two Strings Anagram](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram "制造字母异位词的最小步骤数") | [Go](problems/minimum-number-of-steps-to-make-two-strings-anagram) | Medium |
Expand Down
73 changes: 73 additions & 0 deletions problems/construct-target-array-with-multiple-sums/README.md
View file Open in desktop
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 <openset.wang@gmail.com> |-->
<!--|@link https://github.com/openset |-->
<!--|@home https://github.com/openset/leetcode |-->
<!--+----------------------------------------------------------------------+-->

[< Previous](../maximum-number-of-events-that-can-be-attended "Maximum Number of Events That Can Be Attended")

Next >

## [1354. Construct Target Array With Multiple Sums (Hard)](https://leetcode.com/problems/construct-target-array-with-multiple-sums "多次求和构造目标数组")

<p>Given an array of integers&nbsp;<code>target</code>. From a starting array, <code>A</code>&nbsp;consisting of all 1&#39;s, you may perform the following procedure :</p>

<ul>
<li>let <code>x</code> be the sum of all elements currently in your array.</li>
<li>choose index <code>i</code>, such that&nbsp;<code>0 &lt;= i &lt; target.size</code> and set the value of <code>A</code> at index <code>i</code> to <code>x</code>.</li>
<li>You may repeat this procedure&nbsp;as many times as needed.</li>
</ul>

<p>Return True if it is possible to construct the <code>target</code> array from <code>A</code> otherwise&nbsp;return False.</p>

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

<pre>
<strong>Input:</strong> target = [9,3,5]
<strong>Output:</strong> true
<strong>Explanation:</strong> Start with [1, 1, 1]
[1, 1, 1], sum = 3 choose index 1
[1, 3, 1], sum = 5 choose index 2
[1, 3, 5], sum = 9 choose index 0
[9, 3, 5] Done
</pre>

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

<pre>
<strong>Input:</strong> target = [1,1,1,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Impossible to create target array from [1,1,1,1].
</pre>

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

<pre>
<strong>Input:</strong> target = [8,5]
<strong>Output:</strong> true
</pre>

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

<ul>
<li><code>N == target.length</code></li>
<li><code>1 &lt;= target.length&nbsp;&lt;= 5 * 10^4</code></li>
<li><code>1 &lt;= target[i] &lt;= 10^9</code></li>
</ul>

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

### Hints
<details>
<summary>Hint 1</summary>
Given that the sum is strictly increasing, the largest element in the target must be formed in the last step by adding the total sum in the previous step. Thus, we can simulate the process in a reversed way.
</details>

<details>
<summary>Hint 2</summary>
Subtract the largest with the rest of the array, and put the new element into the array. Repeat until all elements become one
</details>
66 changes: 66 additions & 0 deletions problems/count-negative-numbers-in-a-sorted-matrix/README.md
View file Open in desktop
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 <openset.wang@gmail.com> |-->
<!--|@link https://github.com/openset |-->
<!--|@home https://github.com/openset/leetcode |-->
<!--+----------------------------------------------------------------------+-->

[< Previous](../students-with-invalid-departments "Students With Invalid Departments")

[Next >](../product-of-the-last-k-numbers "Product of the Last K Numbers")

## [1351. Count Negative Numbers in a Sorted Matrix (Easy)](https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix "统计有序矩阵中的负数")

<p>Given a <code>m&nbsp;* n</code>&nbsp;matrix <code>grid</code>&nbsp;which is sorted in non-increasing order both row-wise and column-wise.&nbsp;</p>

<p>Return the number of <strong>negative</strong> numbers in&nbsp;<code>grid</code>.</p>

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

<pre>
<strong>Input:</strong> grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are 8 negatives number in the matrix.
</pre>

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

<pre>
<strong>Input:</strong> grid = [[3,2],[1,0]]
<strong>Output:</strong> 0
</pre>

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

<pre>
<strong>Input:</strong> grid = [[1,-1],[-1,-1]]
<strong>Output:</strong> 3
</pre>

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

<pre>
<strong>Input:</strong> grid = [[-1]]
<strong>Output:</strong> 1
</pre>

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

<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 &lt;= m, n &lt;= 100</code></li>
<li><code>-100 &lt;= grid[i][j] &lt;= 100</code></li>
</ul>

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

### Hints
<details>
<summary>Hint 1</summary>
Use binary search for optimization or simply brute force.
</details>
2 changes: 1 addition & 1 deletion problems/counting-bits/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
</ul>

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

### Similar Questions
1. [Number of 1 Bits](../number-of-1-bits) (Easy)
Expand Down
2 changes: 1 addition & 1 deletion problems/intersection-of-two-arrays/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
<p>&nbsp;</p>

### Related Topics
[[Sort](../../tag/sort/README.md)]
[[Hash Table](../../tag/hash-table/README.md)]
[[Two Pointers](../../tag/two-pointers/README.md)]
[[Binary Search](../../tag/binary-search/README.md)]
[[Sort](../../tag/sort/README.md)]

### Similar Questions
1. [Intersection of Two Arrays II](../intersection-of-two-arrays-ii) (Easy)
Expand Down
84 changes: 84 additions & 0 deletions problems/maximum-number-of-events-that-can-be-attended/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<!--|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](../product-of-the-last-k-numbers "Product of the Last K Numbers")

[Next >](../construct-target-array-with-multiple-sums "Construct Target Array With Multiple Sums")

## [1353. Maximum Number of Events That Can Be Attended (Medium)](https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended "最多可以参加的会议数目")

<p>Given an array of <code>events</code> where <code>events[i] = [startDay<sub>i</sub>, endDay<sub>i</sub>]</code>. Every event <code>i</code> starts at&nbsp;<code>startDay<sub>i</sub></code><sub>&nbsp;</sub>and ends at&nbsp;<code>endDay<sub>i</sub></code>.</p>

<p>You can attend an event <code>i</code>&nbsp;at any day&nbsp;<code>d</code> where&nbsp;<code>startTime<sub>i</sub>&nbsp;&lt;= d &lt;= endTime<sub>i</sub></code>. Notice that you can only attend one event at any time <code>d</code>.</p>

<p>Return <em>the maximum number of events&nbsp;</em>you can attend.</p>

<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/05/e1.png" style="width: 660px; height: 440px;" />
<pre>
<strong>Input:</strong> events = [[1,2],[2,3],[3,4]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can attend all the three events.
One way to attend them all is as shown.
Attend the first event on day 1.
Attend the second event on day 2.
Attend the third event on day 3.
</pre>

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

<pre>
<strong>Input:</strong> events= [[1,2],[2,3],[3,4],[1,2]]
<strong>Output:</strong> 4
</pre>

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

<pre>
<strong>Input:</strong> events = [[1,4],[4,4],[2,2],[3,4],[1,1]]
<strong>Output:</strong> 4
</pre>

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

<pre>
<strong>Input:</strong> events = [[1,100000]]
<strong>Output:</strong> 1
</pre>

<p><strong>Example 5:</strong></p>

<pre>
<strong>Input:</strong> events = [[1,1],[1,2],[1,3],[1,4],[1,5],[1,6],[1,7]]
<strong>Output:</strong> 7
</pre>

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

<ul>
<li><code>1 &lt;= events.length &lt;= 10^5</code></li>
<li><code>events[i].length == 2</code></li>
<li><code>1 &lt;= events[i][0] &lt;= events[i][1] &lt;= 10^5</code></li>
</ul>

### Related Topics
[[Greedy](../../tag/greedy/README.md)]
[[Sort](../../tag/sort/README.md)]
[[Segment Tree](../../tag/segment-tree/README.md)]

### Hints
<details>
<summary>Hint 1</summary>
Sort the events by the start time and in case of tie by the end time in ascending order.
</details>

<details>
<summary>Hint 2</summary>
Loop over the sorted events. Attend as much as you can and keep the last day occupied. When you try to attend new event keep in mind the first day you can attend a new event in.
</details>
2 changes: 1 addition & 1 deletion problems/maximum-students-taking-exam/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

[< Previous](../tweet-counts-per-frequency "Tweet Counts Per Frequency")

Next >
[Next >](../students-with-invalid-departments "Students With Invalid Departments")

## [1349. Maximum Students Taking Exam (Hard)](https://leetcode.com/problems/maximum-students-taking-exam "参加考试的最大学生数")

Expand Down
78 changes: 78 additions & 0 deletions problems/product-of-the-last-k-numbers/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<!--|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](../count-negative-numbers-in-a-sorted-matrix "Count Negative Numbers in a Sorted Matrix")

[Next >](../maximum-number-of-events-that-can-be-attended "Maximum Number of Events That Can Be Attended")

## [1352. Product of the Last K Numbers (Medium)](https://leetcode.com/problems/product-of-the-last-k-numbers "最后 K 个数的乘积")

<p>Implement the class <code>ProductOfNumbers</code>&nbsp;that supports two methods:</p>

<p>1.<code>&nbsp;add(int num)</code></p>

<ul>
<li>Adds the number <code>num</code> to the back of the current list of numbers.</li>
</ul>

<p>2.<code> getProduct(int k)</code></p>

<ul>
<li>Returns the product of the last <code>k</code> numbers in the current list.</li>
<li>You can assume that always the current list has <strong>at least</strong> <code>k</code> numbers.</li>
</ul>

<p>At any time, the product of any contiguous sequence of numbers will fit into a single 32-bit integer without overflowing.</p>

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

<pre>
<strong>Input</strong>
[&quot;ProductOfNumbers&quot;,&quot;add&quot;,&quot;add&quot;,&quot;add&quot;,&quot;add&quot;,&quot;add&quot;,&quot;getProduct&quot;,&quot;getProduct&quot;,&quot;getProduct&quot;,&quot;add&quot;,&quot;getProduct&quot;]
[[],[3],[0],[2],[5],[4],[2],[3],[4],[8],[2]]

<strong>Output</strong>
[null,null,null,null,null,null,20,40,0,null,32]

<strong>Explanation</strong>
ProductOfNumbers productOfNumbers = new ProductOfNumbers();
productOfNumbers.add(3); // [3]
productOfNumbers.add(0); // [3,0]
productOfNumbers.add(2); // [3,0,2]
productOfNumbers.add(5); // [3,0,2,5]
productOfNumbers.add(4); // [3,0,2,5,4]
productOfNumbers.getProduct(2); // return 20. The product of the last 2 numbers is 5 * 4 = 20
productOfNumbers.getProduct(3); // return 40. The product of the last 3 numbers is 2 * 5 * 4 = 40
productOfNumbers.getProduct(4); // return 0. The product of the last 4 numbers is 0 * 2 * 5 * 4 = 0
productOfNumbers.add(8); // [3,0,2,5,4,8]
productOfNumbers.getProduct(2); // return 32. The product of the last 2 numbers is 4 * 8 = 32
</pre>

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

<ul>
<li>There will be at most <code>40000</code>&nbsp;operations considering both <code>add</code> and <code>getProduct</code>.</li>
<li><code>0 &lt;= num&nbsp;&lt;=&nbsp;100</code></li>
<li><code>1 &lt;= k &lt;= 40000</code></li>
</ul>

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

### Hints
<details>
<summary>Hint 1</summary>
Keep all prefix products of numbers in an array, then calculate the product of last K elements in O(1) complexity.
</details>

<details>
<summary>Hint 2</summary>
When a zero number is added, clean the array of prefix products.
</details>
14 changes: 14 additions & 0 deletions problems/students-with-invalid-departments/README.md
View file Open in desktop
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 <openset.wang@gmail.com> |-->
<!--|@link https://github.com/openset |-->
<!--|@home https://github.com/openset/leetcode |-->
<!--+----------------------------------------------------------------------+-->

[< Previous](../maximum-students-taking-exam "Maximum Students Taking Exam")

[Next >](../count-negative-numbers-in-a-sorted-matrix "Count Negative Numbers in a Sorted Matrix")

## [1350. Students With Invalid Departments (Easy)](https://leetcode.com/problems/students-with-invalid-departments "")


17 changes: 17 additions & 0 deletions problems/students-with-invalid-departments/mysql_schemas.sql
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Create table If Not Exists Departments (id int, name varchar(30));
Create table If Not Exists Students (id int, name varchar(30), department_id int);
Truncate table Departments;
insert into Departments (id, name) values ('1', 'Electrical Engineering');
insert into Departments (id, name) values ('7', 'Computer Engineering');
insert into Departments (id, name) values ('13', 'Bussiness Administration');
Truncate table Students;
insert into Students (id, name, department_id) values ('23', 'Alice', '1');
insert into Students (id, name, department_id) values ('1', 'Bob', '7');
insert into Students (id, name, department_id) values ('5', 'Jennifer', '13');
insert into Students (id, name, department_id) values ('2', 'John', '14');
insert into Students (id, name, department_id) values ('4', 'Jasmine', '77');
insert into Students (id, name, department_id) values ('3', 'Steve', '74');
insert into Students (id, name, department_id) values ('6', 'Luis', '1');
insert into Students (id, name, department_id) values ('8', 'Jonathan', '7');
insert into Students (id, name, department_id) values ('7', 'Daiana', '33');
insert into Students (id, name, department_id) values ('11', 'Madelynn', '1');
2 changes: 2 additions & 0 deletions tag/array/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

| # | 题目 | 标签 | 难度 |
| :-: | - | - | :-: |
| 1352 | [最后 K 个数的乘积](../../problems/product-of-the-last-k-numbers) | [[设计](../design/README.md)] [[数组](../array/README.md)] | Medium |
| 1351 | [统计有序矩阵中的负数](../../problems/count-negative-numbers-in-a-sorted-matrix) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] | Easy |
| 1346 | [检查整数及其两倍数是否存在](../../problems/check-if-n-and-its-double-exist) | [[数组](../array/README.md)] | Easy |
| 1343 | [大小为 K 且平均值大于等于阈值的子数组数目](../../problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold) | [[数组](../array/README.md)] | Medium |
| 1338 | [数组大小减半](../../problems/reduce-array-size-to-the-half) | [[贪心算法](../greedy/README.md)] [[数组](../array/README.md)] | Medium |
Expand Down
1 change: 1 addition & 0 deletions tag/binary-search/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

| # | 题目 | 标签 | 难度 |
| :-: | - | - | :-: |
| 1351 | [统计有序矩阵中的负数](../../problems/count-negative-numbers-in-a-sorted-matrix) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] | Easy |
| 1337 | [方阵中战斗力最弱的 K 行](../../problems/the-k-weakest-rows-in-a-matrix) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] | Easy |
| 1300 | [转变数组后最接近目标值的数组和](../../problems/sum-of-mutated-array-closest-to-target) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] | Medium |
| 1292 | [元素和小于等于阈值的正方形的最大边长](../../problems/maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] | Medium |
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /