首页 > 其他分享 >【leetcode 找出第 K 大的异或坐标值]

【leetcode 找出第 K 大的异或坐标值]

时间:2024-05-26 20:33:28浏览次数:27  
标签:matrix int transform value col 异或 坐标值 leetcode row

前缀和 + 最小堆


import java.util.PriorityQueue;

class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.kthLargestValue(new int[][]{
                {5, 2}, {1, 6}
        }, 4);
    }

    public int kthLargestValue(int[][] matrix, int k) {
        int m = matrix.length;
        int n = matrix[0].length;

        // row
        int[][] rowXor = new int[m][n];
        for (int i = 0; i < m; i++) {
            rowXor[i][0] = matrix[i][0];
            for (int j = 1; j < n; j++) {
                rowXor[i][j] = rowXor[i][j - 1] ^ matrix[i][j];
            }
        }
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<Integer>(k) {
            @Override
            public boolean add(Integer value) {
                if (size() < k) {
                    return super.add(value);
                } else {
                    int kThNum = peek();
                    if (kThNum < value) {
                        poll();
                        super.add(value);
                    }
                    return true;
                }
            }
        };

        int[][] transform = new int[m][n];
        int value = matrix[0][0];
        transform[0][0] = value;
        priorityQueue.add(value);
        for (int col = 1; col < n; col++) {
            transform[0][col] = transform[0][col - 1] ^ matrix[0][col];
            priorityQueue.add(transform[0][col]);
        }
        for (int row = 1; row < m; row++) {
            for (int col = 0; col < n; col++) {
                transform[row][col] = transform[row - 1][col] ^ rowXor[row][col];
                priorityQueue.add(transform[row][col]);
            }
        }
        return priorityQueue.peek();
    }
}

标签:matrix,int,transform,value,col,异或,坐标值,leetcode,row
From: https://www.cnblogs.com/fishcanfly/p/18214246

相关文章

  • 【leetcode 399 周赛】【题解】
    第一题和第三题一样。就是求约数第二题就是模拟第4题使用线段树1,3题代码实际上发现没有下面代码的负载,比如:a*b=n,枚举a就好,a在[1,sqrt(n)内。importjava.util.*;classSolution{publicintnumberOfPairs(int[]nums1,int[]nums2,intk){......
  • 【Leetcode 每日一题】28. 找出字符串中第一个匹配项的下标
    给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标(下标从0开始)。如果 needle 不是 haystack 的一部分,则返回  -1 。示例1:输入:haystack="sadbutsad",needle="sad"输出:0解释:"sad"在下标0和6......
  • leetcode力扣 1004. 最大连续1的个数 III
    给定一个二进制数组nums和一个整数k,如果可以翻转最多k个0,则返回数组中连续1的最大个数。示例1:输入:nums=[1,1,1,0,0,0,1,1,1,1,0],k=2输出:6解释:[1,1,1,0,0,1,1,1,1,1,1],翻转两个0后,最长的子数组长度为6。示例2:输入:nums=[0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1......
  • 地下城游戏(leetcode)
    个人主页:Lei宝啊 愿所有美好如期而遇地下城游戏https://leetcode.cn/problems/dungeon-game/description/图解+分析:代码classSolution{public:intcalculateMinimumHP(vector<vector<int>>&vv){introw=vv.size(),col=vv[0].size();......
  • 【找出第 K 大的异或坐标值】python
    4层循环暴力超时 classSolution:defkthLargestValue(self,matrix:List[List[int]],k:int)->int:nums=[]forainrange(len(matrix)):forbinrange(len(matrix[0])):num=0foriinrange(......
  • [LeetCode] 2903. Find Indices With Index and Value Difference I
    Youaregivena0-indexedintegerarraynumshavinglengthn,anintegerindexDifference,andanintegervalueDifference.Yourtaskistofindtwoindicesiandj,bothintherange[0,n-1],thatsatisfythefollowingconditions:abs(i-j)>=index......
  • leetcode力扣 2024. 考试的最大困扰度
    一位老师正在出一场由n道判断题构成的考试,每道题的答案为true(用'T'表示)或者false(用'F'表示)。老师想增加学生对自己做出答案的不确定性,方法是最大化有连续相同结果的题数。(也就是连续出现true或者连续出现false)。给你一个字符串answerKey,其中answerKey[i]是第i......
  • leetcode力扣 213. 打家劫舍 II
    计划偷窃沿街的房屋是小偷的计划。在这个地方,所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的。但是,相邻的房屋都装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。为了计算在不触动警报装置的情况下,今晚能够偷窃到的最高金额,我们......
  • LeetCode/NowCoder-链表经典算法OJ练习4
    ·人的才华就如海绵的水,没有外力的挤压,它是绝对流不出来的。流出来后,海绵才能吸收新的源泉。......
  • LeetCode //C - 119. Pascal‘s Triangle II
    119.Pascal’sTriangleIIGivenanintegerrowIndex,returntherowIndexth(0-indexed)rowofthePascal’striangle.InPascal’striangle,eachnumberisthesumofthetwonumbersdirectlyaboveitasshown: Example1:Input:rowIndex=3Outpu......