2023-09-02力扣每日一题

链接:

2511. 最多可以摧毁的敌人城堡数目

题意和解

阅读理解题,要从1到-1或-1到1,中间只能有0,求最多能有多少0

实际代码:

c++ 复制代码
#include<bits/stdc++.h>
using namespace std;
int captureForts(vector<int>& forts)
{
	int ans=0,after=1E8+7;
	bool Left=false;
	for(int i=0;i<forts.size();i++)
	{
		if(forts[i]==1)
		{
			if(!Left) ans=max(ans,i-after-1);
			after=i;Left=true;
		}
		else if(forts[i]==-1)
		{
			if(Left) ans=max(ans,i-after-1);
			after=i;Left=false;
		}
	}
	return ans>=0?ans:0;
}
int main()
{
	vector<int> forts;int fort;
	while(cin>>fort) forts.push_back(fort);
	int ans=captureForts(forts);
	cout<<ans<<endl;
	return 0;
}

限制:

  • 1 <= forts.length <= 1000
  • -1 <= forts[i] <= 1
相关推荐
smj2302_796826526 小时前
解决leetcode第3911题.移除子数组元素后第k小偶数
数据结构·python·算法·leetcode
_深海凉_9 小时前
LeetCode热题100-寻找两个正序数组的中位数
算法·leetcode·职场和发展
踩坑记录10 小时前
leetcode hot100 寻找两个正序数组的中位数 hard 二分查找 双指针
leetcode
superior tigre13 小时前
78 子集
算法·leetcode·深度优先·回溯
superior tigre15 小时前
739 每日温度
算法·leetcode·职场和发展
6Hzlia15 小时前
【Hot 100 刷题计划】 LeetCode 15. 三数之和 | C++ 排序+双指针
c++·算法·leetcode
北顾笙98017 小时前
day37-数据结构力扣
数据结构·算法·leetcode
6Hzlia19 小时前
【Hot 100 刷题计划】 LeetCode 189. 轮转数组 | C++ 三次反转经典魔法 (O(1) 空间)
c++·算法·leetcode
m0_6294947320 小时前
LeetCode 热题 100-----13.最大子数组和
数据结构·算法·leetcode
田梓燊20 小时前
力扣:94.二叉树的中序遍历
数据结构·算法·leetcode