From 9544656151e03e9d3f5fffe8b9ea14a2beb9cf3d Mon Sep 17 00:00:00 2001 From: openset Date: Wed, 27 Feb 2019 10:03:25 +0800 Subject: [PATCH] Update: description --- problems/2-keys-keyboard/README.md | 34 +++++----- .../README.md | 44 +++++++++---- problems/beautiful-arrangement/README.md | 47 ++++++++------ problems/big-countries/README.md | 20 +++--- problems/bulb-switcher-ii/README.md | 37 ++++++----- problems/cherry-pickup/README.md | 63 ++++++++++++------- .../classes-more-than-5-students/README.md | 29 +++++---- problems/combination-sum-iv/README.md | 15 +++-- .../consecutive-available-seats/README.md | 20 +++--- .../copy-list-with-random-pointer/README.md | 31 +++++++-- 10 files changed, 208 insertions(+), 132 deletions(-) diff --git a/problems/2-keys-keyboard/README.md b/problems/2-keys-keyboard/README.md index a8fdc876d..0b4fa1a46 100644 --- a/problems/2-keys-keyboard/README.md +++ b/problems/2-keys-keyboard/README.md @@ -11,36 +11,38 @@ ## 650. 2 Keys Keyboard (Medium) -

-Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step: +

Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step:

+
    -
  1. Copy All: You can copy all the characters present on the notepad (partial copy is not allowed).
  2. -
  3. Paste: You can paste the characters which are copied last time.
  4. +
  5. Copy All: You can copy all the characters present on the notepad (partial copy is not allowed).
  6. +
  7. Paste: You can paste the characters which are copied last time.
-

-

-Given a number n. You have to get exactly n 'A' on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get n 'A'. -

+

 

+ +

Given a number n. You have to get exactly n 'A' on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get n 'A'.

+ +

Example 1:

-

Example 1:

 Input: 3
 Output: 3
 Explanation:
-Intitally, we have one character 'A'.
+Intitally, we have one character 'A'.
 In step 1, we use Copy All operation.
-In step 2, we use Paste operation to get 'AA'.
-In step 3, we use Paste operation to get 'AAA'.
+In step 2, we use Paste operation to get 'AA'.
+In step 3, we use Paste operation to get 'AAA'.
 
-

+

 

+ +

Note:

-

Note:

    -
  1. The n will be in the range [1, 1000].
  2. +
  3. The n will be in the range [1, 1000].
-

+ +

 

### Related Topics [[Dynamic Programming](https://github.com/openset/leetcode/tree/master/tag/dynamic-programming/README.md)] diff --git a/problems/average-salary-departments-vs-company/README.md b/problems/average-salary-departments-vs-company/README.md index b37e97278..27f032ef9 100644 --- a/problems/average-salary-departments-vs-company/README.md +++ b/problems/average-salary-departments-vs-company/README.md @@ -11,9 +11,10 @@ ## 615. Average Salary: Departments VS Company (Hard) -Given two tables as below, write a query to display the comparison result (higher/lower/same) of the average salary of employees in a department to the company's average salary.

- +Given two tables as below, write a query to display the comparison result (higher/lower/same) of the average salary of employees in a department to the company's average salary. +

 

Table: salary +
 | id | employee_id | amount | pay_date   |
 |----|-------------|--------|------------|
@@ -23,18 +24,26 @@ Table: salary
 | 4  | 1           | 7000   | 2017-02-28 |
 | 5  | 2           | 6000   | 2017-02-28 |
 | 6  | 3           | 8000   | 2017-02-28 |
-

+ + +

 

+The employee_id column refers to the employee_id in the following table employee. + +

 

-The employee_id column refers to the employee_id in the following table employee.

 | employee_id | department_id |
 |-------------|---------------|
 | 1           | 1             |
 | 2           | 2             |
 | 3           | 2             |
-

+ + +

 

+So for the sample data above, the result is: + +

 

-So for the sample data above, the result is:

 | pay_month | department_id | comparison  |
 |-----------|---------------|-------------|
@@ -42,10 +51,21 @@ So for the sample data above, the result is:

| 2017-03 | 2 | lower | | 2017-02 | 1 | same | | 2017-02 | 2 | same | -

+ + +

 

+Explanation + +

 

+In March, the company's average salary is (9000+6000+10000)/3 = 8333.33... + +

 

+The average salary for department '1' is 9000, which is the salary of employee_id '1' since there is only one employee in this department. So the comparison result is 'higher' since 9000 > 8333.33 obviously. + +

 

+The average salary of department '2' is (6000 + 10000)/2 = 8000, which is the average of employee_id '2' and '3'. So the comparison result is 'lower' since 8000 < 8333.33. + +

 

+With he same formula for the average salary comparison in February, the result is 'same' since both the department '1' and '2' have the same average salary with the company, which is 7000. -Explanation

-In March, the company's average salary is (9000+6000+10000)/3 = 8333.33...

-The average salary for department '1' is 9000, which is the salary of employee_id '1' since there is only one employee in this department. So the comparison result is 'higher' since 9000 > 8333.33 obviously.

-The average salary of department '2' is (6000 + 10000)/2 = 8000, which is the average of employee_id '2' and '3'. So the comparison result is 'lower' since 8000 < 8333.33.

-With he same formula for the average salary comparison in February, the result is 'same' since both the department '1' and '2' have the same average salary with the company, which is 7000.

+

 

diff --git a/problems/beautiful-arrangement/README.md b/problems/beautiful-arrangement/README.md index 72a3954ae..20e56f7c3 100644 --- a/problems/beautiful-arrangement/README.md +++ b/problems/beautiful-arrangement/README.md @@ -11,37 +11,46 @@ ## 526. Beautiful Arrangement (Medium) -

-Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is constructed by these N numbers successfully if one of the following is true for the ith position (1 <= i <= N) in this array: +

Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is constructed by these N numbers successfully if one of the following is true for the ith position (1 <= i <= N) in this array:

+
    -
  1. The number at the ith position is divisible by i.
  2. -
  3. i is divisible by the number at the ith position.
  4. +
  5. The number at the ith position is divisible by i.
  6. +
  7. i is divisible by the number at the ith position.
-

-

-Now given N, how many beautiful arrangements can you construct? -

+

 

+ +

Now given N, how many beautiful arrangements can you construct?

+ +

Example 1:

-

Example 1:

 Input: 2
 Output: 2
 Explanation: 
-
The first beautiful arrangement is [1, 2]: -
Number at the 1st position (i=1) is 1, and 1 is divisible by i (i=1). -
Number at the 2nd position (i=2) is 2, and 2 is divisible by i (i=2). -
The second beautiful arrangement is [2, 1]: -
Number at the 1st position (i=1) is 2, and 2 is divisible by i (i=1). -
Number at the 2nd position (i=2) is 1, and i (i=2) is divisible by 1. + +The first beautiful arrangement is [1, 2]: + +Number at the 1st position (i=1) is 1, and 1 is divisible by i (i=1). + +Number at the 2nd position (i=2) is 2, and 2 is divisible by i (i=2). + +The second beautiful arrangement is [2, 1]: + +Number at the 1st position (i=1) is 2, and 2 is divisible by i (i=1). + +Number at the 2nd position (i=2) is 1, and i (i=2) is divisible by 1.
-

-

Note:
+

 

+ +

Note:

+
    -
  1. N is a positive integer and will not exceed 15.
  2. +
  3. N is a positive integer and will not exceed 15.
-

+ +

 

### Related Topics [[Backtracking](https://github.com/openset/leetcode/tree/master/tag/backtracking/README.md)] diff --git a/problems/big-countries/README.md b/problems/big-countries/README.md index 8d14dfd06..946df783e 100644 --- a/problems/big-countries/README.md +++ b/problems/big-countries/README.md @@ -11,7 +11,8 @@ ## 595. Big Countries (Easy) -

There is a table World

+

There is a table World

+
 +-----------------+------------+------------+--------------+---------------+
 | name            | continent  | area       | population   | gdp           |
@@ -23,13 +24,13 @@
 | Angola          | Africa     | 1246700    | 20609294     | 100990000     |
 +-----------------+------------+------------+--------------+---------------+
 
-

-A country is big if it has an area of bigger than 3 million square km or a population of more than 25 million. -

-Write a SQL solution to output big countries' name, population and area. -

-

-For example, according to the above table, we should output: + +

A country is big if it has an area of bigger than 3 million square km or a population of more than 25 million.

+ +

Write a SQL solution to output big countries' name, population and area.

+ +

For example, according to the above table, we should output:

+
 +--------------+-------------+--------------+
 | name         | population  | area         |
@@ -38,4 +39,5 @@ For example, according to the above table, we should output:
 | Algeria      | 37100000    | 2381741      |
 +--------------+-------------+--------------+
 
-

+ +

 

diff --git a/problems/bulb-switcher-ii/README.md b/problems/bulb-switcher-ii/README.md index c251d94fb..10a4f3048 100644 --- a/problems/bulb-switcher-ii/README.md +++ b/problems/bulb-switcher-ii/README.md @@ -11,51 +11,50 @@ ## 672. Bulb Switcher II (Medium) -

-There is a room with n lights which are turned on initially and 4 buttons on the wall. After performing exactly m unknown operations towards buttons, you need to return how many different kinds of status of the n lights could be. -

+

There is a room with n lights which are turned on initially and 4 buttons on the wall. After performing exactly m unknown operations towards buttons, you need to return how many different kinds of status of the n lights could be.

-

-Suppose n lights are labeled as number [1, 2, 3 ..., n], function of these 4 buttons are given below: +

Suppose n lights are labeled as number [1, 2, 3 ..., n], function of these 4 buttons are given below:

    -
  1. Flip all the lights.
  2. -
  3. Flip lights with even numbers.
  4. -
  5. Flip lights with odd numbers.
  6. -
  7. Flip lights with (3k + 1) numbers, k = 0, 1, 2, ...
  8. +
  9. Flip all the lights.
  10. +
  11. Flip lights with even numbers.
  12. +
  13. Flip lights with odd numbers.
  14. +
  15. Flip lights with (3k + 1) numbers, k = 0, 1, 2, ...
-

+

 

+ +

Example 1:

-

Example 1:

 Input: n = 1, m = 1.
 Output: 2
 Explanation: Status can be: [on], [off]
 
-

+

 

+ +

Example 2:

-

Example 2:

 Input: n = 2, m = 1.
 Output: 3
 Explanation: Status can be: [on, off], [off, on], [off, off]
 
-

+

 

+ +

Example 3:

-

Example 3:

 Input: n = 3, m = 1.
 Output: 4
 Explanation: Status can be: [off, on, off], [on, off, on], [off, off, off], [off, on, on].
 
-

-

Note: -n and m both fit in range [0, 1000]. -

+

 

+ +

Note: n and m both fit in range [0, 1000].

### Related Topics [[Math](https://github.com/openset/leetcode/tree/master/tag/math/README.md)] diff --git a/problems/cherry-pickup/README.md b/problems/cherry-pickup/README.md index c26dc6e79..73e741500 100644 --- a/problems/cherry-pickup/README.md +++ b/problems/cherry-pickup/README.md @@ -11,22 +11,35 @@ ## 741. Cherry Pickup (Hard) -

-In a N x N grid representing a field of cherries, each cell is one of three possible integers. -

-

  • 0 means the cell is empty, so you can pass through;
  • -
  • 1 means the cell contains a cherry, that you can pick up and pass through;
  • -
  • -1 means the cell contains a thorn that blocks your way.
  • -

    -Your task is to collect maximum number of cherries possible by following the rules below: -

    -

  • Starting at the position (0, 0) and reaching (N-1, N-1) by moving right or down through valid path cells (cells with value 0 or 1);
  • -
  • After reaching (N-1, N-1), returning to (0, 0) by moving left or up through valid path cells;
  • -
  • When passing through a path cell containing a cherry, you pick it up and the cell becomes an empty cell (0);
  • -
  • If there is no valid path between (0, 0) and (N-1, N-1), then no cherries can be collected.
  • -

    - -

    Example 1:
    +

    In a N x N grid representing a field of cherries, each cell is one of three possible integers.

    + +

     

    + + + +

     

    + +

    Your task is to collect maximum number of cherries possible by following the rules below:

    + +

     

    + + + +

     

    + +

     

    + +

    Example 1:

    +
     Input: grid =
     [[0, 1, -1],
    @@ -39,13 +52,19 @@ The player started at (0, 0) and went down, down, right right to reach (2, 2).
     Then, the player went left, up, up, left to return home, picking up one more cherry.
     The total number of cherries picked up is 5, and this is the maximum possible.
     
    -

    -

    Note: -

  • grid is an N by N 2D array, with 1 <= N <= 50.
  • -
  • Each grid[i][j] is an integer in the set {-1, 0, 1}.
  • -
  • It is guaranteed that grid[0][0] and grid[N-1][N-1] are not -1.
  • -

    +

     

    + +

    Note:

    + + ### Related Topics [[Dynamic Programming](https://github.com/openset/leetcode/tree/master/tag/dynamic-programming/README.md)] diff --git a/problems/classes-more-than-5-students/README.md b/problems/classes-more-than-5-students/README.md index 261ee6651..ef058a4bd 100644 --- a/problems/classes-more-than-5-students/README.md +++ b/problems/classes-more-than-5-students/README.md @@ -11,14 +11,12 @@ ## 596. Classes More Than 5 Students (Easy) -

    -There is a table courses with columns: student and class -

    -Please list out all classes which have more than or equal to 5 students. -

    -

    -For example, the table: -

    +

    There is a table courses with columns: student and class

    + +

    Please list out all classes which have more than or equal to 5 students.

    + +

    For example, the table:

    +
     +---------+------------+
     | student | class      |
    @@ -34,8 +32,9 @@ For example, the table:
     | I       | Math       |
     +---------+------------+
     
    -

    -Should output: + +

    Should output:

    +
     +---------+
     | class   |
    @@ -43,8 +42,8 @@ Should output:
     | Math    |
     +---------+
     
    -

    -

    -Note:
    -The students should not be counted duplicate in each course. -

    + +

     

    + +

    Note:
    +The students should not be counted duplicate in each course.

    diff --git a/problems/combination-sum-iv/README.md b/problems/combination-sum-iv/README.md index c5c4e643f..5bf1eae44 100644 --- a/problems/combination-sum-iv/README.md +++ b/problems/combination-sum-iv/README.md @@ -11,9 +11,10 @@ ## 377. Combination Sum IV (Medium) -

    Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.

    +

    Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.

    + +

    Example:

    -

    Example:

     nums = [1, 2, 3]
     target = 4
    @@ -29,16 +30,18 @@ The possible combination ways are:
     
     Note that different sequences are counted as different combinations.
     
    -Therefore the output is 7.
    +Therefore the output is 7.
     
    -

    + +

     

    Follow up:
    What if negative numbers are allowed in the given array?
    How does it change the problem?
    -What limitation we need to add to the question to allow negative numbers?

    +What limitation we need to add to the question to allow negative numbers?

    -

    Credits:
    Special thanks to @pbrother for adding this problem and creating all test cases.

    +

    Credits:
    +Special thanks to @pbrother for adding this problem and creating all test cases.

    ### Related Topics [[Dynamic Programming](https://github.com/openset/leetcode/tree/master/tag/dynamic-programming/README.md)] diff --git a/problems/consecutive-available-seats/README.md b/problems/consecutive-available-seats/README.md index b9ed6d885..0a6ebda40 100644 --- a/problems/consecutive-available-seats/README.md +++ b/problems/consecutive-available-seats/README.md @@ -11,9 +11,8 @@ ## 603. Consecutive Available Seats (Easy) -Several friends at a cinema ticket office would like to reserve consecutive available seats.
    +Several friends at a cinema ticket office would like to reserve consecutive available seats.
    Can you help to query all the consecutive available seats order by the seat_id using the following cinema table? -
     | seat_id | free |
     |---------|------|
    @@ -22,9 +21,13 @@ Can you help to query all the consecutive available seats order by the seat_id u
     | 3       | 1    |
     | 4       | 1    |
     | 5       | 1    |
    -

    + + +

     

    +Your query should return the following result for the sample case above. + +

     

    -Your query should return the following result for the sample case above.

     | seat_id |
     |---------|
    @@ -32,8 +35,9 @@ Your query should return the following result for the sample case above.

    | 4 | | 5 |
    - Note: -
  • The seat_id is an auto increment int, and free is bool ('1' means free, and '0' means occupied.).
  • -
  • Consecutive available seats are more than 2(inclusive) seats consecutively available.
  • -

    + + diff --git a/problems/copy-list-with-random-pointer/README.md b/problems/copy-list-with-random-pointer/README.md index 6a1bf9ce6..b1f2268ce 100644 --- a/problems/copy-list-with-random-pointer/README.md +++ b/problems/copy-list-with-random-pointer/README.md @@ -11,13 +11,32 @@ ## 138. Copy List with Random Pointer (Medium) -

    -A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. -

    +

    A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.

    -

    -Return a deep copy of the list. -

    +

    Return a deep copy of the list.

    + +

     

    + +

    Example 1:

    + +

    + +
    +Input:
    +{"$id":"1","next":{"$id":"2","next":null,"random":{"$ref":"2"},"val":2},"random":{"$ref":"2"},"val":1}
    +
    +Explanation:
    +Node 1's value is 1, both of its next and random pointer points to Node 2.
    +Node 2's value is 2, its next pointer points to null and its random pointer points to itself.
    +
    + +

     

    + +

    Note:

    + +
      +
    1. You must return the copy of the given head as a reference to the cloned list.
    2. +
    ### Related Topics [[Hash Table](https://github.com/openset/leetcode/tree/master/tag/hash-table/README.md)]