Giter VIP home page Giter VIP logo

lc-python29's Introduction

LC-Python29

Greedy Algorithm 4

860.Lemonade Change, 406.Queue Reconstruction by Height, 452.Minimum Number of Arrows to Burst Balloons

June 30, 2023 4h

Congratulations!
This is the 29th day for leetcode python study. Today we will learn about the Greedy Algorithm!
The challenges today are especially about inserting items according to the ranking and merge intervals of overlapping.

860.Lemonade Change

This is an easy question.

class Solution:
    def lemonadeChange(self, bills: List[int]) -> bool:
        five = 0
        ten = 0
        twenty = 0
        
        for bill in bills:
            if bill == 5:
                five += 1
            
            if bill == 10:
                if five <= 0:
                    return False
                ten += 1
                five -= 1
            
            if bill == 20:
                if five > 0 and ten > 0:
                    five -= 1
                    ten -= 1
                    #twenty += 1
                elif five >= 3:
                    five -= 3
                    #twenty += 1
                else:
                    return False
        
        return True

406.Queue Reconstruction by Height

Deal with one side first and then deal with another side! Firstly sort h/height from large to small, then adjust orders according to k.

class Solution:
    def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]: 
        #firstly sort heights from largest to smallest, if same, sort according to k from least to largest
        people.sort(key = lambda x: (-x[0],  x[1]))
        que = []

        #adjust the order again according to k from least to largest
        #by inserting element p to place p[1]
        for p in people:
            que.insert(p[1], p) 
        
        return que

452.Minimum Number of Arrows to Burst Balloons

Overlapping intervals. If the ith interval's left boundary is greater than the i-1th interval's right boundary, we need one more arrow. Otherwise, no new arrow is needed, and we need to see if the i+1th interval's left boundary is smaller than the minimum of i-1 and ith right boundary. This is updating the new right boundary for i-1 and ith interval.

class Solution:
    def findMinArrowShots(self, points: List[List[int]]) -> int:
        if len(points) == 0: return 0
        points.sort(key = lambda x: x[0])
        result = 1
        for i in range(1, len(points)):
            if points[i][0] > points[i-1][1]:   #baloon i and i-1 have no overlapping interval
                result += 1
            else:
                points[i][1] = min(points[i-1][1], points[i][1])
        return result

lc-python29's People

Contributors

gyjbb avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.