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
相关推荐
Navigator_Z2 小时前
LeetCode //C - 1293. Shortest Path in a Grid with Obstacles Elimination
c语言·算法·leetcode
Nil2082 小时前
leetcode 64最小路径和
算法·leetcode·职场和发展
Nil2083 小时前
leetcode 72编辑距离
算法·leetcode·职场和发展
Nil2083 小时前
leetcode 136只出现一次的数字
leetcode·哈希算法·散列表
局部有解8 小时前
LeetCode Hot100 438 找到字符串中所有字母异位词
c++·算法·leetcode
Nil20810 小时前
leetcode 75颜色分类
数据结构·算法·leetcode
aqiu11111111 小时前
CodeTop按顺序刷题0003:LeetCode 206. 反转链表(迭代法图文详解与动态演示)
算法·leetcode·链表
不会就选b11 小时前
算法日常・每日刷题--<动态规划>29
数据结构·算法·leetcode
虚无的纽扣13 小时前
【力扣刷题】第五天:将 x 减到 0 的最小操作数、最大连续1的个数 III
算法·leetcode·职场和发展
hanlin031 天前
刷题笔记:力扣第32题-最长有效括号
笔记·算法·leetcode