From 9544656151e03e9d3f5fffe8b9ea14a2beb9cf3d Mon Sep 17 00:00:00 2001
From: openset
-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:
-
-Copy All
: You can copy all the characters present on the notepad (partial copy is not allowed).Paste
: You can paste the characters which are copied last time.Copy All
: You can copy all the characters present on the notepad (partial copy is not allowed).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:
n
will be in the range [1, 1000].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:
+-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:
+### 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:
+ +
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].
-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:
+
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
.grid[i][j]
is an integer in the set {-1, 0, 1}
.+ +
Note:
+ +grid
is an N
by N
2D array, with 1 <= N <= 50
.grid[i][j]
is an integer in the set {-1, 0, 1}
.+
-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.
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?
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.
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: -
-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:
+ +