other 1878

news/2025/2/24 3:53:14/文章来源:https://www.cnblogs.com/cynrjy/p/18566678
1878. Get Biggest Three Rhombus Sums in a Grid

You are given an m x n integer matrix grid​​​.

A rhombus sum is the sum of the elements that form the border of a regular rhombus shape in grid​​​. The rhombus must have the shape of a square rotated 45 degrees with each of the corners centered in a grid cell. Below is an image of four valid rhombus shapes with the corresponding colored cells that should be included in each rhombus sum:

Note that the rhombus can have an area of 0, which is depicted by the purple rhombus in the bottom right corner.

Return the biggest three distinct rhombus sums in the grid in descending order. If there are less than three distinct values, return all of them.

 

Example 1:

Input: grid = [[3,4,5,1,3],[3,3,4,2,3],[20,30,200,40,10],[1,5,5,4,1],[4,3,2,2,5]]
Output: [228,216,211]
Explanation: The rhombus shapes for the three biggest distinct rhombus sums are depicted above.
- Blue: 20 + 3 + 200 + 5 = 228
- Red: 200 + 2 + 10 + 4 = 216
- Green: 5 + 200 + 4 + 2 = 211

Example 2:

Input: grid = [[1,2,3],[4,5,6],[7,8,9]]
Output: [20,9,8]
Explanation: The rhombus shapes for the three biggest distinct rhombus sums are depicted above.
- Blue: 4 + 2 + 6 + 8 = 20
- Red: 9 (area 0 rhombus in the bottom right corner)
- Green: 8 (area 0 rhombus in the bottom middle)

Example 3:

Input: grid = [[7,7,7]]
Output: [7]
Explanation: All three possible rhombus sums are the same, so return [7].

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 50
  • 1 <= grid[i][j] <= 105
class Solution {int m;int n;TreeSet<Integer> list = new TreeSet<>(); public int[] getBiggestThree(int[][] grid) {m = grid.length;n = grid[0].length;// 计算半径int len = Math.min(m, n) / 2;// for radius 0~lenfor(int radius = 0; radius <= len; radius++) {// center start sx = radius  sy = radiusint sx = radius, sy = radius;// center end ex = m - radius, ey = n - radiusint ex = m - radius, ey = n - radius;// i in sx~exfor(int i = sx; i <= ex; i++) {for(int j = sy; j <= ey; j++) {int sum = calculate(grid, i, j, radius);if(sum >= 0) list.add(sum);}}}int size = Math.min(list.size(), 3);int[] result = new int[size];for(int i = 0; i < size; i++) {result[i] = list.pollLast();}return result;}// 对以(x, y)为中心,radius为半径的菱形求sumprivate int calculate(int[][] grid, int x, int y, int radius) {// 上下左右4个角int top = x - radius, bottom = x + radius, left = y - radius, right = y + radius;// 超出范围校验if(top < 0 || bottom >= m || left < 0 || right >= n) return -1;// radius为0的直接返回if(radius == 0) return grid[x][y];int sum = 0;//对于4条边分别进行计算// left topfor(int i = 0; i <= radius; i++) {sum += grid[x - i][left + i];}// right top for(int i = 0; i <= radius; i++) {sum += grid[x - i][right - i];}// left bottomfor(int i = 0; i <= radius; i++) {sum += grid[x + i][left + i];}// right top for(int i = 0; i <= radius; i++) {sum += grid[x + i][right - i];}// 四个角多算了一次,要减去return sum - grid[x][left] - grid[x][right] - grid[top][y] - grid[bottom][y];}
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/840651.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ue5.3的game play effect 添加gameplay tags的操作变化

ue5.3的game play effect默认界面没用各种tags,得在Component这里Add element,然后选想要的tag,如图所示

HCIA-08 以太网交换基础

介绍以太网协议的相关概念、MAC地址的类型、二层交换机的工作流程以及二层交换机的工作原理。目录 1-以太网协议:冲突域&广播域 2-以太网帧 2.1 MAC&IP 2.2 帧类型 以太网交换机 同网段通信全过程 1-以太网协议:冲突域&广播域 以太网是当今现有局域网(Local Are…

计算机常识——零拷贝

前言 什么是零拷贝技术? 首先计算机不存在什么真的零拷贝技术,这点是确认的。 零拷贝值得是减少多余的拷贝的意思。 正文 首先如果我们要传输文件是怎么处理的呢? 当需要从磁盘读取数据到内存时,‌CPU会发出指令通知硬盘控制器进行读取操作。‌ 此后,‌CPU可以执行其他任务…

编译 App 工程

Android Studio 跟 IDEA 一样,被改动的文件会自动保存,无须开发者手工保存。它还会自动编译最新的代码,如果代码有误,编辑界面会标红提示出错了。但是有时候可能因为异常关闭的缘故,造成 Android Studio 的编译文件发生损坏,此时需要开发者手动重新编译,手动编译有以下 …

华为交换机简单配置方法

ARP地址解析 二层交换机图示在二层交换机内主机第一次ping对方以后,即第一次发ARP广播,交换机记录双方的mac ip对应地址地址表,后续再交换数据,变成单播display mac-address 查看mac地址表 划分vlan(虚拟局域网)创建vlan 10 20 2个 先把pc1 pc2 pc3 连接的交换机上的端…

RHEL9.4上使用apache搭建http服务器提供repo源

时间:2024.11.24 计划:使用apache搭建HTTP(Hypertext Transfer Protocol)服务器,共享iso镜像为环境内其他主机提供repo(repository)源 参照:马哥教育王老师课程 基于Linux系统的本地Yum源搭建与配置(ISO方式、RPM方式) https://developer.aliyun.com/article/1356520 如何…

JVM常见面试题(四):垃圾回收

堆区域划分,对象什么时候可以被垃圾器回收,如何定位垃圾——引用计数法、可达性分析算法,JVM垃圾回收算法——标记清除算法、标记整理算法、复制算法、分代回收算法;JVM垃圾回收器——串行、并行、CMS垃圾回收器、G1垃圾回收器;强引用、软引用、弱引用、虚引用文章目录 前…

类和对象综合案例——模仿电影信息系统

1.需求2.实际操作 1.创建一个实体类 首先第一件事,就是写好一个实体类,为后面封装所有数据做准备,我们只需要私有化成员变量,然后再ptg即可2.创建处理业务类和测试类 写好了实体类就要开始处理业务了,所以我们要再创建一个专门处理业务的类,名为MovieService 写完了业务类…

CF2038A - Bonus Project 题解

题目传送门 https://codeforces.com/contest/2038/problem/A 先大致捋一下题目的含义 一共有n个工程师,每个工程师完成相应的工作都有一定的奖金a,但同时也会消耗成本b,目前一共有k个工作需要做 这些工程师对他们的同事很友好,他们能接受自己的总收益为0来增长经验,但不能…

BurpSuite安装captcha-killer插件

1.本机已经安装python3.8或者更高版本,使用命令进行更新pip和安装环境模块 python3 -m pip install --upgrade pip pip3 install ddddocr aiohttp -i https://pypi.tuna.tsinghua.edu.cn/simple/ (因为本机安装了python2和python3,所以输入pip3)2.bp导入插件包,低版本导…

Perf Linux性能事件(性能计数)器 与 Flame Graph

from ふぃーる 冬コミ2日目西ふ15Perf 性能采样和计数原理 首先要清楚perf是一个面向事件的可观察性工具from jyy perf在中断来临时,获取OS在中断之前所记录的关键性能指标Perf Stat (性能计数)stat (statistics) 有统计,计数,获取信息等含义perf stat <command&…

技术债正在悄悄拖垮你的团队!

0 前言 软件开发的核心在于应对变化。在软件的生命周期中,目标是能够在合理的时间内实施必要的更改。不管这些更改是技术性的,比如紧急安全升级,还是业务需求所驱动的,比如开发新功能以在目标市场中更具竞争力——能否快速应对变化是成败的关键。 是什么让我们慢下来?通常…