100214. 边界上的蚂蚁
链接
3028. 边界上的蚂蚁
class Solution:
def returnToBoundaryCount(self, nums: List[int]) -> int:
return sum(s == 0 for s in accumulate(nums))
class Solution:
def returnToBoundaryCount(self, nums: List[int]) -> int:
left = 0
res = 0
for num in nums:
left += num
if left == 0:
res += 1
return res

class Solution {
public:
int returnToBoundaryCount(vector<int>& nums) {
int location = 0;
int res = 0;
for (int num : nums){
location += num;
if (location == 0){
res += 1;
}
}
return res;
}
};
100204. 将单词恢复初始状态所需的最短时间 I
链接
3029. 将单词恢复初始状态所需的最短时间 I
100203. 将单词恢复初始状态所需的最短时间 II
链接
3031. 将单词恢复初始状态所需的最短时间 II
方法: 最长公共前缀长度 Z 函数 ⟮O(n)⟯

class Solution:
def minimumTimeToInitialState(self, word: str, k: int) -> int:
n = len(word)
z = [0] * n
left = right = 0
for i in range(1, n):
if i <= right:
z[i] = min(z[i - left], right - i + 1)
while i + z[i] < n and word[z[i]] == word[i + z[i]]:
left, right = i, i + z[i]
z[i] += 1
if i % k == 0 and z[i] >= n - i:
return i // k
return (n - 1) // k + 1
class Solution:
def minimumTimeToInitialState(self, word: str, k: int) -> int:
cnt = 1
n = len(word)
while cnt * k < n:
if word[cnt * k:] == word[: n - cnt * k]:
return cnt
cnt += 1
return cnt

class Solution {
public:
int minimumTimeToInitialState(string word, int k) {
int n = word.size();
vector<int> z(n, 0);
int left = 0, right = 0;
for (int i = 1; i < n; ++i){
if (i <= right){
z[i] = min(z[i - left], right - i + 1);
}
while (i + z[i] < n && word[z[i]] == word[i + z[i]]){
left = i;
right = i + z[i];
z[i] += 1;
}
if (i % k == 0 && z[i] >= n - i){
return i / k;
}
}
return (n - 1) / k + 1;
}
};
class Solution:
def sumScores(self, s: str) -> int:
n = len(s)
res = n
z = [0] * n
left = right = 0
for i in range(1, n):
if i <= right:
z[i] = min(z[i - left], right - i + 1)
while i + z[i] < n and s[z[i]] == s[i + z[i]]:
left, right = i, i + z[i]
z[i] += 1
res += z[i]
return res
100189. 找出网格的区域平均强度 ⟮O(9mn)、O(mn)⟯
链接
3030. 找出网格的区域平均强度
class Solution:
def resultGrid(self, image: List[List[int]], threshold: int) -> List[List[int]]:
m, n = len(image), len(image[0])
result = [[0] * n for _ in range(m)]
cnt = [[0] * n for _ in range(m)]
for i in range(2, m):
for j in range(2, n):
ok = True
for row in range(i - 2, i + 1):
if abs(image[row][j - 2] - image[row][j - 1]) > threshold or abs(image[row][j - 1] - image[row][j]) > threshold:
ok = False
break
if not ok: continue
for y in range(j - 2, j + 1):
if abs(image[i - 2][y] - image[i - 1][y]) > threshold or abs(image[i - 1][y] - image[i][y]) > threshold:
ok = False
break
if not ok: continue
avg = sum(image[x][y] for x in range(i - 2, i + 1) for y in range(j - 2, j + 1)) // 9
for x in range(i - 2, i + 1):
for y in range(j - 2, j + 1):
result[x][y] += avg
cnt[x][y] += 1
for i in range(m):
for j in range(n):
if cnt[i][j] == 0:
result[i][j] = image[i][j]
else:
result[i][j] //= cnt[i][j]
return result
class Solution {
public:
vector<vector<int>> resultGrid(vector<vector<int>>& image, int threshold) {
int m = image.size(), n = image[0].size();
vector<vector<int>> res(m, vector<int>(n, 0));
vector<vector<int>> cnt(m, vector<int>(n, 0));
for (int i = 2; i < m; ++i){
for (int j = 2; j < n; ++j){
bool flag = true;
for (int row = i - 2; row <= i; ++row){
if (abs(image[row][j - 2] - image[row][j - 1]) > threshold || abs(image[row][j - 1] - image[row][j]) > threshold){
flag = false;
break;
}
}
if (!flag){
continue;
}
for (int col = j - 2; col <= j; ++col){
if (abs(image[i - 2][col] - image[i - 1][col]) > threshold || abs(image[i - 1][col] - image[i][col]) > threshold){
flag = false;
break;
}
}
if (!flag){
continue;
}
int avg = 0;
for (int x = i - 2; x <= i; ++x){
for (int y = j - 2; y <= j; ++y){
avg+= image[x][y];
}
}
avg /= 9;
for (int x = i - 2; x <= i; ++x){
for (int y = j - 2; y <= j; ++y){
res[x][y] += avg;
cnt[x][y] += 1;
}
}
}
}
for (int i = 0; i < m; ++i){
for (int j = 0; j < n; ++j){
if (cnt[i][j] == 0){
res[i][j] = image[i][j];
}else{
res[i][j] /= cnt[i][j];
}
}
}
return res;
}
};
方法: 二维前缀 + 二维差分 ⟮O(mn)⟯



class Solution:
def possibleToStamp(self, grid: List[List[int]], stampHeight: int, stampWidth: int) -> bool:
m, n = len(grid), len(grid[0])
s = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(m):
for j in range(n):
s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + grid[i][j]
d = [[0] * (n + 2) for _ in range(m + 2)]
for i2 in range(stampHeight, m + 1):
for j2 in range(stampWidth, n + 1):
i1 = i2 - stampHeight + 1
j1 = j2 - stampWidth + 1
if s[i2][j2] - s[i2][j1-1] - s[i1- 1][j2] + s[i1 - 1][j1 - 1] == 0:
d[i1][j1] += 1
d[i1][j2 + 1] -= 1
d[i2 + 1][j1] -= 1
d[i2 + 1][j2 + 1] += 1
for i in range(m):
for j in range(n):
d[i + 1][j + 1] += d[i + 1][j] + d[i][j + 1] - d[i][j]
if grid[i][j] == 0 and d[i + 1][j + 1] == 0:
return False
return True

class Solution {
public:
bool possibleToStamp(vector<vector<int>>& grid, int stampHeight, int stampWidth) {
int m = grid.size(), n = grid[0].size();
vector<vector<int>> s(m + 1, vector<int>(n + 1, 0));
for (int i = 0; i < m; ++i){
for (int j = 0; j < n; ++j){
s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + grid[i][j];
}
}
vector<vector<int>> d(m + 2, vector<int>(n + 2, 0));
for (int i2 = stampHeight; i2 <= m; ++i2){
for (int j2 = stampWidth; j2 <= n; ++j2){
int i1 = i2 - stampHeight + 1;
int j1 = j2 - stampWidth + 1;
if (s[i2][j2] - s[i2][j1- 1] - s[i1- 1][j2] + s[i1 - 1][j1 - 1] == 0){
d[i1][j1] += 1;
d[i1][j2 + 1] -= 1;
d[i2 + 1][j1] -= 1;
d[i2 + 1][j2 + 1] += 1;
}
}
}
for (int i = 0; i < m; ++i){
for (int j = 0; j < n; ++j){
d[i + 1][j + 1] += d[i + 1][j] + d[i][j + 1] - d[i][j];
if (grid[i][j] == 0 && d[i + 1][j + 1] == 0){
return false;
}
}
}
return true;
}
};