码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode 2511. Maximum Enemy Forts That Can Be Captured


    You are given a 0-indexed integer array forts of length n representing the positions of several forts. forts[i] can be -1, 0, or 1 where:

    • -1 represents there is no fort at the ith position.
    • 0 indicates there is an enemy fort at the ith position.
    • 1 indicates the fort at the ith the position is under your command.

    Now you have decided to move your army from one of your forts at position i to an empty position j such that:

    • 0 <= i, j <= n - 1
    • The army travels over enemy forts only. Formally, for all k where min(i,j) < k < max(i,j), forts[k] == 0.

    While moving the army, all the enemy forts that come in the way are captured.

    Return the maximum number of enemy forts that can be captured. In case it is impossible to move your army, or you do not have any fort under your command, return 0.

    Example 1:

    Input: forts = [1,0,0,-1,0,0,0,0,1]
    Output: 4
    Explanation:
    - Moving the army from position 0 to position 3 captures 2 enemy forts, at 1 and 2.
    - Moving the army from position 8 to position 3 captures 4 enemy forts.
    Since 4 is the maximum number of enemy forts that can be captured, we return 4.
    

    Example 2:

    Input: forts = [0,0,1,-1]
    Output: 0
    Explanation: Since no enemy fort can be captured, 0 is returned.
    

    Constraints:

    • 1 <= forts.length <= 1000
    • -1 <= forts[i] <= 1

    这道题难在阅读理解上,给了个巨复杂的情景,本质就是让你求相邻两个1 / -1之间最多有多少个0。诶,其实也有一点被卡,没有想到可以用two pointers来做。一个i每次一个个往后挪,另一个j就每次当i不是0的时候跳到i的位置。当i不是0的时候,判断j是否是i的相反数,如果是的话就可以update result了。

    1. class Solution {
    2. public int captureForts(int[] forts) {
    3. int result = 0;
    4. int i = 0;
    5. int j = 0;
    6. while (i < forts.length) {
    7. if (forts[i] != 0) {
    8. if (forts[j] == -forts[i]) {
    9. result = Math.max(result, i - j - 1);
    10. }
    11. j = i;
    12. }
    13. i++;
    14. }
    15. return result;
    16. }
    17. }

  • 相关阅读:
    微服务框架 SpringCloud微服务架构 10 使用Docker 10.5 容器命令案例2
    Spring Cloud Alibaba Nacos路由策略之保护阈值!
    计算机视觉-光源的目的和作用
    acwing算法基础之基础算法--双指针算法
    Redis 的网络框架是实现了 Reactor 模型吗?
    JZ6 从尾到头打印链表
    深度、广度优先遍历(邻接表)
    C++:C++编程语言学习之数据类型&常量&变量的简介、案例应用之详细攻略
    Bootstrap的媒体对象组件(图文展示组件),挺有用的一个组件。
    PHP-字符串函数
  • 原文地址:https://blog.csdn.net/qq_37333947/article/details/132807566
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号