当前位置:网站首页>leetcode 1642. Furthest building you can reach
leetcode 1642. Furthest building you can reach
2022-06-24 08:43:00 【Blue feather bird】
You are given an integer array heights representing the heights of buildings, some bricks, and some ladders.
You start your journey from building 0 and move to the next building by possibly using bricks or ladders.
While moving from building i to building i+1 (0-indexed),
If the current building’s height is greater than or equal to the next building’s height, you do not need a ladder or bricks.
If the current building’s height is less than the next building’s height, you can either use one ladder or (h[i+1] - h[i]) bricks.
Return the furthest building index (0-indexed) you can reach if you use the given ladders and bricks optimally.
Example 1:
Input: heights = [4,2,7,6,9,14,12], bricks = 5, ladders = 1
Output: 4
Explanation: Starting at building 0, you can follow these steps:
- Go to building 1 without using ladders nor bricks since 4 >= 2.
- Go to building 2 using 5 bricks. You must use either bricks or ladders because 2 < 7.
- Go to building 3 without using ladders nor bricks since 7 >= 6.
- Go to building 4 using your only ladder. You must use either bricks or ladders because 6 < 9.
It is impossible to go beyond building 4 because you do not have any more bricks or ladders.
You have bricks and ladders in your hands , When the next building is higher than the current building , You have to go up with bricks or ladders ,
If you choose bricks, you need Height difference brick , If you choose a ladder, you only need one .
Ask the farthest building you can reach , From the subscript 0 Start .
Ideas :
If the height difference is very large , Need a lot of bricks , But only a ladder is needed ,
An intuitive idea is that those with the greatest height difference use ladders , The rest is made of bricks , To maximize resources , go further .
Obviously, the height difference is not in good order , You can't sort , After all, the building is right there and can't be moved .
So the idea is to use bricks first , When there are not enough bricks, replace the place with a ladder where the height difference is the largest so far , At the same time, recycle the bricks for next use .
So how to find the biggest height difference so far , Use the maximum heap .
public int furthestBuilding(int[] heights, int bricks, int ladders) {
int n = heights.length;
if(n == 1) return 0;
PriorityQueue<Integer> queue = new PriorityQueue<>(Collections.reverseOrder());
for(int i = 0; i < n - 1; i++) {
if(heights[i] >= heights[i+1]) continue;
bricks -= heights[i+1] - heights[i];
queue.add(heights[i+1] - heights[i]);
if(bricks < 0) {
bricks += queue.poll();
if(ladders > 0) ladders --;
else return i;
}
}
return n - 1;
}
It says that time efficiency is not high , Because the height difference must be added to at every step queue in
public int furthestBuilding(int[] heights, int bricks, int ladders) {
int n = heights.length;
if(n == 1) return 0;
PriorityQueue<Integer> queue = new PriorityQueue<>(Collections.reverseOrder());
int i;
for(i = 1; i < heights.length; i++) {
int heightDiff = heights[i] - heights[i - 1];
if(heightDiff <= 0) {
continue;
}
if(heightDiff <= bricks) {
bricks -= heightDiff;
queue.offer(heightDiff);
} else if(ladders > 0){
if(!queue.isEmpty() && queue.peek() > heightDiff) {
// When it is necessary to change
bricks += queue.poll();
bricks -= heightDiff;
queue.offer(heightDiff);
}
ladders--;
} else {
break;
}
}
return i - 1;
}
边栏推荐
- ZUCC_编译语言原理与编译_实验01 语言分析与简介
- MATLAB Camera Calibrator相机标定
- Easycvr invokes the interface parameter acquisition method and precautions of device video recording on the page
- Promise的使用場景
- One development skill a day: how to establish P2P communication based on webrtc?
- PHP代码加密的几种方案
- Shell pass parameters
- ZUCC_编译语言原理与编译_实验08 语法分析 LR 分析
- Markdown to realize text link jump
- Use cpulimit to free up your CPU
猜你喜欢

ZUCC_编译语言原理与编译_实验01 语言分析与简介

ZUCC_编译语言原理与编译_实验02 FSharp OCaml语言

【关于运维和网工的差别,一文说透】

A preliminary study of IO model

ZUCC_ Principles of compiling language and compilation_ Experiment 04 language and grammar

MATLAB Camera Calibrator相机标定

Base64编码详解及其变种(解决加号在URL变空格问题)

什么是SRE?一文详解SRE运维体系

ZUCC_编译语言原理与编译_实验08 语法分析 LR 分析

小黑ai4code代码baseline啃食1
随机推荐
中国芯片独角兽公司
Use cpulimit to free up your CPU
Scénarios d'utilisation de la promesse
Permission model DAC ACL RBAC ABAC
QPS, TPS, concurrent users, throughput relationship
jwt(json web token)
解析互联网广告术语 CPM、CPC、CPA、CPS、CPL、CPR 是什么意思
Redis cluster data skew
数据平台简介
一文详解|增长那些事儿
Base64编码详解及其变种(解决加号在URL变空格问题)
ZUCC_ Principles of compiling language and compilation_ Experiment 03 getting started with compiler
"Wechat cloud hosting" first practical battle | introduction to minimalist demo
One development skill a day: how to establish P2P communication based on webrtc?
【关于运维和网工的差别,一文说透】
Common date formatter and QT method for obtaining current time
日本大阪大学万伟伟研究员介绍基于WRS系统机器人的快速集成方法和应用
5 minutes, excellent customer service chat handling skills
相机投影矩阵计算
MATLAB Camera Calibrator相机标定