Skip to content

Commit 668fb22

Browse files
committed
+ problem 2102
1 parent 89498fb commit 668fb22

File tree

5 files changed

+191
-0
lines changed

5 files changed

+191
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# 2102. Sequentially Ordinal Rank Tracker
2+
A scenic location is represented by its `name` and attractiveness `score`, where `name` is a **unique** string among all locations and `score` is an integer. Locations can be ranked from the best to the worst. The **higher** the score, the better the location. If the scores of two locations are equal, then the location with the **lexicographically smaller** name is better.
3+
4+
You are building a system that tracks the ranking of locations with the system initially starting with no locations. It supports:
5+
6+
* **Adding** scenic locations, **one at a time**.
7+
* **Querying** the <code>i<sup>th</sup></code> **best** location of **all locations already added**, where `i` is the number of times the system has been queried (including the current query).
8+
* For example, when the system is queried for the <code>4<sup>th</sup></code> time, it returns the <code>4<sup>th</sup></code> best location of all locations already added.
9+
10+
Note that the test data are generated so that **at any time**, the number of queries **does not exceed** the number of locations added to the system.
11+
12+
Implement the `SORTracker` class:
13+
14+
* `SORTracker()` Initializes the tracker system.
15+
* `void add(string name, int score)` Adds a scenic location with `name` and `score` to the system.
16+
* `string get()` Queries and returns the <code>i<sup>th</sup></code> best location, where `i` is the number of times this method has been invoked (including this invocation).
17+
18+
#### Example 1:
19+
<pre>
20+
<strong>Input:</strong>
21+
["SORTracker", "add", "add", "get", "add", "get", "add", "get", "add", "get", "add", "get", "get"]
22+
[[], ["bradford", 2], ["branford", 3], [], ["alps", 2], [], ["orland", 2], [], ["orlando", 3], [], ["alpine", 2], [], []]
23+
<strong>Output:</strong>
24+
[null, null, null, "branford", null, "alps", null, "bradford", null, "bradford", null, "bradford", "orland"]
25+
<strong>Explanation:</strong>
26+
SORTracker tracker = new SORTracker(); // Initialize the tracker system.
27+
tracker.add("bradford", 2); // Add location with name="bradford" and score=2 to the system.
28+
tracker.add("branford", 3); // Add location with name="branford" and score=3 to the system.
29+
tracker.get(); // The sorted locations, from best to worst, are: branford, bradford.
30+
// Note that branford precedes bradford due to its higher score (3 > 2).
31+
// This is the 1st time get() is called, so return the best location: "branford".
32+
tracker.add("alps", 2); // Add location with name="alps" and score=2 to the system.
33+
tracker.get(); // Sorted locations: branford, alps, bradford.
34+
// Note that alps precedes bradford even though they have the same score (2).
35+
// This is because "alps" is lexicographically smaller than "bradford".
36+
// Return the 2nd best location "alps", as it is the 2nd time get() is called.
37+
tracker.add("orland", 2); // Add location with name="orland" and score=2 to the system.
38+
tracker.get(); // Sorted locations: branford, alps, bradford, orland.
39+
// Return "bradford", as it is the 3rd time get() is called.
40+
tracker.add("orlando", 3); // Add location with name="orlando" and score=3 to the system.
41+
tracker.get(); // Sorted locations: branford, orlando, alps, bradford, orland.
42+
// Return "bradford".
43+
tracker.add("alpine", 2); // Add location with name="alpine" and score=2 to the system.
44+
tracker.get(); // Sorted locations: branford, orlando, alpine, alps, bradford, orland.
45+
// Return "bradford".
46+
tracker.get(); // Sorted locations: branford, orlando, alpine, alps, bradford, orland.
47+
// Return "orland".
48+
</pre>
49+
50+
#### Constraints:
51+
* `name` consists of lowercase English letters, and is unique among all locations.
52+
* `1 <= name.length <= 10`
53+
* <code>1 <= score <= 10<sup>5</sup></code>
54+
* At any time, the number of calls to `get` does not exceed the number of calls to `add`.
55+
* At most <code>4 * 10<sup>4</sup></code> calls **in total** will be made to `add` and `get`.
56+
57+
## Solutions (Python)
58+
59+
### 1. Solution
60+
```Python
61+
from sortedcontainers import SortedList
62+
63+
64+
class SORTracker:
65+
66+
def __init__(self):
67+
self.locations = SortedList()
68+
self.i = 0
69+
70+
def add(self, name: str, score: int) -> None:
71+
self.locations.add((-score, name))
72+
73+
def get(self) -> str:
74+
self.i += 1
75+
76+
return self.locations[self.i - 1][1]
77+
78+
# Your SORTracker object will be instantiated and called as such:
79+
# obj = SORTracker()
80+
# obj.add(name,score)
81+
# param_2 = obj.get()
82+
```
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# 2102. 序列顺序查询
2+
一个观光景点由它的名字 `name` 和景点评分 `score` 组成,其中 `name` 是所有观光景点中 **唯一** 的字符串,`score` 是一个整数。景点按照最好到最坏排序。景点评分 **越高** ,这个景点越好。如果有两个景点的评分一样,那么 **字典序较小** 的景点更好。
3+
4+
你需要搭建一个系统,查询景点的排名。初始时系统里没有任何景点。这个系统支持:
5+
6+
* **添加** 景点,每次添加 **一个** 景点。
7+
* **查询** 已经添加景点中第 `i` **** 的景点,其中 `i` 是系统目前位置查询的次数(包括当前这一次)。
8+
* 比方说,如果系统正在进行第 `4` 次查询,那么需要返回所有已经添加景点中第 `4` 好的。
9+
10+
注意,测试数据保证 **任意查询时刻** ,查询次数都 **不超过** 系统中景点的数目。
11+
12+
请你实现 `SORTracker` 类:
13+
14+
* `SORTracker()` 初始化系统。
15+
* `void add(string name, int score)` 向系统中添加一个名为 `name` 评分为 `score` 的景点。
16+
* `string get()` 查询第 `i` 好的景点,其中 `i` 是目前系统查询的次数(包括当前这次查询)。
17+
18+
#### 示例 1:
19+
<pre>
20+
<strong>输入:</strong>
21+
["SORTracker", "add", "add", "get", "add", "get", "add", "get", "add", "get", "add", "get", "get"]
22+
[[], ["bradford", 2], ["branford", 3], [], ["alps", 2], [], ["orland", 2], [], ["orlando", 3], [], ["alpine", 2], [], []]
23+
<strong>输出:</strong>
24+
[null, null, null, "branford", null, "alps", null, "bradford", null, "bradford", null, "bradford", "orland"]
25+
<strong>解释:</strong>
26+
SORTracker tracker = new SORTracker(); // 初始化系统
27+
tracker.add("bradford", 2); // 添加 name="bradford" 且 score=2 的景点。
28+
tracker.add("branford", 3); // 添加 name="branford" 且 score=3 的景点。
29+
tracker.get(); // 从好带坏的景点为:branford ,bradford 。
30+
// 注意到 branford 比 bradford 好,因为它的 评分更高 (3 > 2) 。
31+
// 这是第 1 次调用 get() ,所以返回最好的景点:"branford" 。
32+
tracker.add("alps", 2); // 添加 name="alps" 且 score=2 的景点。
33+
tracker.get(); // 从好到坏的景点为:branford, alps, bradford 。
34+
// 注意 alps 比 bradford 好,虽然它们评分相同,都为 2 。
35+
// 这是因为 "alps" 字典序 比 "bradford" 小。
36+
// 返回第 2 好的地点 "alps" ,因为当前为第 2 次调用 get() 。
37+
tracker.add("orland", 2); // 添加 name="orland" 且 score=2 的景点。
38+
tracker.get(); // 从好到坏的景点为:branford, alps, bradford, orland 。
39+
// 返回 "bradford" ,因为当前为第 3 次调用 get() 。
40+
tracker.add("orlando", 3); // 添加 name="orlando" 且 score=3 的景点。
41+
tracker.get(); // 从好到坏的景点为:branford, orlando, alps, bradford, orland 。
42+
// 返回 "bradford".
43+
tracker.add("alpine", 2); // 添加 name="alpine" 且 score=2 的景点。
44+
tracker.get(); // 从好到坏的景点为:branford, orlando, alpine, alps, bradford, orland 。
45+
// 返回 "bradford" 。
46+
tracker.get(); // 从好到坏的景点为:branford, orlando, alpine, alps, bradford, orland 。
47+
// 返回 "orland" 。
48+
</pre>
49+
50+
#### 提示:
51+
* `name` 只包含小写英文字母,且每个景点名字互不相同。
52+
* `1 <= name.length <= 10`
53+
* <code>1 <= score <= 10<sup>5</sup></code>
54+
* 任意时刻,调用 `get` 的次数都不超过调用 `add` 的次数。
55+
* **总共** 调用 `add``get` 不超过 <code>4 * 10<sup>4</sup></code>
56+
57+
## 题解 (Python)
58+
59+
### 1. 题解
60+
```Python
61+
from sortedcontainers import SortedList
62+
63+
64+
class SORTracker:
65+
66+
def __init__(self):
67+
self.locations = SortedList()
68+
self.i = 0
69+
70+
def add(self, name: str, score: int) -> None:
71+
self.locations.add((-score, name))
72+
73+
def get(self) -> str:
74+
self.i += 1
75+
76+
return self.locations[self.i - 1][1]
77+
78+
# Your SORTracker object will be instantiated and called as such:
79+
# obj = SORTracker()
80+
# obj.add(name,score)
81+
# param_2 = obj.get()
82+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from sortedcontainers import SortedList
2+
3+
4+
class SORTracker:
5+
6+
def __init__(self):
7+
self.locations = SortedList()
8+
self.i = 0
9+
10+
def add(self, name: str, score: int) -> None:
11+
self.locations.add((-score, name))
12+
13+
def get(self) -> str:
14+
self.i += 1
15+
16+
return self.locations[self.i - 1][1]
17+
18+
# Your SORTracker object will be instantiated and called as such:
19+
# obj = SORTracker()
20+
# obj.add(name,score)
21+
# param_2 = obj.get()

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,7 @@
10561056
[2099][2099l]|[Find Subsequence of Length K With the Largest Sum][2099] |![rs]
10571057
[2100][2100l]|[Find Good Days to Rob the Bank][2100] |![rs]
10581058
[2101][2101l]|[Detonate the Maximum Bombs][2101] |![rs]
1059+
[2102][2102l]|[Sequentially Ordinal Rank Tracker][2102] |![py]
10591060
[2103][2103l]|[Rings and Rods][2103] |![rs]
10601061
[2105][2105l]|[Watering Plants II][2105] |![rs]
10611062
[2108][2108l]|[Find First Palindromic String in the Array][2108] |![rs]
@@ -2340,6 +2341,7 @@
23402341
[2099]:Problemset/2099-Find%20Subsequence%20of%20Length%20K%20With%20the%20Largest%20Sum/README.md#2099-find-subsequence-of-length-k-with-the-largest-sum
23412342
[2100]:Problemset/2100-Find%20Good%20Days%20to%20Rob%20the%20Bank/README.md#2100-find-good-days-to-rob-the-bank
23422343
[2101]:Problemset/2101-Detonate%20the%20Maximum%20Bombs/README.md#2101-detonate-the-maximum-bombs
2344+
[2102]:Problemset/2102-Sequentially%20Ordinal%20Rank%20Tracker/README.md#2102-sequentially-ordinal-rank-tracker
23432345
[2103]:Problemset/2103-Rings%20and%20Rods/README.md#2103-rings-and-rods
23442346
[2105]:Problemset/2105-Watering%20Plants%20II/README.md#2105-watering-plants-ii
23452347
[2108]:Problemset/2108-Find%20First%20Palindromic%20String%20in%20the%20Array/README.md#2108-find-first-palindromic-string-in-the-array
@@ -3627,6 +3629,7 @@
36273629
[2099l]:https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/
36283630
[2100l]:https://leetcode.com/problems/find-good-days-to-rob-the-bank/
36293631
[2101l]:https://leetcode.com/problems/detonate-the-maximum-bombs/
3632+
[2102l]:https://leetcode.com/problems/sequentially-ordinal-rank-tracker/
36303633
[2103l]:https://leetcode.com/problems/rings-and-rods/
36313634
[2105l]:https://leetcode.com/problems/watering-plants-ii/
36323635
[2108l]:https://leetcode.com/problems/find-first-palindromic-string-in-the-array/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,7 @@
10561056
[2099][2099l]|[找到和最大的长度为 K 的子序列][2099] |![rs]
10571057
[2100][2100l]|[适合打劫银行的日子][2100] |![rs]
10581058
[2101][2101l]|[引爆最多的炸弹][2101] |![rs]
1059+
[2102][2102l]|[序列顺序查询][2102] |![py]
10591060
[2103][2103l]|[环和杆][2103] |![rs]
10601061
[2105][2105l]|[给植物浇水 II][2105] |![rs]
10611062
[2108][2108l]|[找出数组中的第一个回文字符串][2108] |![rs]
@@ -2340,6 +2341,7 @@
23402341
[2099]:Problemset/2099-Find%20Subsequence%20of%20Length%20K%20With%20the%20Largest%20Sum/README_CN.md#2099-找到和最大的长度为-k-的子序列
23412342
[2100]:Problemset/2100-Find%20Good%20Days%20to%20Rob%20the%20Bank/README_CN.md#2100-适合打劫银行的日子
23422343
[2101]:Problemset/2101-Detonate%20the%20Maximum%20Bombs/README_CN.md#2101-引爆最多的炸弹
2344+
[2102]:Problemset/2102-Sequentially%20Ordinal%20Rank%20Tracker/README_CN.md#2102-序列顺序查询
23432345
[2103]:Problemset/2103-Rings%20and%20Rods/README_CN.md#2103-环和杆
23442346
[2105]:Problemset/2105-Watering%20Plants%20II/README_CN.md#2105-给植物浇水-ii
23452347
[2108]:Problemset/2108-Find%20First%20Palindromic%20String%20in%20the%20Array/README_CN.md#2108-找出数组中的第一个回文字符串
@@ -3627,6 +3629,7 @@
36273629
[2099l]:https://leetcode.cn/problems/find-subsequence-of-length-k-with-the-largest-sum/
36283630
[2100l]:https://leetcode.cn/problems/find-good-days-to-rob-the-bank/
36293631
[2101l]:https://leetcode.cn/problems/detonate-the-maximum-bombs/
3632+
[2102l]:https://leetcode.cn/problems/sequentially-ordinal-rank-tracker/
36303633
[2103l]:https://leetcode.cn/problems/rings-and-rods/
36313634
[2105l]:https://leetcode.cn/problems/watering-plants-ii/
36323635
[2108l]:https://leetcode.cn/problems/find-first-palindromic-string-in-the-array/

0 commit comments

Comments
 (0)