zoukankan      html  css  js  c++  java
  • 1204. Last Person to Fit in the Elevator

    Table: Queue

    +-------------+---------+
    | Column Name | Type |
    +-------------+---------+
    | person_id | int |
    | person_name | varchar |
    | weight | int |
    | turn | int |
    +-------------+---------+
    person_id is the primary key column for this table.
    This table has the information about all people waiting for an elevator.
    The person_id and turn columns will contain all numbers from 1 to n, where n is the number of rows in the table.
     

    The maximum weight the elevator can hold is 1000.

    Write an SQL query to find the person_name of the last person who will fit in the elevator without exceeding the weight limit. It is guaranteed that the person who is first in the queue can fit in the elevator.

    The query result format is in the following example:

    Queue table
    +-----------+-------------------+--------+------+
    | person_id | person_name | weight | turn |
    +-----------+-------------------+--------+------+
    | 5 | George Washington | 250 | 1 |
    | 3 | John Adams | 350 | 2 |
    | 6 | Thomas Jefferson | 400 | 3 |
    | 2 | Will Johnliams | 200 | 4 |
    | 4 | Thomas Jefferson | 175 | 5 |
    | 1 | James Elephant | 500 | 6 |
    +-----------+-------------------+--------+------+

    Result table
    +-------------------+
    | person_name |
    +-------------------+
    | Thomas Jefferson |
    +-------------------+

    Queue table is ordered by turn in the example for simplicity.
    In the example George Washington(id 5), John Adams(id 3) and Thomas Jefferson(id 6) will enter the elevator as their weight sum is 250 + 350 + 400 = 1000.
    Thomas Jefferson(id 6) is the last person to fit in the elevator because he has the last turn in these three people.

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/last-person-to-fit-in-the-elevator
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    # Write your MySQL query statement below

    select t1.person_name from Queue t1 where 
    (
    (select sum(weight) from Queue t2 where t2.turn<=t1.turn)<=1000
     and  (select sum(weight) from Queue t2 where t2.turn<=t1.turn+1)>1000  )
     or 
     ( (select sum(weight) from Queue)<=1000  and t1.turn = (select max(turn) from Queue) )
  • 相关阅读:
    navigateTo防止多次跳转
    vue中的绑定class和微信小程序中的绑定class的区别
    js同步和异步
    本地存储和vuex使用对比
    微信小程序页面跳转区别总结
    CAS-技术专区-认证服务器cas-server搭建
    CAS-技术专区-SSO配置完整案例(静态认证+数据库认证)
    SpringCloud-技术专区-实战案例-Zuul整合OAuth2.0认证服务
    OAuth2.0协议专区-SpringCloud安全-集成OAuth2实现身份认证和单点登录
    OAuth2.0协议专区-SpringCloud微服务实战-基于OAUTH2.0统一认证授权的微服务基础架构
  • 原文地址:https://www.cnblogs.com/leeeee/p/11902039.html
Copyright © 2011-2022 走看看