当前位置: 首页 > news >正文

【java|golang】多字段排序以及排序规则

  1. 奖励最顶尖的 K 名学生

给你两个字符串数组 positive_feedback 和 negative_feedback ,分别包含表示正面的和负面的词汇。不会 有单词同时是正面的和负面的。

一开始,每位学生分数为 0 。每个正面的单词会给学生的分数 加 3 分,每个负面的词会给学生的分数 减 1 分。

给你 n 个学生的评语,用一个下标从 0 开始的字符串数组 report 和一个下标从 0 开始的整数数组 student_id 表示,其中 student_id[i] 表示这名学生的 ID ,这名学生的评语是 report[i] 。每名学生的 ID 互不相同。

给你一个整数 k ,请你返回按照得分 从高到低 最顶尖的 k 名学生。如果有多名学生分数相同,ID 越小排名越前。

示例 1:

输入:positive_feedback = [“smart”,“brilliant”,“studious”], negative_feedback = [“not”], report = [“this student is studious”,“the student is smart”], student_id = [1,2], k = 2
输出:[1,2]
解释:
两名学生都有 1 个正面词汇,都得到 3 分,学生 1 的 ID 更小所以排名更前。
示例 2:

输入:positive_feedback = [“smart”,“brilliant”,“studious”], negative_feedback = [“not”], report = [“this student is not studious”,“the student is smart”], student_id = [1,2], k = 2
输出:[2,1]
解释:

  • ID 为 1 的学生有 1 个正面词汇和 1 个负面词汇,所以得分为 3-1=2 分。
  • ID 为 2 的学生有 1 个正面词汇,得分为 3 分。
    学生 2 分数更高,所以返回 [2,1] 。

提示:

1 <= positive_feedback.length, negative_feedback.length <= 104
1 <= positive_feedback[i].length, negative_feedback[j].length <= 100
positive_feedback[i] 和 negative_feedback[j] 都只包含小写英文字母。
positive_feedback 和 negative_feedback 中不会有相同单词。
n == report.length == student_id.length
1 <= n <= 104
report[i] 只包含小写英文字母和空格 ’ ’ 。
report[i] 中连续单词之间有单个空格隔开。
1 <= report[i].length <= 100
1 <= student_id[i] <= 109
student_id[i] 的值 互不相同 。
1 <= k <= n

public List<Integer> topStudents(String[] positive_feedback, String[] negative_feedback, String[] report, int[] student_id, int k) {Map<Integer, Integer> map = new HashMap<>();Map<String, Integer> feedback = new HashMap<>();for (String s : positive_feedback) {feedback.put(s,3);}for (String s : negative_feedback) {feedback.put(s,-1);}for (int i = 0; i < report.length; i++) {String[] split = report[i].split(" ");int sum=0;for (String s : split) {sum+=feedback.getOrDefault(s,0);}map.put(student_id[i],sum);}Comparator<Map.Entry<Integer, Integer>> comparator = (o1, o2) -> {Integer o1Value = o1.getValue();Integer o2Value = o2.getValue();return o1Value==o2Value?o1.getKey()-o2.getKey():o2Value-o1Value;};List<Map.Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet());Collections.sort(list,comparator);List<Integer> res = new ArrayList<>();for (int i = 0; i < k; i++) {res.add(list.get(i).getKey());}return res;}

在这里插入图片描述

public List<Integer> topStudents(String[] positive_feedback, String[] negative_feedback, String[] report, int[] student_id, int k) {PriorityQueue<int[]> queue = new PriorityQueue<>((o1, o2) -> o1[1]==o2[1]?o1[0]-o2[0]:o2[1]-o1[1]);Map<String, Integer> feedback = new HashMap<>();for (String s : positive_feedback) {feedback.put(s,3);}for (String s : negative_feedback) {feedback.put(s,-1);}for (int i = 0; i < report.length; i++) {String[] split = report[i].split(" ");int sum=0;for (String s : split) {sum+=feedback.getOrDefault(s,0);}queue.add(new int[]{student_id[i], sum});}List<Integer> res = new ArrayList<>();for (int i = 0; i < k; i++) {res.add(queue.poll()[0]);}return res;}

在这里插入图片描述

func topStudents(positive_feedback []string, negative_feedback []string, report []string, student_id []int, k int) []int {ints := make([][2]int, 0)feedback:=make(map[string]int,0)for _, v := range positive_feedback {feedback[v]=3}for _, v := range negative_feedback {feedback[v]=-1}for i := 0; i < len(report); i++ {split:=strings.Split(report[i]," ")sum:=0for _, v := range split {sum+=feedback[v]}ints=append(ints, [2]int{student_id[i], sum})}sort.Slice(ints, func(i, j int) bool {if ints[i][1]==ints[j][1] {return ints[j][0]>ints[i][0]}return ints[i][1]>ints[j][1]})res := make([]int,0)for i := 0; i < k; i++ {res= append(res, ints[i][0])}return res
}

在这里插入图片描述

排序规则

语言排序规则升序降序
Java(o1,o2)o1-o2o2-o1
golang(i,j)i<ji>j
http://www.lryc.cn/news/192317.html

相关文章:

  • 腾讯云 轻量云 上海 VPS 测评
  • 消息称苹果或在明年推出搭载M3芯片的MacBook产品
  • Generalizable NeRF in ICCV‘23
  • Unity2017适配安卓12
  • ios UI 基础开发一
  • echarts一些配置项的使用
  • python yaml库:safe_load()(安全解析函数,解析yaml)(防止yaml文件中包含恶意代码)
  • 小程序:下拉刷新+上拉加载+自定义导航栏
  • 判断两个二叉树是否相等
  • springcloud----检索中间件 ElasticSearch 分布式场景的运用
  • qt创建线程类并实现通信 C++
  • 【elasticsearch】使用自建证书搭建elasticsearch8.0.1集群
  • 一篇文章带你用动态规划解决打家劫舍问题
  • idea中导入eclipse的javaweb项目——tomact服务(保姆级别)
  • 【开源】给ChatGLM写个,Java对接的SDK
  • 基于Pytest+Allure+Excel的接口自动化测试框架
  • 20.2 FMC驱动SDRAM的时序初始化实现及内存测试
  • 联想电脑一键重装系统Win10操作方法
  • Mysql数据库 1.概述
  • Qt编程,文件操作、UDP通信
  • Docker 的数据管理和Dockerfile镜像的创建
  • [python] 利用 Pydoc 快速生成整个 Python 项目的文档
  • Maven 配置指南
  • 第十八章 类和对象——多态
  • 京东数据平台:2023年服饰行业销售数据分析
  • Nginx proxy_set_header参数设置
  • 如何用 ChatGPT 的 Advanced Data Analysis 帮你采集数据?
  • Linux运行环境搭建系列-Flink安装
  • 求最大bit数(java)
  • 【Java 进阶篇】JavaScript 与 HTML 的结合方式