当前位置:网站首页>1700. 无法吃午餐的学生数量
1700. 无法吃午餐的学生数量
2022-07-30 14:20:00 【Mr Gao】
1700. 无法吃午餐的学生数量
学校的自助午餐提供圆形和方形的三明治,分别用数字 0 和 1 表示。所有学生站在一个队列里,每个学生要么喜欢圆形的要么喜欢方形的。
餐厅里三明治的数量与学生的数量相同。所有三明治都放在一个 栈 里,每一轮:
如果队列最前面的学生 喜欢 栈顶的三明治,那么会 拿走它 并离开队列。
否则,这名学生会 放弃这个三明治 并回到队列的尾部。
这个过程会一直持续到队列里所有学生都不喜欢栈顶的三明治为止。
给你两个整数数组 students 和 sandwiches ,其中 sandwiches[i] 是栈里面第 i 个三明治的类型(i = 0 是栈的顶部), students[j] 是初始队列里第 j 名学生对三明治的喜好(j = 0 是队列的最开始位置)。请你返回无法吃午餐的学生数量。
示例 1:
输入:students = [1,1,0,0], sandwiches = [0,1,0,1]
输出:0
解释:
- 最前面的学生放弃最顶上的三明治,并回到队列的末尾,学生队列变为 students = [1,0,0,1]。
- 最前面的学生放弃最顶上的三明治,并回到队列的末尾,学生队列变为 students = [0,0,1,1]。
- 最前面的学生拿走最顶上的三明治,剩余学生队列为 students = [0,1,1],三明治栈为 sandwiches = [1,0,1]。
- 最前面的学生放弃最顶上的三明治,并回到队列的末尾,学生队列变为 students = [1,1,0]。
- 最前面的学生拿走最顶上的三明治,剩余学生队列为 students = [1,0],三明治栈为 sandwiches = [0,1]。
- 最前面的学生放弃最顶上的三明治,并回到队列的末尾,学生队列变为 students = [0,1]。
- 最前面的学生拿走最顶上的三明治,剩余学生队列为 students = [1],三明治栈为 sandwiches = [1]。
- 最前面的学生拿走最顶上的三明治,剩余学生队列为 students = [],三明治栈为 sandwiches = []。
所以所有学生都有三明治吃。
示例 2:
输入:students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1]
输出:3
解题代码如下:
int countStudents(int* students, int studentsSize, int* sandwiches, int sandwichesSize){
int i;
int sand=0;
while(true){
int r=0;
int sstu=0;
for(i=0;i<studentsSize;i++){
if(students[i]==sandwiches[sand]){
sand++;
r=1;
}
else{
students[sstu++]=students[i];
}
}
studentsSize=sstu;
if(r==0){
return sstu;
}
}
return -1;
}
边栏推荐
- Huawei's 7-year-experienced software testing director, gives some advice to all friends who want to change careers to learn software testing
- SLF4J的使用
- What is the relationship between the construction of smart cities and 5G technology in the new era
- 开始学习C语言了
- Meta首份元宇宙白皮书9大看点,瞄准80万亿美元市场
- 【回归预测-CNN预测】基于卷积神经网络CNN实现数据回归预测附matlab代码
- 国内数字藏品的乱象与未来
- 算力顶天地,存力纳乾坤:国家超级计算济南中心的一体两面
- CS内网横向移动 模拟渗透实操 超详细
- Baijiahao cancels the function of posting documents on the interface: the weight of the plug-in chain is blocked
猜你喜欢
随机推荐
[深入研究4G/5G/6G专题-46]: 5G Link Adaption链路自适应-2-常见缩略语
网站添加能换装可互动的live 2d看板娘
Still saying software testing doesn't have a midlife crisis?9 years of test engineers were eliminated
MongoDB启动报错 Process: 29784 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=14)
国内数字藏品的乱象与未来
MPSK抗噪声性能对比(即MPSK标准误码率曲线)
#第九章 子查询课后习题
2022年,目前大环境下还适合转行软件测试吗?
LeetCode_数位枚举_困难_233.数字 1 的个数
Start learning C language
Desktop Software Development Framework Awards
Interface automation framework, lm-easytest beta version released, use it quickly~
canal scrape data
开源工具推荐:高性能计算辅助工具MegPeak
【元胞自动机】基于元胞自动机模拟生命演化、病毒感染等实例附matlab代码
redis6.0 源码学习(五)ziplist
打破原则引入SQL,MongoDB到底想要干啥?
Machine learning difference in the competition and industry application
MIMO雷达波形设计
Container sorting case









