leetcode 1337. The K Weakest Rows in a Matrix(矩阵中最弱的K行)

LeetCode --- 1337. The K Weakest Rows in a Matrix 解题报告 【代码】LeetCode --- 1337. The K Weakest Rows in a Matrix 解题报告。 阅读详情

You are given an m x n binary matrix mat of 1’s (representing soldiers) and 0’s (representing civilians). The soldiers are positioned in front of the civilians. That is, all the 1’s will appear to the left of all the 0’s in each row.

A row i is weaker than a row j if one of the following is true:

The number of soldiers in row i is less than the number of soldiers in row j.
Both rows have the same number of soldiers and i < j.
Return the indices of the k weakest rows in the matrix ordered from weakest to strongest.

Example 1:

Input: mat =
[[1,1,0,0,0],
[1,1,1,1,0],
[1,0,0,0,0],
[1,1,0,0,0],
[1,1,1,1,1]],
k = 3
Output: [2,0,3]
Explanation:
The number of soldiers in each row is:

  • Row 0: 2
  • Row 1: 4
  • Row 2: 1
  • Row 3: 2
  • Row 4: 5
    The rows ordered from weakest to strongest are [2,0,3,1,4].

给一个矩阵,元素只有0 和 1,且 1 都在 0 的前面,
按元素和最小的行数排序,取出前k个行数。

思路:
因为最多100行,每行只有0和1元素,所以每行的和最多是100(n <= 100)。
可以建立一个数组,下标就是每行的和。

每个数组里保存 对应和 的行数,
最后遍历这个数组,依次取出前k个行数即可。

直接对每行求和,运行出来是6ms,可以从哪里改进。
注意每行有个重要的特征,那就是1一定出现在0的前面,
所以,求和也就是找1和0的分界线,可以用binary search。

//0ms
    public int[] kWeakestRows(int[][] mat, int k) {
        ArrayList<Integer>[] sum = new ArrayList[101];
        int[] result = new int[k];
        int cnt = 0;
        
        for(int i = 0; i < mat.length; i ++) {
            int[] row = mat[i];
            //求每行的和
            int left = 0, right = row.length - 1; 
            while(left <= right){
                int mid = left + (right - left) / 2;
                if (row[mid] == 1) left = mid + 1;
                else right = mid - 1;
            }
            if(sum[left] == null) sum[left] = new ArrayList<Integer>();
            sum[left].add(i);
        }
        
        
        for(int i = 0; i < 101; i ++) {
            if(sum[i] == null) continue;
            
            for(int j = 0; j < sum[i].size(); j++) {
                if(cnt >= k) return result;
                result[cnt] = sum[i].get(j);
                cnt ++;
            }
        }
        return result;
    }
【模拟电子设计实战】开源!基于Multisim与AD620的心电采集电路仿真与优化 本文详细介绍了基于Multisim与AD620的心电采集电路仿真与优化实战。通过分析心电信号采集的挑战,重点讲解了AD620仪表放大器和OP07运算放大器的选型优势及电路设计技巧,包括对抗50Hz工频干扰和基线漂移的优化方法。文章还分享了Multisim仿真参数设置与实测对比经验,最终实现性能达标的心电采集电路设计。 阅读详情

相关推荐

openEuler Embedded新增软件包及在线安装软件方法

openEuler Embedded是基于Yocto构建的嵌入式操作系统,默认不包含dnf/apt软件包管理器,主要使用rpm格式。新增软件需通过manifest文件检查是否集成,未集成则需适配新配方。对于在线安装需求,可采用轻量级容器方案:通过isulad运行openEuler服务器版容器镜像,在容器内使用dnf命令安装软件(如Nginx),实现服务器生态兼容。部署前提包括设备支持、存储空间(≥10GB)和网络连接,需先安装并启动isulad服务。这种方法结合了嵌入式系统的轻量化特性和服务器系统的软件管理

专注嵌入式开发与 Linux 系统应用实战,分享芯片驱动、设备调试、系统工具开发干货。 1225

矩阵中战斗力最弱的 K 行(Java算法每日一题)

问: 给你一个大小为 m * n 的矩阵 mat,矩阵由若干军人和平民组成,分别用 1 和 0 表示。 请你返回矩阵中战斗力最弱的 k 行的索引,按从最弱到最强排序。 如果第 i 行的军人数量少于第 j 行,或者两行军人数量相同但 i 小于 j,那么我们认为第 i 行的战斗力比第 j 行弱。 军人 总是 排在一行中的靠前位置,也就是说 1 总是出现在 0 之前。 例: 输入:mat = [[1,1,0,0,0], [1,1,1,1,0], [1,0,0,0,0], [1,1,0,0,0], [1,1,1,1

wanjialin的博客 423

一款值得关注的开源企业管理ERP系统

盒木ERP是一款开源的现代化企业管理系统,采用前后端分离架构,后端基于Python+Django,前端使用Vue+Ant Design。系统涵盖采购、销售、库存、财务等核心业务模块,支持移动端操作和PDA扫码。具有开源免费、技术先进、功能完善等特点,适合中小企业使用或二次开发。项目提供详细部署指南,支持Docker容器化部署,是学习ERP系统开发和企业数字化转型的优秀选择。

1103

1337. The K Weakest Rows in a Matrix

class Solution { public: vector<int> kWeakestRows(vector<vector<int>>& mat, int k) { vector<int> cnt; for(int i=0;i<mat.size();i++){ int ...

zeroQiaoba的博客 283

5nm制程的CV5芯片实测:用它给无人机做8K拍摄和实时避障,功耗和性能到底怎么样?

本文深度评测了5nm制程的CV5芯片在无人机8K拍摄和实时避障中的表现。通过实测数据展示了CV5的异构计算架构、8K视频编码性能和CVflow®AI引擎的加速效果,同时提供了开发中的优化技巧和散热建议,为无人机开发者提供了实用的参考。

weixin_30861797的博客 358

ArcGIS小白必看:如何用3D Analyst工具将二维点线秒变三维模型(附详细步骤)

本文为ArcGIS初学者提供了利用3D Analyst工具将二维点、线数据快速转换为三维模型的完整指南。详细讲解了基于属性字段和数字高程模型(DEM)两种核心方法,并附上从数据准备、工具操作到三维场景可视化与问题排查的详细步骤,帮助用户轻松实现从平面到立体的空间数据建模。

nft7creator的博客 139

1337. The K Weakest Rows in a Matrix*

1337. The K Weakest Rows in a Matrix* https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/ 题目描述 Given a m * n matrix mat of ones (representing soldiers) and zeros (representing civilians), re...

珍妮的选择的博客 475

告别JSON!Anthropic发布Tool Calling 2.0,Agent上下文消耗骤降80%

Anthropic发布ToolCalling 2.0更新,彻底革新AI Agent开发模式。四大核心特性:1)程序化工具调用,允许模型直接输出可执行代码替代JSON交互,降低30-50%Token消耗;2)动态网页过滤,自动清洗网页内容减少24%无效信息;3)工具搜索机制,按需加载工具定义,最高优化80%上下文窗口;4)工具使用示例,通过Few-Shot示范提升复杂表单处理准确率至90%。这一升级将Agent范式从"大模型作为路由"推进到"大模型作为引擎",但需注意代

老孙 | Ethan Sun 的博客:AI 智能体与 AI 原生软件的工程实践 793

leetcode 1337. The K Weakest Rows in a Matrix(python)

描述 Given a m * n matrix mat of ones (representing soldiers) and zeros (representing civilians), return the indexes of the k weakest rows in the matrix ordered from the weakest to the strongest. A row i is weaker than row j, if the number of soldiers in row

王大呀呀的博客 384

头歌操作系统 课堂练习4.2:页式内存管理 答案 无解析

头歌操作系统 课堂练习4.2:页式内存管理 答案 无解析

kercii的博客 1万+

Arduino ESP32 Web服务器从microSD卡读取

Arduino ESP32 Web服务器从microSD卡读取 将网页内容放到SD卡中,当有客户端数据请求时,从SD卡读取信息进行发送,比起SPIFFS文件系统,更节约板上的硬件资源。 SD卡接线说明 ESP32----- SD D5(GPIO 5)-----CS D18(GPIO 18)-----SCK D19(GPIO 19)-----MISO D23()GPIO 23-----MOSI 文件放置 需要把文件放置到SD卡的根目录下。注意SD卡的格式FAT32格式。 实例代码 /*

perseverance51博客 1433

LeetCode-1337. The K Weakest Rows in a Matrix [C++][Java]

Returnthe indices of thekweakestrows in the matrix ordered from weakest to strongest.

贫道绝缘子的博客 425

手把手教你学Simulink--控制执行场景实例:基于Simulink的智能车辆自动紧急制动(AEB)仿真

本文完成了基于 Simulink 的智能车辆自动紧急制动(AEB),实现了:✅ 构建前车急刹与静止障碍物典型场景✅ 设计基于 TTC 的三级 AEB 决策逻辑✅ 验证在高风险场景下有效避免碰撞✅ 掌握从风险评估到制动执行的完整安全链✅ 为ADAS 主动安全功能开发奠定基础。

MHD0815的博客 235

【LeetCode】No.1337. The K Weakest Rows in a Matrix -- Java Version

【LeetCode】No.1337. The K Weakest Rows in a Matrix -- Java Version

汤姆lazy的优快云博客 595

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

[leetcode] 1337. The K Weakest Rows in a Matrix

Description Given a m * n matrix mat of ones (representing soldiers) and zeros (representing civilians), return the indexes of the k weakest rows in the matrix ordered from the weakest to the strongest. A row i is weaker than row j, if the number of soldie

Learning from the mistakes 237

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

LeetCode-1341. The K Weakest Rows in a Matrix

Given am* nmatrixmatofones(representing soldiers) andzeros(representing civilians), return the indexes of thekweakest rows in the matrix ordered from the weakest to the strongest. A rowi...

reigns的博客 460

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

leetcode 1337. The K Weakest Rows in a Matrix

题目 这道题可以用map来实现,记录顺序与下标之间的关系,但是有重复的情况,还要用到list,太麻烦了。我想的是直接拼接字符串,重写排序。 class Solution { public int[] kWeakestRows(int[][] mat, int k) { String[] s = new String[mat.length]; for(int ...

xiaobailaji的博客 165

LeetCode 1337. 方阵中战斗力最弱的 K 行(优先队列)

1. 题目 给你一个大小为 m * n 的方阵 mat,方阵由若干军人和平民组成,分别用 0 和 1 表示。 请你返回方阵中战斗力最弱的 k 行的索引,按从最弱到最强排序。 如果第 i 行的军人数量少于第 j 行,或者两行军人数量相同但 i 小于 j,那么我们认为第 i 行的战斗力比第 j 行弱。 军人 总是 排在一行中的靠前位置,也就是说 1 总是出现在 0 之前。 示例 1: 输入:mat =...

Michael是个半路程序员 1285

(c语言)LeetCode1337. The K Weakest Rows in a Matrix

(力扣) leetcode 1337题 :矩阵中战斗力最弱的 K 行,使用二分查找和快速排序,以及二维数组存储。

墨染 286

LeetCode(Heap)1337. The K Weakest Rows in a Matrix

LeetCode(Heap)1337. The K Weakest Rows in a Matrix

weixin_45428910的博客 186

LeetCode 1337. The K Weakest Rows in a Matrix【数组,二分,堆,快速选择,排序】1224

本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。为了方便在PC上运行调试、分享代码文件,我还建立了相关的。

memcpy0的博客 352

LeetCode 1337. The K Weakest Rows in a Matrix

LeetCode 1337. The K Weakest Rows in a Matri5x (Easy) 主要知识点:pq、binary search;优先级:5

温迪今天刷题了吗 323

【Leetcode】1337. The K Weakest Rows in a Matrix

题目地址: https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/ 给定一个mmm行nnn列的矩阵,每行只有000和111,并且任意一个111都在任意一个000的左边。再给定一个正整数kkk,要求返回前kkk个111的个数最小的行的下标,如果有并列则按行下标从小到大排列。 代码如下: import java.util.Arrays; public class Solution { public int[] kWeakestRows

数学、算法爱好者的博客 263

1337. The K Weakest Rows in a Matrix[Easy](Leetcode每日一题-2021.08.01)

Problem You are given an m x n binary matrix mat of 1’s (representing soldiers) and 0’s (representing civilians). The soldiers are positioned in front of the civilians. That is, all the 1’s will appear to the left of all the 0’s in each row. A row i is wea

Bryan要加油 665

LeetCode知识点总结 - 133

LeetCode 1337. The K Weakest Rows in a Matrix 考点 难度 Sorting Easy 题目 You are given an m x n binary matrix mat of 1’s (representing soldiers) and 0’s (representing civilians). The soldiers are positioned in front of the civilians. That is, all the

m0_59773145的博客 162

LeetCode-Python-1341. 方阵中战斗力最弱的 K 行(排序)

给你一个大小为m* n的方阵mat,方阵由若干军人和平民组成,分别用 0 和 1 表示。 请你返回方阵中战斗力最弱的k行的索引,按从最弱到最强排序。 如果第i行的军人数量少于第j行,或者两行军人数量相同但 i 小于 j,那么我们认为第 i 行的战斗力比第 j 行弱。 军人 总是 排在一行中的靠前位置,也就是说 1 总是出现在 0 之前。 示例 1: 输入:mat...

Keep Coding 546
上一篇: numpy.zeros shape start with 0
下一篇: pytorch F.grid_sample用法
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值