惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
Security Archives - TechRepublic
Security Archives - TechRepublic
大猫的无限游戏
大猫的无限游戏
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
Vercel News
Vercel News
U
Unit 42
L
LangChain Blog
M
MIT News - Artificial intelligence
S
SegmentFault 最新的问题
月光博客
月光博客
MongoDB | Blog
MongoDB | Blog
Jina AI
Jina AI
博客园 - 聂微东
H
Hackread – Cybersecurity News, Data Breaches, AI and More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Recent Announcements
Recent Announcements
Engineering at Meta
Engineering at Meta
Hacker News - Newest:
Hacker News - Newest: "LLM"
V2EX - 技术
V2EX - 技术
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
阮一峰的网络日志
阮一峰的网络日志
F
Fortinet All Blogs
D
DataBreaches.Net
Hacker News: Ask HN
Hacker News: Ask HN
W
WeLiveSecurity
N
News | PayPal Newsroom
量子位
Help Net Security
Help Net Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
S
Security @ Cisco Blogs
Y
Y Combinator Blog
H
Heimdal Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Stack Overflow Blog
Stack Overflow Blog
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
P
Privacy International News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
SecWiki News
SecWiki News
Last Week in AI
Last Week in AI
AI
AI
Recorded Future
Recorded Future
C
Cyber Attacks, Cyber Crime and Cyber Security
Microsoft Security Blog
Microsoft Security Blog
P
Privacy & Cybersecurity Law Blog

博客园 - 司徒正美

leetcode 91. Decode Ways leetcode 1214 Two Sum BSTs leetcode 213 House Robber II leetcode 198 House Robber I leetcode 986. Interval List Intersections leetcode 869. Reordered Power of 2 leetcode 925. Long Pressed Name leetcode 457. Circular Array Loop leetcode 1093. Statistics from a Large Sample leetcode 977. Squares of a Sorted Array leetcode 844. Backspace String Compare leetcode 1032. Stream of Characters leetcode 1023. Camelcase Matching leetcode 745 Prefix and Suffix Search leetcode 720. Longest Word in Dictionary leetcode 692. Top K Frequent Words leetcode 677. Map Sum Pairs leetcode 676. Implement Magic Dictionary leetcode 648. Replace Words
leetcode 881. Boats to Save People
司徒正美 · 2020-01-03 · via 博客园 - 司徒正美

使用一艘船救人,每次最多只能救两人,请问最少要几次

这是左右节点法。

var numRescueBoats = function (people, limit) {
            people.sort((a, b) => a - b)
            var left = 0;
            var right = people.length - 1;
            var boats = 0, track = []
            track.add = function (a) {
                this.push(JSON.stringify(a))
            }
            while (left < right) { //注意条件两次最多两人
                if (people[left] + people[right] <= limit) {
                    //装上left, right
                    track.add([people[left], people[right]])
                    left++;
                    right--
                    boats++;
                } else {
                    //只能装right
                    track.add([people[right]])
                    boats++
                    right--
                }
                if (right == left) {
                    track.add([people[right]])
                    //只能装left
                    boats++;
                }
            }
            console.log(track, 'only test, limit = ', limit)
            return boats;
        };

        //1,2,2, 3
        numRescueBoats([3, 2, 2, 1], 3)
        numRescueBoats([3, 5, 3, 4], 5)
        numRescueBoats([1, 1, 2, 4], 4)