当前位置: 首页 > news >正文

C语言 | Leetcode C语言题解之第329题矩阵中的最长递增路径

题目:

题解:

const int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int rows, columns;typedef struct point {int x, y;
} point;int longestIncreasingPath(int** matrix, int matrixSize, int* matrixColSize) {if (matrixSize == 0 || matrixColSize[0] == 0) {return 0;}rows = matrixSize;columns = matrixColSize[0];int** outdegrees = (int**)malloc(sizeof(int*) * rows);for (int i = 0; i < rows; i++) {outdegrees[i] = (int*)malloc(sizeof(int) * columns);memset(outdegrees[i], 0, sizeof(int) * columns);}for (int i = 0; i < rows; ++i) {for (int j = 0; j < columns; ++j) {for (int k = 0; k < 4; ++k) {int newRow = i + dirs[k][0], newColumn = j + dirs[k][1];if (newRow >= 0 && newRow < rows && newColumn >= 0 && newColumn < columns && matrix[newRow][newColumn] > matrix[i][j]) {++outdegrees[i][j];}}}}point* q = (point*)malloc(sizeof(point) * rows * columns);int l = 0, r = 0;for (int i = 0; i < rows; ++i) {for (int j = 0; j < columns; ++j) {if (outdegrees[i][j] == 0) {q[r++] = (point){i, j};}}}int ans = 0;while (l < r) {++ans;int size = r - l;for (int i = 0; i < size; ++i) {point cell = q[l++];int row = cell.x, column = cell.y;for (int k = 0; k < 4; ++k) {int newRow = row + dirs[k][0], newColumn = column + dirs[k][1];if (newRow >= 0 && newRow < rows && newColumn >= 0 && newColumn < columns && matrix[newRow][newColumn] < matrix[row][column]) {--outdegrees[newRow][newColumn];if (outdegrees[newRow][newColumn] == 0) {q[r++] = (point){newRow, newColumn};}}}}}return ans;
}
http://www.lryc.cn/news/418457.html

相关文章:

  • rabbitmq学习记录
  • MySQL数据库基础:约束
  • Java设计模式和AOP编程
  • 【扒代码】data.py
  • 【时时三省】unity test 测试框架 介绍(适用于C语言进行测试的)
  • 那些你应该掌握的linux命令
  • 系统出现高CPU可能风险因素整理
  • 前端技术 -- 动画效果之GSAP作用与使用示例
  • C口一拖二数据线:解锁数字生活的便捷新篇章LDR6020
  • CH07_数据绑定
  • 24.python基础(8.8)
  • 【论文阅读】MobileNetV4 - Universal Models for the Mobile Ecosystem
  • 大模型日报 2024-08-07
  • 区块链ddos防护怎么做
  • 在Linux中认识pthread库
  • LVS 负载均衡
  • 在Excel中启用宏 (~ ̄▽ ̄)~
  • 连接投影仪/显示器只能扩展不能复制的解决方案
  • 数据库基础知识
  • java JVM 锁消除
  • 基于 Java Supplier与Predicate 封装自动重试机制通用接口
  • Java面试题(基础篇)②
  • 【docker快捷部署系列二】用docker-compose快速配置多个容器,docker部署Springboot+Vue项目和mysql数据库
  • Java新手指南:从菜鸟到编程大师的趣味之路-类和对象
  • 计算机毕业设计选题推荐-房屋租赁系统-Java/Python项目实战
  • LeetCode 3131.找出与数组相加的整数 I:最小值之差(多语言一行版)
  • Win32注册表操作
  • 白骑士的PyCharm教学高级篇 3.3 Web开发支持
  • SpringAOP-底层实现源码解析
  • 【C语言初阶】C语言操作符全攻略:提升编程效率的关键步骤