当前位置:网站首页>Couple Holding Hands [Greedy & Abstract]
Couple Holding Hands [Greedy & Abstract]
2022-08-04 23:48:00 【REN_Linsen】
前言
Greed is a good question to examine analytical skills,Especially the ones that are intrusive,That is, we need to extract useful information,去伪存真,The problem can be abstracted away.动态规划也是一样,If there is no abstract thinking to extract vital information,The state cannot be defined,Naturally, there is no way to find out how to transfer the state.Of course reading comprehension questions/脑筋急转弯,This ability to abstract useful information is even more needed.
一、情侣牵手

二、贪心&抽象
package everyday.hard;
// 情侣牵手.
public class MinSwapsCouples {
/* 抽象思维:其实我们并不关心1/2/3...这些位置,We only care about special adjacencies<0,1><2,3>. Then a pair of special positions<x,y>,有2种情况.First, couples don't care;The second is not a couple,can be replaced1个人,Can also be replaced2个人. Since we only care about adjacent positions,That is to replace the boys of the two couples,换个角度看,In fact, it can also be seen as replacing the two girls,So what we care about is the adjacent pair,rather than a special location. 所以,根据贪心,换1The way of man is the best,leave alone,Just replace it less often. */
public int minSwapsCouples(int[] row) {
int cnt = 0;
for (int i = 0; i < row.length; i += 2) {
int target = (row[i] & 1) == 0 ? row[i] + 1 : row[i] - 1;
if (target == row[i + 1]) continue;
findAndSwap(row, i + 1, target);
cnt++;
}
return cnt;
}
private void findAndSwap(int[] row, int begin, int target) {
int i = begin + 1;
while (i < row.length && target != row[i]) ++i;
row[i] = row[begin];
row[begin] = target;
}
}
总结
1)Practice greed problems more,Exercise analytical skills & Information processing ability & Information extraction and transformation capabilities.
参考文献
[1] LeetCode 情侣牵手
边栏推荐
- .net(C#)获取两个日期间隔的年月日
- 学会反射后,我被录取了(干货)
- 小黑leetcode冲浪:94. 二叉树的中序遍历
- 怎么将自己新文章自动推送给自己的粉丝(巨简单,学不会来打我)
- 中日颜色风格
- After another 3 days, I have sorted out 90 NumPy examples, and I can't help but bookmark it!
- .net (C#) get year month day between two dates
- Nuclei(二)进阶——深入理解workflows、Matchers和Extractors
- 话题 | 雾计算和边缘计算有什么区别?
- Cython
猜你喜欢

Uniapp dynamic sliding navigation effect demo (finishing)

NebulaGraph v3.2.0 Release Note,对查询最短路径的性能等多处优化

MongoDB权限验证开启与mongoose数据库配置

【无标题】线程三连鞭之“线程池”

未上市就“一举成名”,空间媲美途昂,安全、舒适一个不落

游戏3D建模入门,有哪些建模软件可以选择?

功耗控制之DVFS介绍

Flutter启动流程(Skia引擎)介绍与使用

Pytorch分布式训练/多卡/多GPU训练DDP的torch.distributed.launch和torchrun

KT148A语音芯片ic工作原理以及芯片的内部架构描述
随机推荐
Pytorch分布式训练/多卡/多GPU训练DDP的torch.distributed.launch和torchrun
kernel问题定位手段总结
The market value of 360 has evaporated by 390 billion in four years. Can government and enterprise security save lives?
Laravel 实现redis分布式锁
2022牛客暑期多校训练营5(BCDFGHK)
功耗控制之DVFS介绍
3年,从3K涨薪到20k?真是麻雀啄了牛屁股 — 雀食牛逼呀
应用联合、体系化推进。集团型化工企业数字化转型路径
How to burn the KT148A voice chip into the chip through the serial port and the tools on the computer
直接插入排序
MYS-6ULX-IOT 开发板测评——使用 Yocto 添加软件包
Basic web in PLSQL
三大技巧让你成功入门3D建模,零基础小白必看
~ hand AHB - APB Bridge 】 【 AMBA AHB bus
After another 3 days, I have sorted out 90 NumPy examples, and I can't help but bookmark it!
七牛云图片上传
jenkins发送邮件系统配置
Kernel函数解析之kernel_restart
基于Appian低代码平台开发一个SpaceX网站
KT6368A蓝牙的认证问题_FCC和BQB_CE_KC认证或者其它说明