当前位置:网站首页>Leetcode 244周赛-赛后补题题解【西兰花选手】
Leetcode 244周赛-赛后补题题解【西兰花选手】
2022-07-27 14:18:00 【Honyelchak】
Problem A-判断矩阵经轮转后是否一致
题意: 以 90 度顺时针轮转矩阵 mat 中的元素 若干次,如果mat矩阵与target一致,返回true,否则返回false
思路: 直接模拟就可以了,做这题的时候还费劲写了一个原地算法,y神说了没必要,直接开一个二维数组即可。
代码:
Java中不能直接判断两个二维数组的内容是否相同,恶心!
class Solution {
public boolean findRotation(int[][] mat, int[][] target) {
int n = mat.length, num = 3;
boolean flag = false;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (mat[i][j] != target[i][j]) {
flag = true; break;
}
}
if (flag)break;
}
if (!flag)return true;
while(num-- > 0) {
for(int i=0; i<n/2; i++) {
for(int j=i; j<n-i-1; j++) {
int t = mat[i][j];
mat[i][j] = mat[n-j-1][i];
mat[n-j-1][i] = mat[n-i-1][n-j-1];
mat[n-i-1][n-j-1] = mat[j][n-i-1];
mat[j][n-i-1] = t;
}
}
flag = false;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (mat[i][j] != target[i][j]) {
flag = true; break;
}
}
if (flag)break;
}
if (!flag)return true;
}
return false;
}
}
Problem B-使数组元素相等的减少操作次数
思路: 每个元素a需要经历t个阶段最后变成相等最小值,t就是数组中比元素a小的元素种类和。所以先把数组排序,然后统计(元素前边比他小的元素种类即可)
代码:
class Solution {
public int reductionOperations(int[] nums) {
int len = nums.length;
Arrays.sort(nums);
int pre[] = new int[len + 1];
int count = 0;
pre[0] = 0;
for (int i = 1; i < len; i++) {
if ((nums[i] - nums[i-1]) != 0) {
pre[i] = pre[i - 1] + 1;
} else {
pre[i] = pre[i - 1];
}
}
// 下边的累加可以放在第一个for循环中
int res = 0;
for (int i = 1; i < len; i++) {
res += pre[i];
}
return res;
}
}
Problem C-使二进制字符串字符交替的最少反转次数
思路: 两种类型的操作,让统计第二种类型操作的最少次数。第一种类型操作的特殊性,让我想到将两个原串拼接在一起,然后滑动窗口,取第二种操作的最少次数。最终字符串有两种:“0101010101…”或"1010101010…",比较两串不同元素个数即可。
代码:
class Solution {
public int minFlips(String s) {
int len = s.length(), t = 2, res = Integer.MAX_VALUE;
String time = s + s;
t = 2;
while (t-- > 0) {
int count = 0, j = t;
for (int k = 0; k < len; j ^= 1, k++) {
count += (time.charAt(k) == (j +'0')) ? 0:1;
}
res = Math.min(res, count);
for (int k = len, p = t; k < len*2; j ^= 1, p ^= 1, k++) {
count -= (time.charAt(k - len) == (p +'0')) ? 0:1;
count += (time.charAt(k) == (j +'0')) ? 0:1;
res = Math.min(res, count);
}
}
return res;
}
}
Problem D-装包裹的最小浪费空间
思路: 遍历supplier,让box二分package,计算出该supplier所需要的总容积,最后取最小的容积,减去packages的总和。如果遍历package,让package二分box,会超时。
代码:
class Solution {
final long mod = (long)(1e9 + 7);
final long inf = (long)1e18;
public int minWastedSpace(int[] packages, int[][] boxes) {
int supplierNum = boxes.length, packagesNum = packages.length;
long res = inf, sum = 0;
// 包裹总和
for (int i = 0; i < packagesNum; i++) {
sum += packages[i];
}
Arrays.sort(packages);
for (int i = 0; i < supplierNum; i++) {
int len = boxes[i].length, left = -1, right = packagesNum - 1, last = -1;
long count = -sum;
Arrays.sort(boxes[i], 0, len);
if (packages[packagesNum - 1] > boxes[i][len - 1]) continue;
// 统计box总的容积
for (int j = 0; j < len; j ++) {
left = upperBound(packages, left, right, boxes[i][j]);
if (left == last) continue;
count += ((long)(left - last) * boxes[i][j]);
last = left;
}
res = Math.min(res, count);
}
if (res == inf) return -1;
return (int)(res % mod);
}
public int upperBound(int[] a, int left, int right, int key) {
int mid;
while (left < right) {
mid = (left + right + 1) >> 1;
if (a[mid] > key) {
right = mid - 1;
} else {
left = mid;
}
}
return left;
}
}
边栏推荐
- Principle of MOS tube to prevent reverse connection of power supply
- 移动端使用vantUI的list组件,多个tab项来回切换时,列表加载多次导致数据无法正常展示
- DXGI acquisition process
- 网络设备硬核技术内幕 路由器篇 7 汤普金森漫游网络世界(下)
- 【云享读书会第13期】多媒体处理工具 FFmpeg 工具集
- 适配验证新职业来了!华云数据参与国家《信息系统适配验证师国家职业技能标准》编制
- Understand the evolution of redis architecture in one article
- Stm32f103c8t6 drives ssd1306 0.96 "IIC OLED display under Arduino frame
- [ManageEngine] what is Siem
- 对话框管理器第三章:创建控件
猜你喜欢

LeetCode 190. 颠倒二进制位 位运算/easy

关于 CMS 垃圾回收器,你真的懂了吗?

代码覆盖率统计神器-jacoco工具实战

什么是Tor?Tor浏览器更新有什么用?
仪表放大器和运算放大器优缺点对比

Finally, someone finished all the dynamic planning, linked list, binary tree and string required for the interview

Nokia's patent business was hit for the first time, and Chinese enterprises are not so easy to knead

4种单片机驱动继电器方案

光电隔离电路设计方案(六款基于光耦、AD210AN的光电隔离电路图)

Zhou Hongyi: if the digital security ability is backward, it will also be beaten
随机推荐
Nokia's patent business was hit for the first time, and Chinese enterprises are not so easy to knead
The mobile terminal uses the list component of vantui. When multiple tab items are switched back and forth, the list is loaded many times, resulting in the failure of normal display of data
Zhou Hongyi: if the digital security ability is backward, it will also be beaten
Tencent two sides: @bean and @component are used in the same class, what will happen?
网络设备硬核技术内幕 路由器篇 6 汤普金森漫游网络世界(中)
Wechat applet realizes music search page
基于stm32的数字示波器设计方案
What is the breakthrough point of digital transformation in the electronic manufacturing industry? Lean manufacturing is the key
adb命令 (安装apk包格式:adb install 电脑上apk地址包名)
STM32F10x_硬件I2C读写EEPROM(标准外设库版本)
分布式锁
Graphical SQL is too vivid
Jmeter录制接口自动化
[Yunxiang book club issue 13] packaging format of video files
Txt replace line breaks with spaces or cancel line breaks
光电隔离电路设计方案(六款基于光耦、AD210AN的光电隔离电路图)
《剑指Offer》 合并两个排序的链表
多表查询_子查询概述和多表查询_子查询情况1&情况2&情况3
事务_基本演示和事务_默认自动提交&手动提交
Unity性能优化------DrawCall