# leetcode weekly The 278th game of the week is really difficult, but you can still brush the inverted hash of simple sorting + traversal prefix and traversal string

Posted by goodluck4287 on Sun, 30 Jan 2022 13:02:13 +0100

📖 Content of this article: leetcode weekly The 278th game of the week is really difficult, but you can still brush simple sorting + traversal double pointer + prefix and sum

📑 Article column: leetcode weekly game punch in

📆 Last updated: January 23, 2022 leetcode weekly Game 277 of the week

🙊 Personal profile: a Junior Program ape who is studying in a two-year college. In line with paying attention to foundation, clock in algorithm and sharing technology, as a blogger who summarizes personal experience, although he may be lazy sometimes, he will stick to it. If you like blog very much, it is suggested to look at the following line ~ (crazy hint QwQ)

🌇 give the thumbs-up 👍 Collection ⭐ Leaving a message. 📝 One key three connection care program ape, start with you and me

# 🙊 Write in front

🙊 Xiaofu is here. Today, we will update the weekly competition column. Today, Xiaofu plays the weekly competition for the third time. Compared with before, I only know that the questions produced by Netease mutual entertainment are really more or less touching. They are really difficult and really test the level of the problem makers. Sure enough, big factories are big factories. All the questions make you rua brain wide and lose your hair. Today, Xiaofu only A two questions, Try to explain the big man's solution of the third question to everyone clearly. That's it, rush!

# 📆 Week 278 - January 30, 2022

## 📝 T1.5993. Multiply the found value by 2

### subject

Give you an integer array nums, and give you an integer original, which is the first number to search in nums.

Next, you need to follow the following steps:

If the original is found in nums, multiply the original by 2 to get a new original (that is, make original = 2 * original).
Otherwise, stop the process.
As long as the new original can be found in the array, continue the process for the new original.
Returns the final value of original.

### Examples

Example 1:

```Input: nums = [5,3,6,1,12], original = 3
Output: 24
Explanation:
- 3 Can be nums Found in. three * 2 = 6 .
- 6 Can be nums Found in. six * 2 = 12 .
- 12 Can be nums Found in. twelve * 2 = 24 .
- 24 Not in nums Found in. Therefore, return to 24.
```

Example 2:

```Input: nums = [2,7,9], original = 4
Output: 4
Explanation:
- 4 Not in nums Found in. Therefore, return 4.
```

### Tips

1 <= nums.length <= 1000
1 <= nums[i], original <= 1000

### ⭐ Train of thought ⭐

Simple sorting + one-time traversal judgment

### code implementation

```class Solution {
public int findFinalValue(int[] nums, int original) {
int n = nums.length;
Arrays.sort(nums);
for (int i = 0;i< n;i++){
if (original == nums[i]){
original = nums[i];
original *=2;
}
}

return original;
}
}
```

## 📝 T2.5981. All subscripts with the highest score in the group

### subject

Give you a binary array nums with subscript starting from 0, and the array length is n. Nums can be split into two arrays (possibly empty) by subscript i (0 < = I < = n): numleft and numright.

Numleft contains all elements from subscript 0 to i - 1 in nums (including 0 and i - 1), while numright contains all elements from subscript i to n - 1 in nums (including I and n - 1).
If i == 0, numleft is empty and numright will contain all elements in nums.
If i == n, numleft will contain all elements in nums, and numright is empty.
The grouping score of subscript i is the sum of the number of 0 in numleft and the number of 1 in numright.

Returns all different subscripts with the highest score in the group. You can return answers in any order.

### Examples

Example 1:

```Input: nums = [0,0,1,0]
Output:[2,4]
Explanation: group by subscript
- 0 : numsleft by [] . numsright by [0,0,1,0] . Score 0 + 1 = 1 .
- 1 : numsleft by [0] . numsright by [0,1,0] . Score 1 + 1 = 2 .
- 2 : numsleft by [0,0] . numsright by [1,0] . Score 2 + 1 = 3 .
- 3 : numsleft by [0,0,1] . numsright by [0] . Score 2 + 0 = 2 .
- 4 : numsleft by [0,0,1,0] . numsright by [] . The score is 3 + 0 = 3 .
Subscripts 2 and 4 can get the highest grouping score 3.
Attention, the answer [4,2] It is also regarded as the correct answer.
```

Example 2:

```Input: nums = [0,0,0]
Output:[3]
Explanation: group by subscript
- 0 : numsleft by [] . numsright by [0,0,0] . Score 0 + 0 = 0 .
- 1 : numsleft by [0] . numsright by [0,0] . Score 1 + 0 = 1 .
- 2 : numsleft by [0,0] . numsright by [0] . Score 2 + 0 = 2 .
- 3 : numsleft by [0,0,0] . numsright by [] . The score is 3 + 0 = 3 .
Only subscript 3 can get the highest grouping score 3.
```

Example 3:

```Input: nums = [1,1]
Output:[0]
Explanation: group by subscript
- 0 : numsleft by [] . numsright by [1,1] . Score 0 + 2 = 2 .
- 1 : numsleft by [1] . numsright by [1] . Score 0 + 1 = 1 .
- 2 : numsleft by [1,1] . numsright by [] . Score 0 + 0 = 0 .
Only subscript 0 can get the highest grouping score of 2.
```

### Tips

n == nums.length
1 <= n <= 10^5
Num [i] is 0 or 1

### ⭐ Train of thought ⭐

At the beginning, Xiao Fu thought about how to quickly AC drop the problem. He thought it was a simple violence + hash table query that caused the timeout. Looking back, he meowed that n was 10 ^ 5. No wonder. Then we need to change ideas or optimize to solve problems.

• Prefixes and ideas
• When we traverse the first time, we only record the highest score on the right
• When we traverse twice, we need to determine the score of the left and right. If we encounter 0, the left value is + 1, and if we encounter 1, the right value is - 1.
• After obtaining the right and left values at the same time, judge with the max value. If the sum of the current maximum left and right values is less than the sum of the left and right values at this time, clear the data in the list and reset the position subscript of the coordinate position as the sum of the maximum left and right values.

### code implementation

```class Solution {
public List<Integer> maxScoreIndices(int[] nums) {
//Used to record the sum of the current maximum left and right values
int max = 0;
//Record the left and right values respectively
int left=0, right=0;
//The first traversal obtains the case that the left array is empty and the right array is full
for (int i: nums) {
if (i== 1) {
right++;
}
}
List<Integer> res = new ArrayList<>();
//At this time, the value of the right full value is the initial value of the sum of the maximum left and right values
max = right;
//At this time, the corresponding result subscript is 0
//Traverse for the second time
for (int i = 0; i <nums.length; i++) {
//Record the left value of the left array of the current subscript i size and the right value of the n-i size
if (nums[i] == 0) {
left++;
} else {
right--;
}
//If a new maximum value appears, the subscript of the previous record as the maximum value is cleared and then added
if (max < left + right) {
res.clear();
max = left + right;
} else if (max == left + right) {
}
}
return res;
}
}
```

## 📝 T3.5994. Find the substring of the given hash value

### subject

Given the integers p and m, the hash value of a string s with length k and subscript starting from 0 is calculated according to the following function:

hash(s, p, m) = (val(s[0]) * p0 + val(s[1]) * p1 + ... + val(s[k-1]) * pk-1) mod m.
Where val(s[i]) represents the subscript of s[i] in the alphabet, from val('a ') = 1 to val('z') = 26.

Give you a string s and integers power, modulo, K and hashValue. Please return the first substring sub with length k in s, which satisfies hash (sub, power, module) = = hashValue.

The test data guarantees that there must be at least one such substring.

A substring is defined as a sequence of consecutive non empty characters in a string.

### Examples

Example 1:

```Input: s = "leetcode", power = 7, modulo = 20, k = 2, hashValue = 0
Output:"ee"
Explanation:"ee" The hash value of is hash("ee", 7, 20) = (5 * 1 + 5 * 7) mod 20 = 40 mod 20 = 0 .
"ee" Is the first substring with a hash value of 0 with a length of 2, so we return "ee" .
```

### Tips

1 <= k <= s.length <= 2 * 10^4
1 <= power, modulo <= 10^9
0 <= hashValue < modulo
s contains only lowercase English letters.
The test data guarantees that there must be substrings that meet the conditions.

### ⭐ Train of thought ⭐

Posted here fsyun The solution of the boss. For your reference and study, Xiao Fu is still working hard. It seems that both sliding window and violence can be solved. The difficulty of this problem is not the power of large numbers, but the reverse inversion and hash is the positive solution. Alas, it seems that the string has always been a pain point~

### code implementation

```class Solution {
public String subStrHash(String s, int power, int modulo, int k, int hashValue) {
char[] str = s.toCharArray();
int n = str.length;
long x = 0, b = 1;
String ans = null;
for (int i = 0; i < k; i++) {
char ch = str[n - 1 - i];
x = (x * power + ch - 'a' + 1) % modulo;
}
for (int i = 0; i < k - 1; i++)
b = b * power % modulo;
if (x == hashValue)
ans = s.substring(n - k);
for (int i = n - k - 1; i >= 0; i--) {
x = (x + modulo - (b * (str[i + k] - 'a' + 1) % modulo)) % modulo;
char ch = str[i];
x = (x * power + ch - 'a' + 1) % modulo;
if (x == hashValue)
ans = s.substring(i, i + k);
}
return ans;
}
}
```

# 🙊 Write at the end

The third weekly match of Xiaofu punch in 2022-01-30

Try to do all the questions you can do well

Two questions were right in this week's competition

At the same time, they also know their weak areas

Related data structure of string and solution of algorithm

But this ranking has increased

So it's still a good weekly experience

last

Make progress every day and harvest every day

I wish you success in your career and success in your studies

If you feel good, don't forget to click three times~

Topics: Algorithm leetcode Permutation