Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package g2201_2300.s2217_find_palindrome_with_fixed_length;

// #Medium #Array #Math #2022_06_12_Time_37_ms_(88.60%)_Space_53.7_MB_(77.19%)

@SuppressWarnings("java:S2184")
public class Solution {
public long[] kthPalindrome(int[] queries, int intLength) {
long minHalf = (long) Math.pow(10, (intLength - 1) / 2);
long maxIndex = (long) Math.pow(10, (intLength + 1) / 2) - minHalf;
boolean isOdd = intLength % 2 == 1;
long[] res = new long[queries.length];
for (int i = 0; i < res.length; i++) {
res[i] = queries[i] > maxIndex ? -1 : helper(queries[i], minHalf, isOdd);
}
return res;
}

private long helper(long index, long minHalf, boolean isOdd) {
long half = minHalf + index - 1;
long res = half;
if (isOdd) {
res /= 10;
}
while (half != 0) {
res = res * 10 + half % 10;
half /= 10;
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
2217\. Find Palindrome With Fixed Length

Medium

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_.

A **palindrome** is a number that reads the same backwards and forwards. Palindromes cannot have leading zeros.

**Example 1:**

**Input:** queries = [1,2,3,4,5,90], intLength = 3

**Output:** [101,111,121,131,141,999]

**Explanation:**

The first few palindromes of length 3 are:

101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, ...

The 90<sup>th</sup> palindrome of length 3 is 999.

**Example 2:**

**Input:** queries = [2,4,6], intLength = 4

**Output:** [1111,1331,1551]

**Explanation:**

The first six palindromes of length 4 are:

1001, 1111, 1221, 1331, 1441, and 1551.

**Constraints:**

* <code>1 <= queries.length <= 5 * 10<sup>4</sup></code>
* <code>1 <= queries[i] <= 10<sup>9</sup></code>
* `1 <= intLength <= 15`
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package g2201_2300.s2218_maximum_value_of_k_coins_from_piles;

// #Hard #Array #Dynamic_Programming #Prefix_Sum
// #2022_06_12_Time_54_ms_(96.38%)_Space_42.3_MB_(97.46%)

import java.util.List;

public class Solution {
public int maxValueOfCoins(List<List<Integer>> piles, int k) {
int[] dp = new int[k + 1];
for (List<Integer> pile : piles) {
int m = pile.size();
int[] cum = new int[m + 1];
for (int i = 0; i < m; i++) {
cum[i + 1] = cum[i] + pile.get(i);
}
int[] curdp = new int[k + 1];
for (int i = 0; i <= k; i++) {
for (int j = 0; j <= m && i + j <= k; j++) {
curdp[i + j] = Math.max(curdp[i + j], dp[i] + cum[j]);
}
}
dp = curdp;
}
return dp[k];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
2218\. Maximum Value of K Coins From Piles

Hard

There are `n` **piles** of coins on a table. Each pile consists of a **positive number** of coins of assorted denominations.

In one move, you can choose any coin on **top** of any pile, remove it, and add it to your wallet.

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_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2019/11/09/e1.png)

**Input:** piles = [[1,100,3],[7,8,9]], k = 2

**Output:** 101

**Explanation:** The above diagram shows the different ways we can choose k coins.

The maximum total we can obtain is 101.

**Example 2:**

**Input:** piles = [[100],[100],[100],[100],[100],[100],[1,1,1,1,1,1,700]], k = 7

**Output:** 706

**Explanation:** The maximum total can be obtained if we choose all coins from the last pile.

**Constraints:**

* `n == piles.length`
* `1 <= n <= 1000`
* <code>1 <= piles[i][j] <= 10<sup>5</sup></code>
* `1 <= k <= sum(piles[i].length) <= 2000`
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package g2201_2300.s2220_minimum_bit_flips_to_convert_number;

// #Easy #Bit_Manipulation #2022_06_12_Time_1_ms_(67.86%)_Space_41.4_MB_(25.22%)

public class Solution {
private int decToBinary(int n) {
int[] binaryNum = new int[32];
int i = 0;
while (n > 0) {
binaryNum[i] = n % 2;
n = n / 2;
i++;
}

int answer = 0;
for (int j = i - 1; j >= 0; j--) {
if (binaryNum[j] == 1) {
answer++;
}
}

return answer;
}

public int minBitFlips(int start, int goal) {
int answer = start ^ goal;
return decToBinary(answer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
2220\. Minimum Bit Flips to Convert Number

Easy

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`.

* 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.

Given two integers `start` and `goal`, return _the **minimum** number of **bit flips** to convert_ `start` _to_ `goal`.

**Example 1:**

**Input:** start = 10, goal = 7

**Output:** 3

**Explanation:** The binary representation of 10 and 7 are 1010 and 0111 respectively. We can convert 10 to 7 in 3 steps:

- Flip the first bit from the right: 1010 -> 1011.

- Flip the third bit from the right: 1011 -> 1111\.

- Flip the fourth bit from the right: 1111 -> 0111\.

It can be shown we cannot convert 10 to 7 in less than 3 steps. Hence, we return 3.

**Example 2:**

**Input:** start = 3, goal = 4

**Output:** 3

**Explanation:** The binary representation of 3 and 4 are 011 and 100 respectively. We can convert 3 to 4 in 3 steps:

- Flip the first bit from the right: 011 -> 010.

- Flip the second bit from the right: 010 -> 000\.

- Flip the third bit from the right: 000 -> 100\.

It can be shown we cannot convert 3 to 4 in less than 3 steps. Hence, we return 3.

**Constraints:**

* <code>0 <= start, goal <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package g2201_2300.s2222_number_of_ways_to_select_buildings;

// #Medium #String #Dynamic_Programming #Prefix_Sum
// #2022_06_12_Time_19_ms_(98.28%)_Space_42.6_MB_(98.62%)

public class Solution {
public long numberOfWays(String s) {
long z = 0;
long o = 0;
long zo = 0;
long oz = 0;
long zoz = 0;
long ozo = 0;
for (char c : s.toCharArray()) {
if (c == '0') {
zoz += zo;
oz += o;
z++;
} else {
ozo += oz;
zo += z;
o++;
}
}
return zoz + ozo;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
2222\. Number of Ways to Select Buildings

Medium

You are given a **0-indexed** binary string `s` which represents the types of buildings along a street where:

* `s[i] = '0'` denotes that the <code>i<sup>th</sup></code> building is an office and
* `s[i] = '1'` denotes that the <code>i<sup>th</sup></code> building is a restaurant.

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.

* 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.

Return _the **number of valid ways** to select 3 buildings._

**Example 1:**

**Input:** s = "001101"

**Output:** 6

**Explanation:** The following sets of indices selected are valid:

- [0,2,4] from "**0**0**1**1**0**1" forms "010"

- [0,3,4] from "**0**01**10**1" forms "010"

- [1,2,4] from "0**01**1**0**1" forms "010"

- [1,3,4] from "0**0**1**10**1" forms "010"

- [2,4,5] from "00**1**1**01**" forms "101"

- [3,4,5] from "001**101**" forms "101"

No other selection is valid. Thus, there are 6 total ways.

**Example 2:**

**Input:** s = "11100"

**Output:** 0

**Explanation:** It can be shown that there are no valid selections.

**Constraints:**

* <code>3 <= s.length <= 10<sup>5</sup></code>
* `s[i]` is either `'0'` or `'1'`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package g2201_2300.s2223_sum_of_scores_of_built_strings;

// #Hard #String #Binary_Search #Hash_Function #String_Matching #Rolling_Hash #Suffix_Array
// #2022_06_12_Time_21_ms_(63.91%)_Space_54.3_MB_(42.86%)

public class Solution {
public long sumScores(String s) {
int n = s.length();
char[] ss = s.toCharArray();
int[] z = new int[n];
int l = 0;
int r = 0;
for (int i = 1; i < n; i++) {
if (i <= r) {
z[i] = Math.min(z[i - l], r - i + 1);
}
while (i + z[i] < n && ss[z[i]] == ss[i + z[i]]) {
z[i]++;
}
if (i + z[i] - 1 > r) {
l = i;
r = i + z[i] - 1;
}
}
long sum = n;
for (int i = 0; i < n; i++) {
sum += z[i];
}
return sum;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
2223\. Sum of Scores of Built Strings

Hard

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>.

* 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.

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>).

Given the final string `s`, return _the **sum** of the **score** of every_ <code>s<sub>i</sub></code>.

**Example 1:**

**Input:** s = "babab"

**Output:** 9

**Explanation:** For s<sub>1</sub> == "b", the longest common prefix is "b" which has a score of 1.

For s<sub>2</sub> == "ab", there is no common prefix so the score is 0.

For s<sub>3</sub> == "bab", the longest common prefix is "bab" which has a score of 3.

For s<sub>4</sub> == "abab", there is no common prefix so the score is 0.

For s<sub>5</sub> == "babab", the longest common prefix is "babab" which has a score of 5.

The sum of the scores is 1 + 0 + 3 + 0 + 5 = 9, so we return 9.

**Example 2:**

**Input:** s = "azbazbzaz"

**Output:** 14

**Explanation:**

For s<sub>2</sub> == "az", the longest common prefix is "az" which has a score of 2.

For s<sub>6</sub> == "azbzaz", the longest common prefix is "azb" which has a score of 3.

For s<sub>9</sub> == "azbazbzaz", the longest common prefix is "azbazbzaz" which has a score of 9.

For all other s<sub>i</sub>, the score is 0.

The sum of the scores is 2 + 3 + 9 = 14, so we return 14.

**Constraints:**

* <code>1 <= s.length <= 10<sup>5</sup></code>
* `s` consists of lowercase English letters.
Loading