程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

leetcode 1642. Furthest Building You Can Reach(python)

編輯:Python

Keep creating , Accelerate growth ! This is my participation 「 Nuggets day new plan · 6 Yuegengwen challenge 」 Of the 29 God , Click to see the event details

describe

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.

Example 2:

Input: heights = [4,12,2,7,3,18,20,3,19], bricks = 10, ladders = 2
Output: 7

Example 3:

Input: heights = [14,3,19,3], bricks = 17, ladders = 0
Output: 3

Note:

1 <= heights.length <= 10^5
1 <= heights[i] <= 10^6
0 <= bricks <= 10^9
0 <= ladders <= heights.length

analysis

According to the meaning , Given an array of integers heights, Indicates the height of the building , And some bricks and some ladders . from 0 Building No. 1 starts to walk , And may use bricks or ladders to move to the next building . From architecture i Move to building i+1(0 Indexes ) when ,

  • If the height of the current building is greater than or equal to the height of the next building , No ladder or brick is required .
  • If the height of the current building is less than the height of the next building , You can use a ladder or (h[i+1] - h[i]) brick .

If the given ladder and bricks are used in the best way , The farthest building index that can be reached is returned (0 Start index ).

This question is about greedy thinking and sorting , When we move on the roof , You only need to use bricks or ladders when climbing high , Because the ladder is infinitely high , So according to the greedy thought , You must try to use bricks first , But when the bricks are used up , What we need to pay attention to , At this point, if we have no bricks in our hands and only one ladder , It must stop when you run into a tall building after you use it , But if we use a ladder to replace the bricks where we used the most bricks before , We will have many more bricks in our hands , And then we will encounter high-rise buildings within our capabilities , We can still move on , This operation can make the climbing process go further . The most common idea of using bricks here is to use the big root pile .

The time complexity is O(NlogN) , The space complexity is O(N).

answer

class Solution(object):
def furthestBuilding(self, heights, bricks, ladders):
"""
:type heights: List[int]
:type bricks: int
:type ladders: int
:rtype: int
"""
L = []
for i in range(1, len(heights)):
d = heights[i] - heights[i-1]
if d > 0:
heapq.heappush(L, -d)
bricks -= d
if bricks < 0:
if ladders > 0:
ladders -= 1
bricks += -heapq.heappop(L)
else:
return i-1
return len(heights)-1

Running results

Runtime: 1035 ms, faster than 34.48% of Python online submissions for Furthest Building You Can Reach.
Memory Usage: 24.3 MB, less than 22.41% of Python online submissions for Furthest Building You Can Reach.

Original link

leetcode.com/problems/fu…

Your support is my greatest motivation


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved