推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
penguinhlxhands
V2EX  ›  Python

个算法题想了好久没思路,求指点

  •  1
     
  •   penguinhlxhands · Jul 27, 2020 · 2425 views
    This topic created in 2250 days ago, the information mentioned may be changed or developed.

    射击训练,需要给每个士兵发子弹,发子弹的个数规则是根据士兵的训练成绩给定的,传入士兵的训练成绩,要求相邻士兵中,成绩好的士兵的子弹数必须更多,每个士兵至少分配一个子弹。

    输入描述:

    输入每个士兵的训练成绩,如 1,2,3,代表 3 位士兵的成绩分别为 1,2,3

    输出描述:

    最少分配的子弹数

    示例 1:

    输入:1,2,2

    输出:4

    AlisaDestiny
        1
    AlisaDestiny  
       Jul 27, 2020   ❤️ 2
    这个就是求数据的极值点,最小值处的值都是 1,最大值处的值是 n = max(到左极值距离,到右极值距离),距离远的一侧从最小值处到最大值处需要的子弹数是 sum(1..n),另一侧是 sum(1..到极大值距离-1)
    AlisaDestiny
        2
    AlisaDestiny  
       Jul 27, 2020
    需要的子弹总数就是将各个区段的和值相加。
    nthhdy
        3
    nthhdy  
       Jul 27, 2020
    如果相邻的分数相同,应该是没有约束的
    warush
        4
    warush  
       Jul 28, 2020 via iPhone   ❤️ 1
    这不就是 leetcode 上的 candy,从左往右便利一下给最低的子弹,然后再从有往左边遍历一遍
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   904 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 19:31 · PVG 03:31 · LAX 12:31 · JFK 15:31
    ♥ Do have faith in what you're doing.