【MarsCode】每日一题 (集合)之 游戏排名第三大的分数

游戏排名第三大的分数

1.问题描述

小M想要通过查看往届游戏比赛的排名来确定自己比赛的目标分数。他希望找到往届比赛中排名第三的分数,作为自己的目标。具体规则如下:

  1. 如果分数中有三个或以上不同的分数,返回其中第三大的分数。
  2. 如果不同的分数只有两个或更少,那么小M将选择最大的分数作为他的目标。

请你帮小M根据给定的分数数组计算目标分数。


测试样例

样例1:

输入:n = 3,nums = [3, 2, 1] 输出:1

样例2:

输入:n = 2,nums = [1, 2] 输出:2

样例3:

输入:n = 4,nums = [2, 2, 3, 1] 输出:1

2.思路与题解

我们需要找到给定分数数组中排名第三的分数。如果不同的分数少于三个,则返回最大的分数。

数据结构选择

  1. Set: 用于存储不同的分数。Set 可以自动去重,非常适合处理这个问题。
  2. List: 用于存储排序后的分数。List 可以方便地进行排序和访问。

算法步骤

  1. 去重: 使用 Set 存储分数,自动去除重复的分数。

  2. 排序: 将 Set 转换为 List,并按降序排序。

  3. 返回结果

    :

    • 如果 List 的大小大于等于 3,返回第三大的分数。
    • 否则,返回最大的分数。

2.4代码框架

Java

java
复制代码
import java.util.*; public class Main { public static int solution(int n, int[] nums) { // 使用 Set 去重 Set<Integer> uniqueScores = new HashSet<>(); for (int num : nums) { uniqueScores.add(num); } // 将 Set 转换为 List 并排序 List<Integer> sortedScores = new ArrayList<>(uniqueScores); Collections.sort(sortedScores, Collections.reverseOrder()); // 返回结果 if (sortedScores.size() >= 3) { return sortedScores.get(2); // 第三大的分数 } else { return sortedScores.get(0); // 最大的分数 } } public static void main(String[] args) { System.out.println(solution(3, new int[]{3, 2, 1}) == 1); System.out.println(solution(2, new int[]{1, 2}) == 2); System.out.println(solution(4, new int[]{2, 2, 3, 1}) == 1); } }

C++

c++
复制代码
#include <iostream> #include <vector> #include <set> #include <algorithm> int solution(int n, std::vector<int>& nums) { // 使用 set 去重 std::set<int> uniqueScores; for (int num : nums) { uniqueScores.insert(num); } // 将 set 转换为 vector 并排序 std::vector<int> sortedScores(uniqueScores.begin(), uniqueScores.end()); std::sort(sortedScores.rbegin(), sortedScores.rend()); // 降序排序 // 返回结果 if (sortedScores.size() >= 3) { return sortedScores[2]; // 第三大的分数 } else { return sortedScores[0]; // 最大的分数 } } int main() { std::vector<int> nums1 = {3, 2, 1}; std::vector<int> nums2 = {1, 2}; std::vector<int> nums3 = {2, 2, 3, 1}; std::cout << (solution(3, nums1) == 1) << std::endl; std::cout << (solution(2, nums2) == 2) << std::endl; std::cout << (solution(4, nums3) == 1) << std::endl; return 0; }

Python

python
复制代码
def solution(n, nums): # 使用 set 去重 unique_scores = set(nums) # 将 set 转换为 list 并排序 sorted_scores = sorted(unique_scores, reverse=True) # 返回结果 if len(sorted_scores) >= 3: return sorted_scores[2] # 第三大的分数 else: return sorted_scores[0] # 最大的分数 # 测试用例 print(solution(3, [3, 2, 1]) == 1) print(solution(2, [1, 2]) == 2) print(solution(4, [2, 2, 3, 1]) == 1)

Golang

go
复制代码
package main import ( "fmt" "sort" ) func solution(n int, nums []int) int { // 使用 map 去重 uniqueScores := make(map[int]bool) for _, num := range nums { uniqueScores[num] = true } // 将 map 的 key 转换为 slice 并排序 var sortedScores []int for num := range uniqueScores { sortedScores = append(sortedScores, num) } sort.Sort(sort.Reverse(sort.IntSlice(sortedScores))) // 返回结果 if len(sortedScores) >= 3 { return sortedScores[2] // 第三大的分数 } else { return sortedScores[0] // 最大的分数 } } func main() { fmt.Println(solution(3, []int{3, 2, 1}) == 1) fmt.Println(solution(2, []int{1, 2}) == 2) fmt.Println(solution(4, []int{2, 2, 3, 1}) == 1) }

2.5一些疑难的代码解释

  • 去重: 使用 Set 去重。

  • 排序: 使用 Collections.sort 进行降序排序。

  • 返回结果: 根据 List 的大小返回第三大的分数或最大的分数。

详细说明

  • Collections.sort(sortedScores, Collections.reverseOrder());

    • Collections.sort 是 Java 标准库中的一个方法,用于对列表进行排序。
    • sortedScores 是一个 List<Integer>,即一个整数列表。
    • Collections.reverseOrder() 是一个比较器(Comparator),它返回一个比较器,该比较器会对元素进行降序排序。
  • Collections.sort:

    • 这个方法有两个重载形式:
      • Collections.sort(List<T> list):对列表进行自然顺序(升序)排序。
      • Collections.sort(List<T> list, Comparator<? super T> c):对列表按照指定的比较器进行排序。
  • Collections.reverseOrder():

    • 这个方法返回一个比较器,该比较器会对元素进行降序排序。
    • 例如,如果你有一个整数列表 [3, 1, 2],使用 Collections.reverseOrder() 进行排序后,列表将变为 [3, 2, 1]
  • 代码示例

    假设 sortedScores 初始值为 [3, 1, 2],那么执行这段代码后,sortedScores 将变为 [3, 2, 1]

    java
    复制代码
    List<Integer> sortedScores = new ArrayList<>(Arrays.asList(3, 1, 2)); Collections.sort(sortedScores, Collections.reverseOrder()); System.out.println(sortedScores); // 输出: [3, 2, 1]

3.欢迎大佬们关注或莅临本渣的一些个人website

gitee: https://gitee.com/xiao-chenago github:https://github.com/cool-icu0 语雀:https://www.yuque.com/icu0 csdn:https://cool-icu.blog.csdn.net/

0个评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
下载 APP