[HackerRank]Climbing the Leaderboard解题思路分享

本文分享了HackerRank上一题名为'Climbing the Leaderboard'的解题思路,讨论了如何将时间复杂度从o(i*j)优化到o(i+j)。通过分析题目,利用分数的降序和Alice分数的升序特性,提出了一种新的解决方案,实现空间复杂度为o(1),并给出了具体的代码实现。
摘要由CSDN通过智能技术生成

Climbing the Leaderboard

题目分析

在HackerRank上看到了一道Medium的题,难度不高,但在时间复杂度和空间复杂度上都有许多可以优化的地方,同时看了leaderboard上前几个没有这种做法的,时间复杂度实现了o(i*j)到o(i+j)的优化,所以简单记录一下。

题目

在这里插入图片描述
题目大意:
这是一个游戏机分数排名系统,按照分数降序排名,同分记同一名。
Alice在玩游戏机时游戏机已经有n个玩家的分数scores[i],Alice玩了m次游戏,她玩过这m次游戏后的总分数为alice[j]。
要求输出m个整数,第j个数代表玩过j次时的名词。
其中,题目规定scores[i]为降序,alice[j]为升序。

例子

在这里插入图片描述
初始有七次游戏记录,排名如最后的图。
在这里插入图片描述
在进行第一次游戏后排名如图。
在这里插入图片描述
第二次后。
后几次不一一列举。

解题

看过题,最初的思路是用链表把scores去重添加后,再依次查询alice[j]的名词。
空间复杂度o(i),时间复杂度o(i*j)。
自己提交了几次总是在几个例子上超时,我就看了看Leaderboard上的java8的代码,和我几乎没区别,只是他几年前提交的,没用官方的IO方式,怀疑是因为hackerrank官方IO方式导致的超时。
就放下他代码的链接吧,思路很朴实。
link.

所以我重新设计了个算法:
主要利用了scores递减,alice递增的特性。
同时对scores和alice扫描,scores从前向后扫描,alice从后向前扫描,在scores[i]和alice[j]中选大的值。
那么扫描的过程就是降序的,就可以通过对扫描过的分数计数即为排名n,同时我们单独对扫描过的alice的分数计数排名m。
第j次游戏后的,alice[j] = n - m;
空间复杂度o(1),时间复杂度o(i+j)。

代码实现

    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
leetcode 凑硬币 :open_file_folder: 算法 :page_facing_up: :open_file_folder: 黑客等级 :open_file_folder: 数组 :open_file_folder: 2_-_中 :page_facing_up: :open_file_folder: 堆 :open_file_folder: 1_-_简单 :page_facing_up: :open_file_folder: Linked_Lists :open_file_folder: 2_-_中 :page_facing_up: :open_file_folder: 队列 :open_file_folder: 3_-_硬 :page_facing_up: :open_file_folder: 递归 :open_file_folder: 2_-_中 :page_facing_up: :open_file_folder: 排序 :open_file_folder: 2_-_中 :page_facing_up: :open_file_folder: 堆栈 :open_file_folder: 3_-_硬 :page_facing_up: :page_facing_up: :open_file_folder: 力码 :open_file_folder: 大批 :open_file_folder: 1_-_简单 :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :open_file_folder: 2_-_中 :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :open_file_folder: 3_-_硬 :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :open_file_folder: 位操作 :open_file_folder: 1_-_简单 :page_facing_up: :open_file_folder: DP :open_file_folder: 1_-_简单 :page_facing_up: :open_file_folder: 2_-_中 :page_facing_up: :page_facing_up: :page_facing_up: :page_facing_up: :open_file_folder: 深度优先_搜索,广度优先_搜索 :open_file_folder: 1_-_简单 :page_facing_up: :open_file_folder: 2_-_中
Unity中有多个Climbing System插件,但最流行和常用的是Climbing System Pro插件。下面是Climbing System Pro插件的文档: 1. 简介 Climbing System Pro是一款Unity插件,用于快速创建高品质的爬山系统。它支持多种爬山类型,包括攀岩、墙壁攀登、梯子攀爬等。Climbing System Pro具有一些强大的功能,例如手部IK、动画融合、多个爬山点的支持等。 2. 安装 您可以在Unity Asset Store中找到Climbing System Pro插件并进行安装。安装完成后,您可以在Unity编辑器中看到Climbing System Pro的菜单选项。 3. 快速入门 要创建一个基本的爬山系统,您需要执行以下步骤: - 创建一个包含Mesh Collider组件的游戏对象,用于表示可爬物体。 - 将Climber组件添加到要进行爬山的角色上。 - 在Climber组件中选择“Add Climbing Point”选项,添加一个或多个爬山点。 - 在Climber组件中选择“Set Climbing Type”选项,选择爬山类型(例如攀岩、墙壁攀登等)。 - 按下游戏中的特定按键,以开始爬山。 4. 高级用法 Climbing System Pro还具有一些高级功能,例如动画融合、手部IK等。您可以通过查看Climbing System Pro的文档来了解更多信息。 5. 支持 如果您在使用Climbing System Pro时遇到了任何问题,您可以查看官方文档或联系开发者以获取支持。 以上是Climbing System Pro插件的简要文档,希望能够帮助您了解如何使用该插件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值