1.58K subscribers
598 photos
1 file
970 links
Don't miss a day to solve the problem
My leetcode graph - https://leetcode.com/SamoylenkoDmitry/
Download Telegram
Problem URL: [Knight Dialer](https://leetcode.com/problems/knight-dialer/?envType=daily-question&envId=2023-11-27)

The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagram.

A chess knight can move as indicated in the chess diagram below.

We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).

Given an integer n, return how many distinct phone numbers of length n we can dial.

You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.

As the answer may be very large, return the answer modulo 109 + 7.

Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.

Example 2:
Input: n = 2
Output: 20
Explanation: All the valid numbers we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]

Example 3:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.

Constraints:
1 <= n <= 5000
👍1
Problem:

Along a long library corridor, there is a line of seats and decorative plants. You are given a 0-indexed string corridor of length n consisting of letters 'S' and 'P' where each 'S' represents a seat and each 'P' represents a plant.

One room divider has already been installed to the left of index 0, and another to the right of index n - 1. Additional room dividers can be installed. For each position between indices i - 1 and i (1 <= i <= n - 1), at most one divider can be installed.

Divide the corridor into non-overlapping sections, where each section has exactly two seats with any number of plants. There may be multiple ways to perform the division. Two ways are different if there is a position with a room divider installed in the first way but not in the second way.

Return the number of ways to divide the corridor. Since the answer may be very large, return it modulo 109 + 7. If there is no way, return 0.

Example 1:

- Input: corridor = "SSPPSPS"
- Output: 3

Explanation: There are 3 different ways to divide the corridor.
The black bars in the above image indicate the two room dividers already installed.
Note that in each of the ways, each section has exactly two seats.

Example 2:

- Input: corridor = "PPSPSP"
- Output: 1

Explanation: There is only 1 way to divide the corridor, by not installing any additional dividers.
Installing any would create some section that does not have exactly two seats.

Example 3:

- Input: corridor = "S"
- Output: 0

Explanation: There is no way to divide the corridor because there will always be a section that does not have exactly two seats.

Constraints:

- n == corridor.length
- 1 <= n <= 10^5
- corridor[i] is either 'S' or 'P'.
👍1
### Problem Description

Problem url: [Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/?envType=daily-question&amp;envId=2023-11-29)

Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).

### Note

Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned.

In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 3, the input represents the signed integer -3.

### Examples
#### Example 1:
Input: n = 00000000000000000000000000001011
Output: 3
Explanation: The input binary string 00000000000000000000000000001011 has a total of three '1' bits.

#### Example 2:
Input: n = 00000000000000000000000010000000
Output: 1
Explanation: The input binary string 00000000000000000000000010000000 has a total of one '1' bit.

#### Example 3:
Input: n = 11111111111111111111111111111101
Output: 31
Explanation: The input binary string 11111111111111111111111111111101 has a total of thirty one '1' bits.


### Constraints

The input must be a binary string of length 32.

### Follow up

If this function is called many times, how would you optimize it?
👍1
[Problem URL](https://leetcode.com/problems/minimum-one-bit-operations-to-make-integers-zero/?envType=daily-question&amp;envId=2023-11-30)

Given an integer n, transform it into 0 using the following operations:

- Change the rightmost (0th) bit in the binary representation of n.
- Change the ith bit in the binary representation of n if the (i-1)th bit is set to 1 and the (i-2)th through 0th bits are set to 0.

Return the minimum number of operations to transform n into 0.

#### Example 1:
Input: n = 3
Output: 2

Explanation:
The binary representation of 3 is "11".
- "11" -> "01" with the 2nd operation since the 0th bit is 1.
- "01" -> "00" with the 1st operation.

#### Example 2:
Input: n = 6
Output: 4

Explanation:
The binary representation of 6 is "110".
- "110" -> "010" with the 2nd operation since the 1st bit is 1 and 0th through 0th bits are 0.
- "010" -> "011" with the 1st operation.
- "011" -> "001" with the 2nd operation since the 0th bit is 1.
- "001" -> "000" with the 1st operation.

#### Constraints:
0 <= n <= 10^9
👍2
Problem URL: [Check if Two String Arrays are Equivalent](https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/?envType=daily-question&amp;envId=2023-12-01)

Text:
Given two string arrays word1 and word2, return true if the two arrays represent the same string, and false otherwise.

A string is represented by an array if the array elements concatenated in order forms the string.

Example 1:
Input:
- word1 = ["ab", "c"]
- word2 = ["a", "bc"]

Output: true

Explanation:
- word1 represents string "ab" + "c" -> "abc"
- word2 represents string "a" + "bc" -> "abc"
- The strings are the same, so return true.

Example 2:
Input:
- word1 = ["a", "cb"]
- word2 = ["ab", "c"]

Output: false

Example 3:
Input:
- word1 = ["abc", "d", "defg"]
- word2 = ["abcddefg"]

Output: true

Constraints:
- 1 <= word1.length, word2.length <= 10^3
- 1 <= word1[i].length, word2[i].length <= 10^3
- 1 <= sum(word1[i].length), sum(word2[i].length) <= 10^3
- word1[i] and word2[i] consist of lowercase letters.
👍1
#### Problem
We are given an array of strings words and a string chars. A string is considered "good" if it can be formed using characters from chars, with each character used only once. We need to find the sum of the lengths of all "good" strings in words.

#### Example 1
- Input:
- words = ["cat","bt","hat","tree"]
- chars = "atach"
- Output: 6
- Explanation: The "good" strings that can be formed are "cat" and "hat", so the answer is 3 + 3 = 6.

#### Example 2
- Input:
- words = ["hello","world","leetcode"]
- chars = "welldonehoneyr"
- Output: 10
- Explanation: The "good" strings that can be formed are "hello" and "world", so the answer is 5 + 5 = 10.

#### Constraints
- 1 <= words.length <= 1000
- 1 <= words[i].length, chars.length <= 100
- words[i] and chars consist of lowercase English letters.

#### Problem URL
[Problem URL](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/?envType=daily-question&amp;envId=2023-12-02)
👍1
#### Problem Description

On a 2D plane, there are n points with integer coordinates points[i] = [xi, yi]. Return the minimum time in seconds to visit all the points in the order given by points.

You can move according to these rules:
- In 1 second, you can either:
- move vertically by one unit,
- move horizontally by one unit, or
- move diagonally sqrt(2) units (in other words, move one unit vertically then one unit horizontally in 1 second).

You have to visit the points in the same order as they appear in the array. You are allowed to pass through points that appear later in the order, but these do not count as visits.

#### Example 1:
Input: points = [[1,1],[3,4],[-1,0]]
Output: 7
Explanation: One optimal path is [1,1] -> [2,2] -> [3,3] -> [3,4] -> [2,3] -> [1,2] -> [0,1] -> [-1,0]
Time from [1,1] to [3,4] = 3 seconds
Time from [3,4] to [-1,0] = 4 seconds
Total time = 7 seconds

#### Constraints:

- 1 <= points.length <= 100
- points[i].length == 2
- -1000 <= points[i][0], points[i][1] <= 1000
👍1
Problem URL: [https://leetcode.com/problems/largest-3-same-digit-number-in-string/?envType=daily-question&amp;envId=2023-12-04](https://leetcode.com/problems/largest-3-same-digit-number-in-string/?envType=daily-question&amp;envId=2023-12-04)

Problem Statement

You are given a string num representing a large integer. An integer is good if it meets the following conditions:

- It is a substring of num with length 3.
- It consists of only one unique digit.

Return the maximum good integer as a string or an empty string "" if no such integer exists.

Note:
- A substring is a contiguous sequence of characters within a string.
- There may be leading zeroes in num or a good integer.

Example 1:
Input: num = "6777133339"
Output: "777"
Explanation: There are two distinct good integers: "777" and "333".
"777" is the largest, so we return "777".


Example 2:
Input: num = "2300019"
Output: "000"
Explanation: "000" is the only good integer.



Constraints:
- 3 <= num.length <= 1000
- num only consists of digits.
👍2
Problem url: [Count of Matches in Tournament](https://leetcode.com/problems/count-of-matches-in-tournament/?envType=daily-question&amp;envId=2023-12-05)
Text:
Given an integer n, the number of teams in a tournament. You need to return the number of matches played in the tournament until a winner is decided.

If the current number of teams is even, each team gets paired with another team. A total of n / 2 matches are played, and n / 2 teams advance to the next round.

If the current number of teams is odd, one team randomly advances in the tournament, and the rest gets paired. A total of (n - 1) / 2 matches are played, and (n - 1) / 2 + 1 teams advance to the next round.

Example 1:
Input: n = 7
Output: 6
Explanation:
- 1st Round: Teams = 7, Matches = 3, and 4 teams advance.
- 2nd Round: Teams = 4, Matches = 2, and 2 teams advance.
- 3rd Round: Teams = 2, Matches = 1, and 1 team is declared the winner.
Total number of matches = 3 + 2 + 1 = 6.

Constraints:
1 <= n <= 200
👍1
I am traveling, so here is a sunrise at 11 km, leetcode maybe later
🤝8🆒1
I think this easy week is somehow related to ongoing AdventOfCode 😉
🫡1
https://leetcode.com/problems/largest-odd-number-in-string/description/

Return the largest valued odd integer that is a non-empty substring of a given string num. If no odd integer exists, return an empty string.
👍1
https://leetcode.com/problems/construct-string-from-binary-tree/description/

606. Construct String from Binary Tree

Given the root of a binary tree, construct a string consisting of parenthesis and integers using the preorder traversal. Omit all unnecessary empty parenthesis pairs that do not affect the one-to-one mapping relationship between the string and the original binary tree.

Example 1:

Input: root = [1,2,3,4]
Output: "1(2(4))(3)"
Explanation: Originally, it needs to be "1(2(4)())(3()())", but you need to omit all the unnecessary empty parenthesis pairs. And it will be "1(2(4))(3)"

Example 2:

Input: root = [1,2,3,null,4]
Output: "1(2()(4))(3)"
Explanation: Almost the same as the first example, except we cannot omit the first parenthesis pair to break the one-to-one mapping relationship between the input and the output.

Constraints:

- The number of nodes in the tree is in the range [1, 104].
- Node.val is between -1000 and 1000.
👍1
https://leetcode.com/problems/binary-tree-inorder-traversal/description/

Binary Tree Inorder Traversal - Given the root of a binary tree, return the inorder traversal of its nodes' values.
👍1