当前位置:网站首页>June 27, 2021: given a positive array arr, it represents the weight of several people
June 27, 2021: given a positive array arr, it represents the weight of several people
2022-06-24 08:10:00 【Fuda scaffold constructor's daily question】
2021-06-27: Given an array of positive numbers arr, Represents the weight of several people . Give me a positive number limit, Indicates the deadweight shared by all ships . A maximum of two people can sit on each ship , And shall not exceed the load , I want all the people to cross the river at the same time , And use the best allocation method to minimize the number of ships . Returns the minimum number of ships .
Fuda answer 2021-06-27:
An array is 1 3 5 5 5 7 9 2 4 6 8 10,limit yes 10.
1. Sort .1 3 5 5 5 7 9 2 4 6 8 10 After sorting 1 2 3 4 5 5 5 6 7 8 9 10.
2. Find less than or equal to limit/2 Location ,, At this time, the position is 6.
3. Prepare double pointer , The left pointer points to 6 Location , The right pointer points to 7 Location . The sum of the left and right pointers is greater than limit, The left pointer moves to the left ; The sum of the left and right pointers is less than limit, Right pointer moves right ; The sum of the left and right hands equals limit, The left pointer moves to the left , Right pointer moves right .
4. Statistics count . Left and right can be matched , The left and right numbers are calculated as 1; Those who cannot be paired , Every two numbers on the left count as 1, On the right 1 Count as 1. Add them all at last , Is the value that needs to be returned .
Time complexity :O(N). Extra space complexity :O(1).
The code to use golang To write . The code is as follows :
package main
import (
"fmt"
"sort"
)
func main() {
arr := []int{1, 3, 5, 5, 5, 7, 9, 2, 4, 6, 8, 10}
fmt.Println(arr)
ret := minBoat(arr, 10)
fmt.Println(ret)
}
func minBoat(arr []int, limit int) int {
if len(arr) == 0 {
return 0
}
N := len(arr)
sort.Ints(arr)
fmt.Println(arr)
if arr[N-1] > limit {
return -1
}
lessR := -1
for i := N - 1; i >= 0; i-- {
if arr[i] <= (limit / 2) {
lessR = i
break
}
}
if lessR == -1 {
return N
}
L := lessR
R := lessR + 1
noUsed := 0
for L >= 0 {
solved := 0
for R < N && arr[L]+arr[R] <= limit {
R++
solved++
}
if solved == 0 {
noUsed++
L--
} else {
L = getMax(-1, L-solved)
}
}
all := lessR + 1
used := all - noUsed
moreUnsolved := (N - all) - used
return used + ((noUsed + 1) >> 1) + moreUnsolved
}
func getMax(a int, b int) int {
if a > b {
return a
} else {
return b
}
}The results are as follows :
边栏推荐
- 4-operation list (loop structure)
- OC Extension 检测手机是否安装某个App(源码)
- 1-4metasploitable2介绍
- 1279_VMWare Player安装VMWare Tools时VSock安装失败解决
- Introduction of model compression tool based on distiller
- 不止于观测|阿里云可观测套件正式发布
- Signature analysis of app x-zse-96 in a Q & a community
- Chrono usage notes
- Thread considerations
- Swift 基础 闭包/Block的使用(源码)
猜你喜欢

On the H5 page, the Apple phone blocks the content when using fixed to locate the bottom of the tabbar

JDBC 在性能测试中的应用

Backup and restore SQL Server Databases locally

5-if语句(选择结构)

Specify IP when calling feign interface

Do you still have the opportunity to become a machine learning engineer without professional background?

Swift 基础 闭包/Block的使用(源码)

解决错误: LNK2019 无法解析的外部符号
![[C language] system date & time](/img/de/faf397732bfa4920a8ed68df5dbc48.png)
[C language] system date & time

毕业两年月薪36k,说难也不难吧
随机推荐
[test development] first knowledge of software testing
[run the script framework in Django and store the data in the database]
热赛道上的冷思考:乘数效应才是东数西算的根本要求
Thread support
Random number remarks
Chapter 3 curve graph of canvas
[测试开发]初识软件测试
Pipeline concept of graphic technology
Solution to the error of running NPM run eject
More than observation | Alibaba cloud observable suite officially released
Swift Extension ChainLayout(UI的链式布局)(源码)
Leetcode 174 Dungeon games (June 23, 2022)
From jsonpath and XPath to spl
Duilib display memory picture
研究生英语期末考试复习
Case examples of corpus data processing (cases related to sentence retrieval)
Configure your own free Internet domain name with ngrok
Getting started with crawler to giving up 06: crawler play Fund (with code)
Gossip: what happened to 3aC?
[008] filter the table data row by row, jump out of the for cycle and skip this cycle VBA