C++
Java
Python
Python3
C
C#
JavaScript
Ruby
Swift
Go
Scala
Kotlin
Rust
PHP
TypeScript
Racket
Erlang
Elixir
Dart
monokai
ambiance
chaos
chrome
cloud9_day
cloud9_night
cloud9_night_low_color
clouds
clouds_midnight
cobalt
crimson_editor
dawn
dracula
dreamweaver
eclipse
github
github_dark
gob
gruvbox
gruvbox_dark_hard
gruvbox_light_hard
idle_fingers
iplastic
katzenmilch
kr_theme
kuroir
merbivore
merbivore_soft
mono_industrial
nord_dark
one_dark
pastel_on_dark
solarized_dark
solarized_light
sqlserver
terminal
textmate
tomorrow
tomorrow_night
tomorrow_night_blue
tomorrow_night_bright
tomorrow_night_eighties
twilight
vibrant_ink
xcode
上次编辑到这里,代码来自缓存 点击恢复默认模板
class SORTracker {
public:
SORTracker() {
}
void add(string name, int score) {
}
string get() {
}
};
/**
* Your SORTracker object will be instantiated and called as such:
* SORTracker* obj = new SORTracker();
* obj->add(name,score);
* string param_2 = obj->get();
*/
运行代码
提交
java 解法, 执行用时: 286 ms, 内存消耗: 65 MB, 提交时间: 2023-09-11 15:04:15
class SORTracker {
List<String> names = new ArrayList<>();
List<Integer> scores = new ArrayList<>();
Comparator<Integer> comparator = Comparator.comparing(scores::get)
.thenComparing(Comparator.comparing(names::get).reversed());
Queue<Integer> minHeap = new PriorityQueue<>(comparator);
Queue<Integer> maxHeap = new PriorityQueue<>(comparator.reversed());
public SORTracker() {
}
public void add(String name, int score) {
names.add(name);
scores.add(score);
minHeap.offer(names.size() - 1);
maxHeap.offer(minHeap.poll());
}
public String get() {
minHeap.offer(maxHeap.poll());
return names.get(minHeap.peek());
}
}
/**
* Your SORTracker object will be instantiated and called as such:
* SORTracker obj = new SORTracker();
* obj.add(name,score);
* String param_2 = obj.get();
*/
cpp 解法, 执行用时: 472 ms, 内存消耗: 145.8 MB, 提交时间: 2023-09-11 15:03:29
class SORTracker {
set<pair<int, string>> s;
set<pair<int, string>>::iterator cur;
public:
SORTracker() {
s.emplace(0, ""); // 哨兵
cur = s.begin();
}
void add(string name, int score) {
pair<int, string> p = {-score, name};
s.emplace(p);
if (p < *cur) --cur;
}
string get() {
return cur++->second;
}
};
/**
* Your SORTracker object will be instantiated and called as such:
* SORTracker* obj = new SORTracker();
* obj->add(name,score);
* string param_2 = obj->get();
*/
python3 解法, 执行用时: 636 ms, 内存消耗: 39.5 MB, 提交时间: 2023-09-11 15:02:34
from sortedcontainers import SortedList
class SORTracker:
def __init__(self):
self.d = SortedList()
self.i = 0
def add(self, name: str, score: int) -> None:
self.d.add((-score, name))
def get(self) -> str:
self.i += 1
return self.d[self.i - 1][1]
# Your SORTracker object will be instantiated and called as such:
# obj = SORTracker()
# obj.add(name,score)
# param_2 = obj.get()
golang 解法, 执行用时: 356 ms, 内存消耗: 21.1 MB, 提交时间: 2023-09-11 15:01:49
type pair struct {
score int
name string
}
func compare(x, y interface{}) int {
a, b := x.(pair), y.(pair)
if a.score > b.score || a.score == b.score && a.name < b.name {
return -1
}
return 1
}
type SORTracker struct {
*redblacktree.Tree
cur redblacktree.Iterator
}
func Constructor() SORTracker {
root := redblacktree.NewWith(compare)
root.Put(pair{}, nil) // 哨兵
return SORTracker{root, root.IteratorAt(root.Left())}
}
func (t *SORTracker) Add(name string, score int) {
p := pair{score, name}
t.Put(p, nil)
if compare(p, t.cur.Key()) < 0 {
t.cur.Prev() // 移动至前一个元素
}
}
func (t *SORTracker) Get() string {
name := t.cur.Key().(pair).name
t.cur.Next() // 移动至下一个元素
return name
}
/**
* Your SORTracker object will be instantiated and called as such:
* obj := Constructor();
* obj.Add(name,score);
* param_2 := obj.Get();
*/