当前位置:网站首页>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 情侣牵手
边栏推荐
- 3年,从3K涨薪到20k?真是麻雀啄了牛屁股 — 雀食牛逼呀
- 游戏3D建模入门,有哪些建模软件可以选择?
- VMware NSX 4.0 -- 网络安全虚拟化平台
- SQL关联表更新
- Security software Avast and Symantec NortonLifeLock merge with UK approval, market value soars 43%
- 加解密在线工具和进制转化在线工具
- uniapp 分享功能-分享给朋友群聊朋友圈效果(整理)
- Develop a SpaceX website based on the Appian low-code platform
- 2022牛客暑期多校训练营5(BCDFGHK)
- NebulaGraph v3.2.0 Release Note, many optimizations such as the performance of querying the shortest path
猜你喜欢

MySQL的安装与卸载

I was rejected by the leader for a salary increase, and my anger rose by 9.5K after switching jobs. This is my mental journey

从单体架构迁移到 CQRS 后,我觉得 DDD 并不可怕

Xiaohei leetcode surfing: 94. Inorder traversal of binary tree

线程三连鞭之“线程的状态”
![[Happy Qixi Festival] How does Nacos realize the service registration function?](/img/df/5793145da45bc80d227b0babfac914.png)
[Happy Qixi Festival] How does Nacos realize the service registration function?

小黑leetcode之旅:95. 至少有 K 个重复字符的最长子串

407. 接雨水 II

零基础如何入门软件测试?再到测开(小编心得)

怎么将自己新文章自动推送给自己的粉丝(巨简单,学不会来打我)
随机推荐
LeetCode Hot 100
4 - "PyTorch Deep Learning Practice" - Backpropagation
NebulaGraph v3.2.0 Release Note,对查询最短路径的性能等多处优化
吐槽 | 参加IT培训的正确姿势
资深游戏建模师告知新手,游戏场景建模师必备软件有哪些?
线程三连鞭之“线程的状态”
golang 协程的实现原理
Basic web in PLSQL
工业物联网 —— 新型数据库的召唤
仿网易云音乐小程序-uniapp
Nuclei (2) Advanced - In-depth understanding of workflows, Matchers and Extractors
Bidding Announcement | Operation and Maintenance Project of Haina Baichuang Official Account
#yyds干货盘点#交换设备丢包严重的故障处理
小黑leetcode冲浪:94. 二叉树的中序遍历
NebulaGraph v3.2.0 Release Note, many optimizations such as the performance of querying the shortest path
OPENCV学习DAY8
Statistical words (DAY 101) Huazhong University of Science and Technology postgraduate examination questions
uniapp 分享功能-分享给朋友群聊朋友圈效果(整理)
上课笔记(6)(2)——#742. 周末舞会
大师教你3D实时角色制作流程,游戏建模流程分享