当前位置:网站首页>详解牛客----手套
详解牛客----手套
2022-04-23 16:37:00 【长大Leslie】
原题链接:手套__牛客网
解题思路:
1. 首先考虑两边手套都不出现0的情况
以取左边的为例子:把左边所有的手套数量减去数量最小的颜色的数量然后+1, 对应上图就是取17-2+1=16只,也就是说,左边取16只手套,肯定把所有颜色都取出,现在只要往另一只手中取一只就能匹配了。假设取15只手套,那么可能存在这么一种情况:取了4只黄色,5只绿色,6只蓝色;那么橙色的就可能取不到,所以取15只手套不能100%的把所有可能出现的颜色都取出。同样的操作把右边的走一遍,判断左边和右边所取的数量哪一种更小,然后最后的值+1就能匹配了。
2. 存在两边可能出现 0 的情况
最终代码实现:
class Gloves {
public:
int findMinimum(int n, vector<int> left, vector<int> right) {
// write code here
int sum=0;
int left_min=INT_MAX;
int right_min=INT_MAX;
int left_sum=0,right_sum=0;
for(int i=0;i<n;i++)
{
if(left[i]*right[i]==0)
sum+=left[i]+right[i];//如果存在一个为0,就要把这种情况求出
else
{
left_sum+=left[i];
left_min=left[i]<left_min?left[i]:left_min;
right_sum+=right[i];
right_min=right[i]<right_min?right[i]:right_min;
}
}
return sum+=(left_sum>right_sum?(right_sum-right_min+1):(left_sum-left_min+1))+1;
}
};
版权声明
本文为[长大Leslie]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_56695823/article/details/124317478
边栏推荐
- The most detailed knapsack problem!!!
- Homewbrew installation, common commands and installation path
- About background image gradient()!
- JMeter installation tutorial and solutions to the problems I encountered
- Report FCRA test question set and answers (11 wrong questions)
- 力扣-746.使用最小花费爬楼梯
- UWA Pipeline 功能详解|可视化配置自动测试
- NVIDIA graphics card driver error
- What is cloud migration? The four modes of cloud migration are?
- Public variables of robotframework
猜你喜欢
File upload and download of robot framework
The first line and the last two lines are frozen when paging
Gartner publie une étude sur les nouvelles technologies: un aperçu du métacosme
The system research problem that has plagued for many years has automatic collection tools, which are open source and free
LVM and disk quota
面试题 17.10. 主要元素
Questions about disaster recovery? Click here
深度学习100例 | 第41天-卷积神经网络(CNN):UrbanSound8K音频分类(语音识别)
Hypermotion cloud migration completes Alibaba cloud proprietary cloud product ecological integration certification
Nacos 详解,有点东西
随机推荐
Day 9 static abstract class interface
Server log analysis tool (identify, extract, merge, and count exception information)
On the security of key passing and digital signature
Set the color change of interlaced lines in cells in the sail software and the font becomes larger and red when the number is greater than 100
Project framework of robot framework
Database dbvisualizer Pro reported file error, resulting in data connection failure
第九天 static 抽象类 接口
OAK-D树莓派点云项目【附详细代码】
磁盘管理与文件系统
What does cloud disaster tolerance mean? What is the difference between cloud disaster tolerance and traditional disaster tolerance?
力扣-198.打家劫舍
There is a problem with the light switch from 1 to 100
安装Redis并部署Redis高可用集群
Solution of garbled code on idea console
Query the data from 2013 to 2021, and only query the data from 2020. The solution to this problem is carried out
Gartner publie une étude sur les nouvelles technologies: un aperçu du métacosme
Day (5) of picking up matlab
建站常用软件PhpStudy V8.1图文安装教程(Windows版)超详细
Research and Practice on business system migration of a government cloud project
G008-HWY-CC-ESTOR-04 华为 Dorado V6 存储仿真器配置