Skip to content

Commit 3489315

Browse files
authored
Added tasks 2217, 2218, 2220, 2222, 2223.
1 parent b74f1d2 commit 3489315

File tree

15 files changed

+477
-0
lines changed

15 files changed

+477
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g2201_2300.s2217_find_palindrome_with_fixed_length;
2+
3+
// #Medium #Array #Math #2022_06_12_Time_37_ms_(88.60%)_Space_53.7_MB_(77.19%)
4+
5+
@SuppressWarnings("java:S2184")
6+
public class Solution {
7+
public long[] kthPalindrome(int[] queries, int intLength) {
8+
long minHalf = (long) Math.pow(10, (intLength - 1) / 2);
9+
long maxIndex = (long) Math.pow(10, (intLength + 1) / 2) - minHalf;
10+
boolean isOdd = intLength % 2 == 1;
11+
long[] res = new long[queries.length];
12+
for (int i = 0; i < res.length; i++) {
13+
res[i] = queries[i] > maxIndex ? -1 : helper(queries[i], minHalf, isOdd);
14+
}
15+
return res;
16+
}
17+
18+
private long helper(long index, long minHalf, boolean isOdd) {
19+
long half = minHalf + index - 1;
20+
long res = half;
21+
if (isOdd) {
22+
res /= 10;
23+
}
24+
while (half != 0) {
25+
res = res * 10 + half % 10;
26+
half /= 10;
27+
}
28+
return res;
29+
}
30+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2217\. Find Palindrome With Fixed Length
2+
3+
Medium
4+
5+
Given an integer array `queries` and a **positive** integer `intLength`, return _an array_ `answer` _where_ `answer[i]` _is either the_ <code>queries[i]<sup>th</sup></code> _smallest **positive palindrome** of length_ `intLength` _or_ `-1` _if no such palindrome exists_.
6+
7+
A **palindrome** is a number that reads the same backwards and forwards. Palindromes cannot have leading zeros.
8+
9+
**Example 1:**
10+
11+
**Input:** queries = [1,2,3,4,5,90], intLength = 3
12+
13+
**Output:** [101,111,121,131,141,999]
14+
15+
**Explanation:**
16+
17+
The first few palindromes of length 3 are:
18+
19+
101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, ...
20+
21+
The 90<sup>th</sup> palindrome of length 3 is 999.
22+
23+
**Example 2:**
24+
25+
**Input:** queries = [2,4,6], intLength = 4
26+
27+
**Output:** [1111,1331,1551]
28+
29+
**Explanation:**
30+
31+
The first six palindromes of length 4 are:
32+
33+
1001, 1111, 1221, 1331, 1441, and 1551.
34+
35+
**Constraints:**
36+
37+
* <code>1 <= queries.length <= 5 * 10<sup>4</sup></code>
38+
* <code>1 <= queries[i] <= 10<sup>9</sup></code>
39+
* `1 <= intLength <= 15`
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g2201_2300.s2218_maximum_value_of_k_coins_from_piles;
2+
3+
// #Hard #Array #Dynamic_Programming #Prefix_Sum
4+
// #2022_06_12_Time_54_ms_(96.38%)_Space_42.3_MB_(97.46%)
5+
6+
import java.util.List;
7+
8+
public class Solution {
9+
public int maxValueOfCoins(List<List<Integer>> piles, int k) {
10+
int[] dp = new int[k + 1];
11+
for (List<Integer> pile : piles) {
12+
int m = pile.size();
13+
int[] cum = new int[m + 1];
14+
for (int i = 0; i < m; i++) {
15+
cum[i + 1] = cum[i] + pile.get(i);
16+
}
17+
int[] curdp = new int[k + 1];
18+
for (int i = 0; i <= k; i++) {
19+
for (int j = 0; j <= m && i + j <= k; j++) {
20+
curdp[i + j] = Math.max(curdp[i + j], dp[i] + cum[j]);
21+
}
22+
}
23+
dp = curdp;
24+
}
25+
return dp[k];
26+
}
27+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2218\. Maximum Value of K Coins From Piles
2+
3+
Hard
4+
5+
There are `n` **piles** of coins on a table. Each pile consists of a **positive number** of coins of assorted denominations.
6+
7+
In one move, you can choose any coin on **top** of any pile, remove it, and add it to your wallet.
8+
9+
Given a list `piles`, where `piles[i]` is a list of integers denoting the composition of the <code>i<sup>th</sup></code> pile from **top to bottom**, and a positive integer `k`, return _the **maximum total value** of coins you can have in your wallet if you choose **exactly**_ `k` _coins optimally_.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2019/11/09/e1.png)
14+
15+
**Input:** piles = [[1,100,3],[7,8,9]], k = 2
16+
17+
**Output:** 101
18+
19+
**Explanation:** The above diagram shows the different ways we can choose k coins.
20+
21+
The maximum total we can obtain is 101.
22+
23+
**Example 2:**
24+
25+
**Input:** piles = [[100],[100],[100],[100],[100],[100],[1,1,1,1,1,1,700]], k = 7
26+
27+
**Output:** 706
28+
29+
**Explanation:** The maximum total can be obtained if we choose all coins from the last pile.
30+
31+
**Constraints:**
32+
33+
* `n == piles.length`
34+
* `1 <= n <= 1000`
35+
* <code>1 <= piles[i][j] <= 10<sup>5</sup></code>
36+
* `1 <= k <= sum(piles[i].length) <= 2000`
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g2201_2300.s2220_minimum_bit_flips_to_convert_number;
2+
3+
// #Easy #Bit_Manipulation #2022_06_12_Time_1_ms_(67.86%)_Space_41.4_MB_(25.22%)
4+
5+
public class Solution {
6+
private int decToBinary(int n) {
7+
int[] binaryNum = new int[32];
8+
int i = 0;
9+
while (n > 0) {
10+
binaryNum[i] = n % 2;
11+
n = n / 2;
12+
i++;
13+
}
14+
15+
int answer = 0;
16+
for (int j = i - 1; j >= 0; j--) {
17+
if (binaryNum[j] == 1) {
18+
answer++;
19+
}
20+
}
21+
22+
return answer;
23+
}
24+
25+
public int minBitFlips(int start, int goal) {
26+
int answer = start ^ goal;
27+
return decToBinary(answer);
28+
}
29+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2220\. Minimum Bit Flips to Convert Number
2+
3+
Easy
4+
5+
A **bit flip** of a number `x` is choosing a bit in the binary representation of `x` and **flipping** it from either `0` to `1` or `1` to `0`.
6+
7+
* For example, for `x = 7`, the binary representation is `111` and we may choose any bit (including any leading zeros not shown) and flip it. We can flip the first bit from the right to get `110`, flip the second bit from the right to get `101`, flip the fifth bit from the right (a leading zero) to get `10111`, etc.
8+
9+
Given two integers `start` and `goal`, return _the **minimum** number of **bit flips** to convert_ `start` _to_ `goal`.
10+
11+
**Example 1:**
12+
13+
**Input:** start = 10, goal = 7
14+
15+
**Output:** 3
16+
17+
**Explanation:** The binary representation of 10 and 7 are 1010 and 0111 respectively. We can convert 10 to 7 in 3 steps:
18+
19+
- Flip the first bit from the right: 1010 -> 1011.
20+
21+
- Flip the third bit from the right: 1011 -> 1111\.
22+
23+
- Flip the fourth bit from the right: 1111 -> 0111\.
24+
25+
It can be shown we cannot convert 10 to 7 in less than 3 steps. Hence, we return 3.
26+
27+
**Example 2:**
28+
29+
**Input:** start = 3, goal = 4
30+
31+
**Output:** 3
32+
33+
**Explanation:** The binary representation of 3 and 4 are 011 and 100 respectively. We can convert 3 to 4 in 3 steps:
34+
35+
- Flip the first bit from the right: 011 -> 010.
36+
37+
- Flip the second bit from the right: 010 -> 000\.
38+
39+
- Flip the third bit from the right: 000 -> 100\.
40+
41+
It can be shown we cannot convert 3 to 4 in less than 3 steps. Hence, we return 3.
42+
43+
**Constraints:**
44+
45+
* <code>0 <= start, goal <= 10<sup>9</sup></code>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g2201_2300.s2222_number_of_ways_to_select_buildings;
2+
3+
// #Medium #String #Dynamic_Programming #Prefix_Sum
4+
// #2022_06_12_Time_19_ms_(98.28%)_Space_42.6_MB_(98.62%)
5+
6+
public class Solution {
7+
public long numberOfWays(String s) {
8+
long z = 0;
9+
long o = 0;
10+
long zo = 0;
11+
long oz = 0;
12+
long zoz = 0;
13+
long ozo = 0;
14+
for (char c : s.toCharArray()) {
15+
if (c == '0') {
16+
zoz += zo;
17+
oz += o;
18+
z++;
19+
} else {
20+
ozo += oz;
21+
zo += z;
22+
o++;
23+
}
24+
}
25+
return zoz + ozo;
26+
}
27+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2222\. Number of Ways to Select Buildings
2+
3+
Medium
4+
5+
You are given a **0-indexed** binary string `s` which represents the types of buildings along a street where:
6+
7+
* `s[i] = '0'` denotes that the <code>i<sup>th</sup></code> building is an office and
8+
* `s[i] = '1'` denotes that the <code>i<sup>th</sup></code> building is a restaurant.
9+
10+
As a city official, you would like to **select** 3 buildings for random inspection. However, to ensure variety, **no two consecutive** buildings out of the **selected** buildings can be of the same type.
11+
12+
* For example, given `s = "0**0**1**1**0**1**"`, we cannot select the <code>1<sup>st</sup></code>, <code>3<sup>rd</sup></code>, and <code>5<sup>th</sup></code> buildings as that would form `"0**11**"` which is **not** allowed due to having two consecutive buildings of the same type.
13+
14+
Return _the **number of valid ways** to select 3 buildings._
15+
16+
**Example 1:**
17+
18+
**Input:** s = "001101"
19+
20+
**Output:** 6
21+
22+
**Explanation:** The following sets of indices selected are valid:
23+
24+
- [0,2,4] from "**0**0**1**1**0**1" forms "010"
25+
26+
- [0,3,4] from "**0**01**10**1" forms "010"
27+
28+
- [1,2,4] from "0**01**1**0**1" forms "010"
29+
30+
- [1,3,4] from "0**0**1**10**1" forms "010"
31+
32+
- [2,4,5] from "00**1**1**01**" forms "101"
33+
34+
- [3,4,5] from "001**101**" forms "101"
35+
36+
No other selection is valid. Thus, there are 6 total ways.
37+
38+
**Example 2:**
39+
40+
**Input:** s = "11100"
41+
42+
**Output:** 0
43+
44+
**Explanation:** It can be shown that there are no valid selections.
45+
46+
**Constraints:**
47+
48+
* <code>3 <= s.length <= 10<sup>5</sup></code>
49+
* `s[i]` is either `'0'` or `'1'`.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g2201_2300.s2223_sum_of_scores_of_built_strings;
2+
3+
// #Hard #String #Binary_Search #Hash_Function #String_Matching #Rolling_Hash #Suffix_Array
4+
// #2022_06_12_Time_21_ms_(63.91%)_Space_54.3_MB_(42.86%)
5+
6+
public class Solution {
7+
public long sumScores(String s) {
8+
int n = s.length();
9+
char[] ss = s.toCharArray();
10+
int[] z = new int[n];
11+
int l = 0;
12+
int r = 0;
13+
for (int i = 1; i < n; i++) {
14+
if (i <= r) {
15+
z[i] = Math.min(z[i - l], r - i + 1);
16+
}
17+
while (i + z[i] < n && ss[z[i]] == ss[i + z[i]]) {
18+
z[i]++;
19+
}
20+
if (i + z[i] - 1 > r) {
21+
l = i;
22+
r = i + z[i] - 1;
23+
}
24+
}
25+
long sum = n;
26+
for (int i = 0; i < n; i++) {
27+
sum += z[i];
28+
}
29+
return sum;
30+
}
31+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2223\. Sum of Scores of Built Strings
2+
3+
Hard
4+
5+
You are **building** a string `s` of length `n` **one** character at a time, **prepending** each new character to the **front** of the string. The strings are labeled from `1` to `n`, where the string with length `i` is labeled <code>s<sub>i</sub></code>.
6+
7+
* For example, for `s = "abaca"`, <code>s<sub>1</sub> == "a"</code>, <code>s<sub>2</sub> == "ca"</code>, <code>s<sub>3</sub> == "aca"</code>, etc.
8+
9+
The **score** of <code>s<sub>i</sub></code> is the length of the **longest common prefix** between <code>s<sub>i</sub></code> and <code>s<sub>n</sub></code> (Note that <code>s == s<sub>n</sub></code>).
10+
11+
Given the final string `s`, return _the **sum** of the **score** of every_ <code>s<sub>i</sub></code>.
12+
13+
**Example 1:**
14+
15+
**Input:** s = "babab"
16+
17+
**Output:** 9
18+
19+
**Explanation:** For s<sub>1</sub> == "b", the longest common prefix is "b" which has a score of 1.
20+
21+
For s<sub>2</sub> == "ab", there is no common prefix so the score is 0.
22+
23+
For s<sub>3</sub> == "bab", the longest common prefix is "bab" which has a score of 3.
24+
25+
For s<sub>4</sub> == "abab", there is no common prefix so the score is 0.
26+
27+
For s<sub>5</sub> == "babab", the longest common prefix is "babab" which has a score of 5.
28+
29+
The sum of the scores is 1 + 0 + 3 + 0 + 5 = 9, so we return 9.
30+
31+
**Example 2:**
32+
33+
**Input:** s = "azbazbzaz"
34+
35+
**Output:** 14
36+
37+
**Explanation:**
38+
39+
For s<sub>2</sub> == "az", the longest common prefix is "az" which has a score of 2.
40+
41+
For s<sub>6</sub> == "azbzaz", the longest common prefix is "azb" which has a score of 3.
42+
43+
For s<sub>9</sub> == "azbazbzaz", the longest common prefix is "azbazbzaz" which has a score of 9.
44+
45+
For all other s<sub>i</sub>, the score is 0.
46+
47+
The sum of the scores is 2 + 3 + 9 = 14, so we return 14.
48+
49+
**Constraints:**
50+
51+
* <code>1 <= s.length <= 10<sup>5</sup></code>
52+
* `s` consists of lowercase English letters.

0 commit comments

Comments
 (0)