2022-12-11 第 323 场周赛

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);
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值