2024年4月13日美团春招实习试题【第一题:好子矩阵】-题目+题解+在线评测【模拟】

news/2024/9/23 14:35:48/

2024年4月13日美团春招实习试题【第一题:好子矩阵】-题目+题解+在线评测【模拟】

  • 题目描述:
    • 输入描述
    • 输出描述
    • 样例
  • 解题思路一:模拟
  • 解题思路二:思路二
  • 解题思路三:直接判断

题目描述:

塔子哥定义一个矩阵是”好矩阵”,当且仅当该矩阵所有元素都相同。 现在塔子哥拿到了一个矩阵,她想知道该矩阵有多少2*2的子知阵是好矩阵?

输入描述

第一行输入两个正整数m和n,代表输入矩阵的行数和列数。

接下来的n行,每行输入m个正整数a(i,j),代表塔子哥拿到的矩阵

1<=n,m<=100

1 < = a ( i , j ) < = 1 0 9 1<=a_{(i,j)}<=10^9 1<=a(i,j)<=109

输出描述

2*2好子矩阵的数量

样例

输入

3 3
1 2 1
1 1 1
1 1 3

输出

1

说明

只有左下角一个好子矩阵

OJ链接:
https://codefun2000.com/p/P1819

解题思路一:模拟

m, n = map(int, input().split())
matrix = [[0] * n for _ in range(m)]
for i in range(m):row = list(map(int, input().split()))matrix[i] = row
def goodMatrix(matrix, x, y):t = matrix[x][y]if t == matrix[x+1][y] and t == matrix[x+1][y+1] and t == matrix[x][y+1]:return Truereturn Falsecnt = 0
for i in range(m-1):for j in range(n-1):if goodMatrix(matrix, i, j):cnt += 1
print(cnt)

时间复杂度:O(nm)
空间复杂度:O(1)

解题思路二:思路二

import collectionsn, m = map(int, input().split())
mat = list()
for _ in range(n):mat.append(list(map(int, input().split())))bad_start = collections.defaultdict(list)
ret = 0for i in range(n-1):for j in range(m-1):if j not in bad_start[i] and mat[i][j+1] == mat[i][j]:if mat[i+1][j+1] != mat[i][j+1]:bad_start[i].append(j+1)else:if mat[i+1][j] != mat[i+1][j+1]:bad_start[i+1].append(j)else:ret += 1print(ret)

时间复杂度:O(nm)
空间复杂度:O(1)

解题思路三:直接判断

n, m = map(int, input().split())
g = [list(map(int, input().split())) for _ in range(n)]
ans = 0
for i in range(n - 1):for j in range(m - 1):if g[i][j] == g[i + 1][j] and g[i + 1][j] == g[i][j + 1] and g[i][j + 1] == g[i + 1][j + 1]:ans += 1
print(ans)# python
n,m = map(int,input().split())
matrix=[]
for _ in range(n):row=list(map(int, input().split()))matrix.append(row)
count=0
for i in range(n-1):for j in range(m-1):if matrix[i][j] == matrix[i+1][j]==matrix[i][j+1]==matrix[i+1][j+1]:count+=1
print(count)# java
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();int m = scanner.nextInt();int[][] g = new int[n][m];for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {g[i][j] = scanner.nextInt();}}int ans = 0;for (int i = 0; i < n - 1; i++) {for (int j = 0; j < m - 1; j++) {if (g[i][j] == g[i + 1][j] && g[i + 1][j] == g[i][j + 1] && g[i][j + 1] == g[i + 1][j + 1]) {ans++;}}}System.out.println(ans);scanner.close();}
}# c++
#include <iostream>
#include <vector>using namespace std;int main() {int n, m;cin >> n >> m;vector<vector<int>> g(n, vector<int>(m));for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {cin >> g[i][j];}}int ans = 0;for (int i = 0; i < n - 1; i++) {for (int j = 0; j < m - 1; j++) {if (g[i][j] == g[i + 1][j] && g[i + 1][j] == g[i][j + 1] && g[i][j + 1] == g[i + 1][j + 1]) {ans++;}}}cout << ans << endl;return 0;
}

时间复杂度:O(nm)
空间复杂度:O(1)


http://www.ppmy.cn/news/1428547.html

相关文章

四.RocketMQ的几种消息发送方式应用

RocketMQ的几种消息发送方式应用 一&#xff1a;普通消息1&#xff09;发送同步消息2&#xff09;发送异步消息3&#xff09;单向发送消息4&#xff09;消费消息-负载均衡模式5&#xff09;消费消息-广播模式 二&#xff1a;顺序消息1.顺序消息指的是:严格按照消息的发送顺序进…

ubuntu安装QEMU

qemu虚拟机的使用&#xff08;一&#xff09;——ubuntu20.4安装QEMU_ubuntu安装qemu-CSDN博客 遇到的问题&#xff1a; (1)本来使用git clone https://github.com/qemu/qemu.git fatal: 无法访问 https://github.com/qemu/qemu.git/&#xff1a;GnuTLS recv error (-110): …

OpenHarmony实战开发-页面深色模式适配。

介绍 本示例介绍在开发应用以适应深色模式时&#xff0c;对于深色和浅色模式的适配方案&#xff0c;采取了多种策略如下&#xff1a; 1. 固定属性适配&#xff1a;对于部分组件的颜色属性&#xff0c;如背景色或字体颜色&#xff0c;若保持不变&#xff0c;可直接设定固定色值…

SQL中WITH RECURSIVE的用法

SQL中WITH RECURSIVE的用法 文章目录 SQL中WITH RECURSIVE的用法定义**WITH RECURSIVE 结构通常包含以下几个关键部分&#xff1a;****1. CTE&#xff08;Common Table Expression&#xff0c;公用表表达式&#xff09;&#xff1a;**2.递归查询的结构3.连接操作符&#xff1a;…

【C语言】每日一题,快速提升(2)!

&#x1f525;博客主页&#x1f525;&#xff1a;【 坊钰_CSDN博客 】 欢迎各位点赞&#x1f44d;评论✍收藏⭐ 题目&#xff1a;杨氏矩阵 有一个数字矩阵&#xff0c;矩阵的每行从左到右是递增的&#xff0c;矩阵从上到下是递增的&#xff0c;请编写程序在这样的矩阵中查找某个…

基于SpringCloudAlibaba+Sentinel的分布式限流设计

胡弦&#xff0c;视频号2023年度优秀创作者&#xff0c;互联网大厂P8技术专家&#xff0c;Spring Cloud Alibaba微服务架构实战派(上下册)和RocketMQ消息中间件实战派(上下册)的作者&#xff0c;资深架构师&#xff0c;技术负责人&#xff0c;极客时间训练营讲师&#xff0c;四…

智慧煤矿/智慧矿区视频汇聚存储与安全风险智能分析平台建设思路

一、建设背景 目前我国非常重视煤矿安全生产&#xff0c;并投入大量资金用于煤矿安全综合远程监控系统的研发。视频监控系统作为实现煤矿智能化无人开采的关键系统与煤矿安全生产的多系统协同分析与处理的关键信息源&#xff0c;在智慧矿山管控平台的建设中发挥着重要的作用。…

MATLAB求和函数

语法 S sum(A) S sum(A,“all”) S sum(A,dim) S sum(A,vecdim) S sum(,outtype) S sum(,nanflag) 说明 示例 S sum(A) 返回沿大小大于 1 的第一个数组维度计算的元素之和。 如果 A 是向量&#xff0c;则 sum(A) 返回元素之和。 如果 A 是矩阵&#xff0c;则 sum(A) 将…