首页 > 其他分享 >力扣2173题

力扣2173题

时间:2024-08-01 19:26:24浏览次数:8  
标签:player Matches id 力扣 2173 result day match

力扣之2173. 最多连胜的次数

说明

跳转

准备工作

drop database if exists db_1;
create database db_1;
use db_1;


Create table If Not Exists Matches (player_id int, match_day date, result ENUM('Win', 'Draw', 'Lose'));
Truncate table Matches;
insert into Matches (player_id, match_day, result) values ('1', '2022-01-17', 'Win');
insert into Matches (player_id, match_day, result) values ('1', '2022-01-18', 'Win');
insert into Matches (player_id, match_day, result) values ('1', '2022-01-25', 'Win');
insert into Matches (player_id, match_day, result) values ('1', '2022-01-31', 'Draw');
insert into Matches (player_id, match_day, result) values ('1', '2022-02-08', 'Win');
insert into Matches (player_id, match_day, result) values ('2', '2022-02-06', 'Lose');
insert into Matches (player_id, match_day, result) values ('2', '2022-02-08', 'Lose');
insert into Matches (player_id, match_day, result) values ('3', '2022-03-30', 'Win');
分析一

暂时无法在飞书文档外展示此内容

实现一
drop database if exists db_1;
create database db_1;
use db_1;


Create table If Not Exists Matches (player_id int, match_day date, result ENUM('Win', 'Draw', 'Lose'));
Truncate table Matches;
insert into Matches (player_id, match_day, result) values ('1', '2022-01-17', 'Win');
insert into Matches (player_id, match_day, result) values ('1', '2022-01-18', 'Win');
insert into Matches (player_id, match_day, result) values ('1', '2022-01-25', 'Win');
insert into Matches (player_id, match_day, result) values ('1', '2022-01-31', 'Draw');
insert into Matches (player_id, match_day, result) values ('1', '2022-02-08', 'Win');
insert into Matches (player_id, match_day, result) values ('2', '2022-02-06', 'Lose');
insert into Matches (player_id, match_day, result) values ('2', '2022-02-08', 'Lose');
insert into Matches (player_id, match_day, result) values ('3', '2022-03-30', 'Win');

todo 题目要求: 选手的 连胜数 是指连续获胜的次数,且没有被平局或输球中断。

todo 编写解决方案来计算每个参赛选手最多的连胜数。

todo 结果可以以 任何顺序 返回。

– 第一步: 构建两个等差数据

with t1 as (
    select
        player_id, match_day, result,
        row_number() over (partition by player_id order by match_day) as rn1,
        row_number() over (partition by player_id, result order by match_day) as rn2
    from matches
)
select
    player_id, match_day, result, rn1, rn2,
    (rn1 - rn2) as diff,
    if(result='Win', 1, 0) result2
from t1
;

– 第一步: 构建两个等差数据

with t1 as (
    select
        player_id, match_day, result,
        row_number() over (partition by player_id order by match_day) as rn1,
        row_number() over (partition by player_id, result order by match_day) as rn2
    from matches
)
, t2 as (
    select
        player_id,
        (rn1 - rn2) as diff,
        sum(if(result='Win', 1, 0)) as cnt
    from t1
    group by player_id, (rn1 - rn2)
)
select
    player_id,
    max(cnt) as longest_streak
from t2
group by player_id
;

实现二

进阶: 如果我们想计算最长的连续不输的次数(即获胜或平局),你将如何调整?

with t1 as (
    select
        player_id, match_day, result,
        row_number() over (partition by player_id order by match_day) as rn1,
        row_number() over (partition by player_id, if(result!='Lose', 'Not_Lose', 'Lose') order by match_day) as rn2
    from matches
)
, t2 as (
    select
        player_id,
        (rn1 - rn2) as diff,
        sum(if(result!='Lose', 1, 0)) cnt
    from t1
    group by player_id, (rn1 - rn2)
)
select
    player_id,
    max(cnt) as 最大不输的次数
from t2
group by player_id
;

小结

  • 连续
    • 公式: 构建两个公差为1的等差数列
    • 注意同一组差值相等的, 是连续的

标签:player,Matches,id,力扣,2173,result,day,match
From: https://blog.csdn.net/weixin_54514072/article/details/140854269

相关文章

  • 力扣题解2-两数相加
    问题的描述如下:分析过程:为了解决这个问题,我们需要逐位相加两个链表对应位置的值,并处理进位问题。具体步骤如下:初始化一个新的链表,用于存储结果。使用两个指针遍历两个输入链表,逐位相加,同时考虑进位。如果一个链表比另一个短,则将其视为0进行计算。处理最后可能存在的进位......
  • 最细哈希表相关的力扣题和讲解和Java、C++常用的数据结构(哈希法)来源于代码随想录,十分
    20240725一、什么时候适用什么样的结构。1.java中1.1HashSet:1.2TreeSet:1.3LinkedHashSet:1.4HashMap:1.5TreeMap:1.6LinkedHashMap:1.7总结2.c++中2.1std::unordered_set:2.2std::set:2.3std::multiset:2.4std::unordered_map:2.5std::map:2.6std::multimap:3代码......
  • LeetCode_sql_day07(579. 查询员工的累计薪水,2173.最多连胜的次数)
    描述:579.查询员工的累计薪水编写一个解决方案,在一个统一的表中计算出每个员工的 累计工资汇总 。员工的 累计工资汇总 可以计算如下:对于该员工工作的每个月,将 该月 和 前两个月 的工资 加 起来。这是他们当月的 3个月总工资和 。如果员工在前几个月没有为公......
  • 力扣高频SQL 50题(基础版)第二十题
    文章目录力扣高频SQL50题(基础版)第二十题2356.每位教师所教授的科目种类的数量题目说明思路分析实现过程准备数据实现方式结果截图力扣高频SQL50题(基础版)第二十题2356.每位教师所教授的科目种类的数量题目说明表:Teacher±------------±-----+|ColumnName......
  • 力扣90题:子集II的 Java 实现
    引言LeetCode是一个流行的在线编程平台,提供了大量的算法题目供开发者练习。第90题“子集II”是一个中等难度的题目,要求找出数组的所有子集,但是含重复数字的子集只计算一次。本文将介绍如何使用Java解决这个问题。题目描述给定一个可能包含重复数字的整数数组nums,返回......
  • 力扣-415-字符串相加
    思路是模拟从低位到高位的按位相加,需要考虑进位publicStringaddStrings(Stringnum1,Stringnum2){intbitLen1=num1.length()-1,bitLen2=num2.length()-1;StringBuilderstringBuilder=newStringBuilder();intcarry=0;......
  • 算法力扣刷题记录 五十八【701.二叉搜索树中的插入操作】
    前言本文是二叉搜索树操作。二叉树篇继续。一、题目阅读给定二叉搜索树(BST)的根节点root和要插入树中的值value,将值插入二叉搜索树。返回插入后二叉搜索树的根节点。输入数据保证,新值和原始二叉搜索树中的任意节点值都不同。注意,可能存在多种有效的插入方式,只......
  • 力扣题解1-两数之和
    LeetCode第一题"两数之和"(TwoSum)问题分析过程:这个问题可以通过多种方法解决,包括暴力解法和使用哈希表的解法。以下是详细的分析过程:暴力解法:遍历数组中的每一对元素,检查它们的和是否等于目标值。时间复杂度是O(n^2),其中n是数组的长度。使用哈希表:使用一......
  • 贪心算法(二) ------力扣860--柠檬水找零,力扣605--种花问题
    目录力扣860----柠檬水找零题目分析 代码 力扣605---种花问题问题题目分析 代码 力扣860----柠檬水找零题目在柠檬水摊上,每一杯柠檬水的售价为 5 美元。顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯。每位顾客只买一杯柠檬水,然后向你......
  • 力扣3226 使两个整数相等的位更改次数
    写的代码:classSolution{public:stringcc(intnum){stringres="";while(num>0){intr=num%2;res=static_cast<char>(48+r)+res;num/=2;}returnres;}int......