# LeetCode notes: Biweekly Contest 62

Keywords: Python Algorithm leetcode

## 1. Topic 1

The test question link given in question 1 is as follows:

### 1. Problem solving ideas

This question is still relatively simple. First, check whether the total number of elements is consistent, and then cut them directly in order.

### 2. Code implementation

The python code is implemented as follows:

class Solution:
def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:
if len(original) != n * m:
return []
res = [original[i:i+n] for i in range(0, n*m, n)]
return res


The submitted code evaluation shows that it takes 956ms and occupies 21.6MB of memory.

## 2. Topic 2

The link of the test questions given in question 2 is as follows:

### 1. Problem solving ideas

For this problem, we first make statistics on the original array, and then investigate each element. If the element is just the beginning of target, we need to consider the following two cases:

1. If the remaining string is the same as the current string, you can bring A n 2 A_n^2 An2 pair s;
2. If the remaining string is different from the current string, it can bring n ⋅ m n\cdot m n ⋅ m pair pairs;

### 2. Code implementation

The python code is implemented as follows:

class Solution:
def numOfPairs(self, nums: List[str], target: str) -> int:
cnt = Counter(nums)
res = 0
for s1 in cnt:
if not target.startswith(s1):
continue
s2 = target[len(s1):]
if s1 != s2:
res += cnt[s1] * cnt[s2]
else:
res += cnt[s1] * (cnt[s1]-1)
return res


The submitted code evaluation shows that it takes 40ms and occupies 14.2MB of memory.

## 3. Topic 3

The test question link for question 3 is as follows:

### 1. Problem solving ideas

This problem is to consider the maximum length that continuous T and continuous F can form respectively.

For any of the above cases, we can use the sliding window to investigate and get the final result.

### 2. Code implementation

The python code is implemented as follows:

class Solution:

def count_max_len(ch):
res = 0
i, j, cnt, rep = 0, 0, 0, 0
while j < n:
cnt += 1
rep += 1
j += 1
while rep > k:
cnt -= 1
rep -= 1
i += 1
res = max(res, cnt)
return res

return max(count_max_len("T"), count_max_len("F"))


The submitted code evaluation shows that it takes 540ms and occupies 14.3MB of memory.

## 4. Topic 4

The test question link given in question 4 is as follows:

### 1. Problem solving ideas

Let's first consider the segmentation method that can be obtained without changing the elements at all. We only need to find the sum s of the elements, then the prefix sum is s 2 \frac{s}{2} The position of 2s ， can be used as the dividing point (the end needs to be removed, because at least one element needs to be guaranteed on both sides).

Next, we consider the impact of replacing element i at a certain position. At this time, the sum of elements is corrected to s − x i + k s - x_i + k s − xi + k, at this time, if the separation point is in the front, then we need to find the preamble and before the element is s − x i + k 2 \frac{s-x_i+k}{2} The number of positions of 2s − xi + k. if the separation point is behind the element, we need to find the post order sum after the element is s − x i + k 2 \frac{s-x_i+k}{2} The number of positions of 2s − xi + k is the number of partition points of the array after modifying the elements at that position.

### 2. Code implementation

The python code is implemented as follows:

class Solution:
def waysToPartition(self, nums: List[int], k: int) -> int:
n, s = len(nums), sum(nums)
res = [0 for _ in range(n)]

cnt = defaultdict(int)
pre = nums
cnt[pre] += 1
for i in range(1, n):
s_prime = s - nums[i] + k
res[i] += cnt[s_prime/2]
pre += nums[i]
cnt[pre] += 1

cnt = defaultdict(int)
post = nums[-1]
cnt[post] += 1
for i in range(n-2, -1, -1):
s_prime = s - nums[i] + k
res[i] += cnt[s_prime/2]
post += nums[i]
cnt[post] += 1

cnt[s] -= 1
res.append(cnt[s/2])

return max(res)


The submitted code evaluation shows that it takes 3533ms and occupies 36.5MB of memory.

Posted by flashbot on Tue, 05 Oct 2021 17:36:05 -0700