6257. 删除每行中的最大值
class Solution {
public int deleteGreatestValue(int[][] grid) {
int m = grid.length, n = grid[0].length;
int ans = 0;
for (int i = 0; i < n; i++) {
int add = 0;
for (int j = 0; j < m; j++) {
int max = 0, idx = 0;
for (int k = 0; k < n; k++) {
if (grid[j][k] < 0) {
continue;
}
idx = Math.max(max,grid[j][k]) == max ? idx : k;
max = Math.max(max,grid[j][k]) == max ? max : grid[j][k];
}
add = Math.max(add,max);
grid[j][idx] = -1;
}
ans += add;
}
return ans;
}
}
6258. 数组中最长的方波
哈希+递归
class Solution {
public int longestSquareStreak(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int num : nums) {
set.add(num);
}
int ans = 0;
for (int num : nums) {
ans = Math.max(ans,dfs(num,set));
}
return ans > 0 ? ans +1 : -1;
}
public int dfs(int num, Set<Integer> set) {
// if (num * num < 0) {
// return 0;
// }
if (!set.contains(num * num)) {
return 0;
}
return 1 + dfs(num * num,set);
}
}
6259. 设计内存分配器
class Allocator {
int[] nums;
public Allocator(int n) {
nums = new int[n];
}
public int allocate(int size, int mID) {
int n = nums.length, ans = -1;
for (int i = 0; i < n; i++) {
int t = size - 1;
while (i + t < n && t >= 0 && nums[i+t] == 0) {
t--;
}
if (t == -1) {
ans = i;
for (; i < ans+size; i++) {
nums[i] = mID;
}
return ans;
}
}
return ans;
}
public int free(int mID) {
int n = nums.length, ans = 0;
for (int i = 0; i < n; i++) {
if (nums[i] == mID) {
nums[i] = 0;
ans++;
}
}
return ans;
}
}
/**
* Your Allocator object will be instantiated and called as such:
* Allocator obj = new Allocator(n);
* int param_1 = obj.allocate(size,mID);
* int param_2 = obj.free(mID);
*/