XingXing Park

猩猩的乐园


  • Home

  • Archives

  • Tags

  • Job Info

  • Search

[Leetcode 1076] Project Employees II

Posted on 2020-08-01 | In leetcode | Comments:

原题说明

Table: Project

+————-+———+
| Column Name | Type |
+————-+———+
| project_id | int |
| employee_id | int |
+————-+———+
(project_id, employee_id) is the primary key of this table.
employee_id is a foreign key to Employee table.

Table: Employee

+——————+———+
| Column Name | Type |
+——————+———+
| employee_id | int |
| name | varchar |
| experience_years | int |
+——————+———+
employee_id is the primary key of this table.

 

Write an SQL query that reports all the projects that have the most employees.

The query result format is in the following example:

Project table:
+————-+————-+
| project_id | employee_id |
+————-+————-+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 4 |
+————-+————-+

Employee table:
+————-+——–+——————+
| employee_id | name | experience_years |
+————-+——–+——————+
| 1 | Khaled | 3 |
| 2 | Ali | 2 |
| 3 | John | 1 |
| 4 | Doe | 2 |
+————-+——–+——————+

Result table:
+————-+
| project_id |
+————-+
| 1 |
+————-+
The first project has 3 employees while the second one has 2.


Read more »

[Leetcode 1074] Number of Submatrices That Sum to Target

Posted on 2020-08-01 | In leetcode | Comments:

原题说明

Given a matrix, and a target, return the number of non-empty submatrices that sum to target.

A submatrix x1, y1, x2, y2 is the set of all cells matrix[x][y] with x1 <= x <= x2 and y1 <= y <= y2.

Two submatrices (x1, y1, x2, y2) and (x1’, y1’, x2’, y2’) are different if they have some coordinate that is different: for example, if x1 != x1’.

 

Example 1:

Input: matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0
Output: 4
Explanation: The four 1x1 submatrices that only contain 0.

Example 2:

Input: matrix = [[1,-1],[-1,1]], target = 0
Output: 5
Explanation: The two 1x2 submatrices, plus the two 2x1 submatrices, plus the 2x2 submatrix.

 

Note:

  1. 1 <= matrix.length <= 300
  2. 1 <= matrix[0].length <= 300
  3. -1000 <= matrix[i] <= 1000
  4. -10^8 <= target <= 10^8

Read more »

[Leetcode 1071] Greatest Common Divisor of Strings

Posted on 2020-07-26 | In leetcode | Comments:

原题说明

For strings S and T, we say “T divides S“ if and only if S = T + … + T  (T concatenated with itself 1 or more times)

Return the largest string X such that X divides str1 and X divides str2.

Example 1:

Input: str1 = “ABCABC”, str2 = “ABC”
Output: “ABC”

Example 2:

Input: str1 = “ABABAB”, str2 = “ABAB”
Output: “AB”

Example 3:

Input: str1 = “LEET”, str2 = “CODE”
Output: “”

Note:

  1. 1 <= str1.length <= 1000
  2. 1 <= str2.length <= 1000
  3. str1[i] and str2[i] are English uppercase letters.

Read more »

[Leetcode 1069] Product Sales Analysis II

Posted on 2020-07-26 | In leetcode | Comments:

原题说明

SQL Schema

Table: Sales

+————-+——-+
| Column Name | Type |
+————-+——-+
| sale_id | int |
| product_id | int |
| year | int |
| quantity | int |
| price | int |
+————-+——-+
sale_id is the primary key of this table.
product_id is a foreign key to Product table.
Note that the price is per unit.

Table: Product

+————–+———+
| Column Name | Type |
+————–+———+
| product_id | int |
| product_name | varchar |
+————–+———+
product_id is the primary key of this table.

 

Write an SQL query that reports the total quantity sold for every product id.

The query result format is in the following example:

Sales table:
+———+————+——+———-+——-+
| sale_id | product_id | year | quantity | price |
+———+————+——+———-+——-+
| 1 | 100 | 2008 | 10 | 5000 |
| 2 | 100 | 2009 | 12 | 5000 |
| 7 | 200 | 2011 | 15 | 9000 |
+———+————+——+———-+——-+
Product table:
+————+————–+
| product_id | product_name |
+————+————–+
| 100 | Nokia |
| 200 | Apple |
| 300 | Samsung |
+————+————–+
Result table:
+————–+—————-+
| product_id | total_quantity |
+————–+—————-+
| 100 | 22 |
| 200 | 15 |
+————–+—————-+

Read more »

[Leetcode 1067] Digit Count in Range

Posted on 2020-07-26 | In leetcode | Comments:

原题说明

Given an integer d between 0 and 9, and two positive integers low and high as lower and upper bounds, respectively. Return the number of times that d occurs as a digit in all integers between low and high, including the bounds low and high.

Example 1:

Input: d = 1, low = 1, high = 13
Output: 6
Explanation:
The digit d=1 occurs 6 times in 1,10,11,12,13. Note that the digit d=1 occurs twice in the number 11.

Example 2:

Input: d = 3, low = 100, high = 250
Output: 35
Explanation:
The digit d=3 occurs 35 times in 103,113,123,130,131,…,238,239,243.

Note:

  1. 0 <= d <= 9
  2. 1 <= low <= high <= 2×10^8

Read more »

[Leetcode 1065] Index Pairs of a String

Posted on 2020-07-26 | In leetcode | Comments:

原题说明

Given a text string and words (a list of strings), return all index pairs [i, j] so that the substring text[i]…text[j] is in the list of words. 

Example 1:

Input: text = “thestoryofleetcodeandme”, words = [“story”,”fleet”,”leetcode”]
Output: [[3,7],[9,13],[10,17]]

Example 2:

Input: text = “ababa”, words = [“aba”,”ab”]
Output: [[0,1],[0,2],[2,3],[2,4]]
Explanation:
Notice that matches can overlap, see “aba” is found in [0,2] and [2,4].

Note:

  1. All strings contains only lowercase English letters.
  2. It’s guaranteed that all strings in words are different.
  3. 1 <= text.length <= 100
  4. 1 <= words.length <= 20
  5. 1 <= words[i].length <= 50
  6. Return the pairs [i,j] in sorted order (i.e. sort them by their first coordinate in case of ties sort them by their second coordinate).

Read more »

[Leetcode 1072] Flip Columns For Maximum Number of Equal Rows

Posted on 2020-07-25 | In leetcode | Comments:

原题说明

Given a matrix consisting of 0s and 1s, we may choose any number of columns in the matrix and flip every cell in that column.  Flipping a cell changes the value of that cell from 0 to 1 or from 1 to 0.

Return the maximum number of rows that have all values equal after some number of flips.

 

    Example 1:

    Input: [[0,1],[1,1]]
    Output: 1
    Explanation: After flipping no values, 1 row has all values equal.

    Example 2:

    Input: [[0,1],[1,0]]
    Output: 2
    Explanation: After flipping values in the first column, both rows have equal values.

    Example 3:

    Input: [[0,0,0],[0,0,1],[1,1,0]]
    Output: 2
    Explanation: After flipping values in the first two columns, the last two rows have equal values.

     

    Note:

    1. 1 <= matrix.length <= 300
    2. 1 <= matrix[i].length <= 300
    3. All matrix[i].length‘s are equal
    4. matrix[i][j] is 0 or 1

    Read more »

    [Leetcode 1070] Product Sales Analysis III

    Posted on 2020-07-25 | In leetcode | Comments:

    原题说明

    Table: Sales

    +————-+——-+
    | Column Name | Type |
    +————-+——-+
    | sale_id | int |
    | product_id | int |
    | year | int |
    | quantity | int |
    | price | int |
    +————-+——-+
    sale_id is the primary key of this table.
    product_id is a foreign key to Product table.
    Note that the price is per unit.

    Table: Product

    +————–+———+
    | Column Name | Type |
    +————–+———+
    | product_id | int |
    | product_name | varchar |
    +————–+———+
    product_id is the primary key of this table.

     

    Write an SQL query that selects the product id, year, quantity, and price for the first year of every product sold.

    The query result format is in the following example:

    Sales table:
    +———+————+——+———-+——-+
    | sale_id | product_id | year | quantity | price |
    +———+————+——+———-+——-+
    | 1 | 100 | 2008 | 10 | 5000 |
    | 2 | 100 | 2009 | 12 | 5000 |
    | 7 | 200 | 2011 | 15 | 9000 |
    +———+————+——+———-+——-+

    Product table:
    +————+————–+
    | product_id | product_name |
    +————+————–+
    | 100 | Nokia |
    | 200 | Apple |
    | 300 | Samsung |
    +————+————–+

    Result table:
    +————+————+———-+——-+
    | product_id | first_year | quantity | price |
    +————+————+———-+——-+
    | 100 | 2008 | 10 | 5000 |
    | 200 | 2011 | 15 | 9000 |
    +————+————+———-+——-+


    Read more »

    [Leetcode 1068] Product Sales Analysis I

    Posted on 2020-07-02 | In leetcode | Comments:

    原题说明

    Table: Sales

    +————-+——-+
    | Column Name | Type |
    +————-+——-+
    | sale_id | int |
    | product_id | int |
    | year | int |
    | quantity | int |
    | price | int |
    +————-+——-+
    (sale_id, year) is the primary key of this table.
    product_id is a foreign key to Product table.
    Note that the price is per unit.

    Table: Product

    +————–+———+
    | Column Name | Type |
    +————–+———+
    | product_id | int |
    | product_name | varchar |
    +————–+———+
    product_id is the primary key of this table.

     

    Write an SQL query that reports all product names of the products in the Sales table along with their selling year and price.

    For example:

    Sales table:
    +———+————+——+———-+——-+
    | sale_id | product_id | year | quantity | price |
    +———+————+——+———-+——-+
    | 1 | 100 | 2008 | 10 | 5000 |
    | 2 | 100 | 2009 | 12 | 5000 |
    | 7 | 200 | 2011 | 15 | 9000 |
    +———+————+——+———-+——-+

    Product table:
    +————+————–+
    | product_id | product_name |
    +————+————–+
    | 100 | Nokia |
    | 200 | Apple |
    | 300 | Samsung |
    +————+————–+

    Result table:
    +————–+——-+——-+
    | product_name | year | price |
    +————–+——-+——-+
    | Nokia | 2008 | 5000 |
    | Nokia | 2009 | 5000 |
    | Apple | 2011 | 9000 |
    +————–+——-+——-+


    Read more »

    [Leetcode 1066] Campus Bikes II

    Posted on 2020-07-02 | In leetcode | Comments:

    原题说明

    On a campus represented as a 2D grid, there are N workers and M bikes, with N <= M. Each worker and bike is a 2D coordinate on this grid.

    We assign one unique bike to each worker so that the sum of the Manhattan distances between each worker and their assigned bike is minimized.

    The Manhattan distance between two points p1 and p2 is Manhattan(p1, p2) = |p1.x - p2.x| + |p1.y - p2.y|.

    Return the minimum possible sum of Manhattan distances between each worker and their assigned bike.

     

    Example 1:

    Input: workers = [[0,0],[2,1]], bikes = [[1,2],[3,3]]
    Output: 6
    Explanation:
    We assign bike 0 to worker 0, bike 1 to worker 1. The Manhattan distance of both assignments is 3, so the output is 6.

    Example 2:

    Input: workers = [[0,0],[1,1],[2,0]], bikes = [[1,0],[2,2],[2,1]]
    Output: 4
    Explanation:
    We first assign bike 0 to worker 0, then assign bike 1 to worker 1 or worker 2, bike 2 to worker 2 or worker 1. Both assignments lead to sum of the Manhattan distances as 4.

     

    Note:

    1. 0 <= workers[i][0], workers[i][1], bikes[i][0], bikes[i][1] < 1000
    2. All worker and bike locations are distinct.
    3. 1 <= workers.length <= bikes.length <= 10

    Read more »
    123…13

    猩猩的乐园

    技术面试问题详解
    123 posts
    2 categories
    69 tags
    RSS
    © 2018 – 2020 猩猩的乐园
    |