XingXing Park

猩猩的乐园


  • Home

  • Archives

  • Tags

  • Job Info

  • Search

[Leetcode 1056] Confusing Number

Posted on 2020-06-13 | In leetcode | Comments:

原题说明

Given a number N, return true if and only if it is a confusing number, which satisfies the following condition:

We can rotate digits by 180 degrees to form new digits. When 0, 1, 6, 8, 9 are rotated 180 degrees, they become 0, 1, 9, 8, 6 respectively. When 2, 3, 4, 5 and 7 are rotated 180 degrees, they become invalid. A confusing number is a number that when rotated 180 degrees becomes a different number with each digit valid.

 

Example 1:

Input: 6
Output: true
Explanation:
We get 9 after rotating 6, 9 is a valid number and 9!=6.

Example 2:

Input: 89
Output: true
Explanation:
We get 68 after rotating 89, 86 is a valid number and 86!=89.

Example 3:

Input: 11
Output: false
Explanation:
We get 11 after rotating 11, 11 is a valid number but the value remains the same, thus 11 is not a confusing number.

Example 4:

Input: 25
Output: false
Explanation:
We get an invalid number after rotating 25.

Read more »

[Leetcode 1050] Actors and Directors Who Cooperated At Least Three Times

Posted on 2020-06-13 | In leetcode | Comments:

原题说明

SQL Schema

Table: ActorDirector

+————-+———+
| Column Name | Type |
+————-+———+
| actor_id | int |
| director_id | int |
| timestamp | int |
+————-+———+
timestamp is the primary key column for this table.

 

Write a SQL query for a report that provides the pairs (actor_id, director_id) where the actor have cooperated with the director at least 3 times.

Example:

ActorDirector table:
+————-+————-+————-+
| actor_id | director_id | timestamp |
+————-+————-+————-+
| 1 | 1 | 0 |
| 1 | 1 | 1 |
| 1 | 1 | 2 |
| 1 | 2 | 3 |
| 1 | 2 | 4 |
| 2 | 1 | 5 |
| 2 | 1 | 6 |
+————-+————-+————-+

Result table:
+————-+————-+
| actor_id | director_id |
+————-+————-+
| 1 | 1 |
+————-+————-+
The only pair is (1, 1) where they cooperated exactly 3 times.

Read more »

[Leetcode 1051] Height Checker

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

原题说明

Students are asked to stand in non-decreasing order of heights for an annual photo.

Return the minimum number of students that must move in order for all students to be standing in non-decreasing order of height.

Notice that when a group of students is selected they can reorder in any possible way between themselves and the non selected students remain on their seats.

 Example 1:

Input: heights = [1,1,4,2,1,3]
Output: 3
Explanation:
Current array : [1,1,4,2,1,3]
Target array : [1,1,1,2,3,4]
On index 2 (0-based) we have 4 vs 1 so we have to move this student.
On index 4 (0-based) we have 1 vs 3 so we have to move this student.
On index 5 (0-based) we have 3 vs 4 so we have to move this student.

Example 2:

Input: heights = [5,1,2,3,4]
Output: 5

Example 3:

Input: heights = [1,2,3,4,5]
Output: 0

Constraints:

  • 1 <= heights.length <= 100
  • 1 <= heights[i] <= 100

Read more »

[Leetcode 1049] Last Stone Weight II

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

原题说明

We have a collection of rocks, each rock has a positive integer weight.

Each turn, we choose any two rocks and smash them together.  Suppose the stones have weights x and y with x <= y.  The result of this smash is:

  • If x == y, both stones are totally destroyed;
  • If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x.

At the end, there is at most 1 stone left.  Return the smallest possible weight of this stone (the weight is 0 if there are no stones left.)

 Example 1:

Input: [2,7,4,1,8,1]
Output: 1
Explanation:
We can combine 2 and 4 to get 2 so the array converts to [2,7,1,8,1] then,
we can combine 7 and 8 to get 1 so the array converts to [2,1,1,1] then,
we can combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
we can combine 1 and 1 to get 0 so the array converts to [1] then that’s the optimal value. 

Note:

  1. 1 <= stones.length <= 30
  2. 1 <= stones[i] <= 100

Read more »

[Leetcode 1054] Distant Barcodes

Posted on 2020-06-05 | In leetcode | Comments:

原题说明

In a warehouse, there is a row of barcodes, where the i-th barcode is barcodes[i].

Rearrange the barcodes so that no two adjacent barcodes are equal.  You may return any answer, and it is guaranteed an answer exists.

 

Example 1:

Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]

Example 2:

Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]

 

Note:

  1. 1 <= barcodes.length <= 10000
  2. 1 <= barcodes[i] <= 10000

Read more »

[Leetcode 1052] Grumpy Bookstore Owner

Posted on 2020-06-05 | In leetcode | Comments:

原题说明

Today, the bookstore owner has a store open for customers.length minutes.  Every minute, some number of customers (customers[i]) enter the store, and all those customers leave after the end of that minute.

On some minutes, the bookstore owner is grumpy.  If the bookstore owner is grumpy on the i-th minute, grumpy[i] = 1, otherwise grumpy[i] = 0.  When the bookstore owner is grumpy, the customers of that minute are not satisfied, otherwise they are satisfied.

The bookstore owner knows a secret technique to keep themselves not grumpy for X minutes straight, but can only use it once.

Return the maximum number of customers that can be satisfied throughout the day.

 

Example 1:

Input: customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], X = 3
Output: 16
Explanation: The bookstore owner keeps themselves not grumpy for the last 3 minutes.
The maximum number of customers that can be satisfied = 1 + 1 + 1 + 1 + 7 + 5 = 16.

 

Note:

  • 1 <= X <= customers.length == grumpy.length <= 20000
  • 0 <= customers[i] <= 1000
  • 0 <= grumpy[i] <= 1

Read more »

[Leetcode 1047] Remove All Adjacent Duplicates In String

Posted on 2020-05-31 | In leetcode | Comments:

原题说明

Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent and equal letters, and removing them.

We repeatedly make duplicate removals on S until we no longer can.

Return the final string after all such duplicate removals have been made.  It is guaranteed the answer is unique.

Example 1:

Input: “abbaca”
Output: “ca”
Explanation:
For example, in “abbaca” we could remove “bb” since the letters are adjacent and equal, and this is the only possible move.  The result of this move is that the string is “aaca”, of which only “aa” is possible, so the final string is “ca”.

Note:

  1. 1 <= S.length <= 20000
  2. S consists only of English lowercase letters.

Read more »

[Leetcode 1045] Customers Who Bought All Products

Posted on 2020-05-31 | In leetcode | Comments:

原题说明

Table: Customer

+————-+———+
| Column Name | Type |
+————-+———+
| customer_id | int |
| product_key | int |
+————-+———+
product_key is a foreign key to Product table.

Table: Product

+————-+———+
| Column Name | Type |
+————-+———+
| product_key | int |
+————-+———+
product_key is the primary key column for this table.

 

Write an SQL query for a report that provides the customer ids from the Customer table that bought all the products in the Product table.

For example:

Customer table:
+————-+————-+
| customer_id | product_key |
+————-+————-+
| 1 | 5 |
| 2 | 6 |
| 3 | 5 |
| 3 | 6 |
| 1 | 6 |
+————-+————-+

Product table:
+————-+
| product_key |
+————-+
| 5 |
| 6 |
+————-+

Result table:
+————-+
| customer_id |
+————-+
| 1 |
| 3 |
+————-+
The customers who bought all the products (5 and 6) are customers with id 1 and 3.


Read more »

[Leetcode 1048] Longest String Chain

Posted on 2020-05-28 | In leetcode | Comments:

原题说明

Given a list of words, each word consists of English lowercase letters.

Let’s say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, “abc” is a predecessor of “abac”.

A word chain is a sequence of words [word_1, word_2, …, word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.

Return the longest possible length of a word chain with words chosen from the given list of words.

 

Example 1:

Input: [“a”,”b”,”ba”,”bca”,”bda”,”bdca”]
Output: 4
Explanation: one of
the longest word chain is “a”,”ba”,”bda”,”bdca”.

 

Note:

  1. 1 <= words.length <= 1000
  2. 1 <= words[i].length <= 16
  3. words[i] only consists of English lowercase letters.

Read more »

[Leetcode 1046] Last Stone Weight

Posted on 2020-05-28 | In leetcode | Comments:

原题说明

We have a collection of stones, each stone has a positive integer weight.

Each turn, we choose the two heaviest stones and smash them together.  Suppose the stones have weights x and y with x <= y.  The result of this smash is:

  • If x == y, both stones are totally destroyed;
  • If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x.

At the end, there is at most 1 stone left.  Return the weight of this stone (or 0 if there are no stones left.)

 

Example 1:

Input: [2,7,4,1,8,1]
Output: 1
Explanation:
We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then,
we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then,
we combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
we combine 1 and 1 to get 0 so the array converts to [1] then that’s the value of last stone.

 

Note:

  1. 1 <= stones.length <= 30
  2. 1 <= stones[i] <= 1000

Read more »
1…345…13

猩猩的乐园

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