Skip to content

Commit 51350fe

Browse files
authoredSep 25, 2024
feat: add solutions to lc problems: No.1560~1561 (#3559)
1 parent 8ffb89d commit 51350fe

File tree

16 files changed

+187
-186
lines changed

16 files changed

+187
-186
lines changed
 

‎solution/1500-1599/1560.Most Visited Sector in a Circular Track/README.md

+39-3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ tags:
6969

7070
### 方法一:考虑开始、结束的位置关系
7171

72+
由于每个阶段的结束位置是下一个阶段的开始位置,并且每个阶段都是逆时针方向的,所以我们可以根据开始和结束的位置关系来确定每个扇区的经过次数。
73+
74+
如果 $\textit{rounds}[0] \leq \textit{rounds}[m]$,那么从 $\textit{rounds}[0]$ 开始,到 $\textit{rounds}[m]$ 结束的所有扇区经过的次数是最多的,我们可以直接返回这个区间内的所有扇区。
75+
76+
否则,从 $1$ 开始,到 $\textit{rounds}[m]$ 结束的所有扇区和从 $\textit{rounds}[0]$ 开始,到 $n$ 结束的所有扇区的并集是经过次数最多的,我们可以返回这两个区间的并集。
77+
78+
时间复杂度 $O(n)$,其中 $n$ 是扇区的个数。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
79+
7280
<!-- tabs:start -->
7381

7482
#### Python3
@@ -114,10 +122,16 @@ public:
114122
int m = rounds.size() - 1;
115123
vector<int> ans;
116124
if (rounds[0] <= rounds[m]) {
117-
for (int i = rounds[0]; i <= rounds[m]; ++i) ans.push_back(i);
125+
for (int i = rounds[0]; i <= rounds[m]; ++i) {
126+
ans.push_back(i);
127+
}
118128
} else {
119-
for (int i = 1; i <= rounds[m]; ++i) ans.push_back(i);
120-
for (int i = rounds[0]; i <= n; ++i) ans.push_back(i);
129+
for (int i = 1; i <= rounds[m]; ++i) {
130+
ans.push_back(i);
131+
}
132+
for (int i = rounds[0]; i <= n; ++i) {
133+
ans.push_back(i);
134+
}
121135
}
122136
return ans;
123137
}
@@ -146,6 +160,28 @@ func mostVisited(n int, rounds []int) []int {
146160
}
147161
```
148162

163+
#### TypeScript
164+
165+
```ts
166+
function mostVisited(n: number, rounds: number[]): number[] {
167+
const ans: number[] = [];
168+
const m = rounds.length - 1;
169+
if (rounds[0] <= rounds[m]) {
170+
for (let i = rounds[0]; i <= rounds[m]; ++i) {
171+
ans.push(i);
172+
}
173+
} else {
174+
for (let i = 1; i <= rounds[m]; ++i) {
175+
ans.push(i);
176+
}
177+
for (let i = rounds[0]; i <= n; ++i) {
178+
ans.push(i);
179+
}
180+
}
181+
return ans;
182+
}
183+
```
184+
149185
<!-- tabs:end -->
150186

151187
<!-- solution:end -->

‎solution/1500-1599/1560.Most Visited Sector in a Circular Track/README_EN.md

+40-4
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,15 @@ We can see that both sectors 1 and 2 are visited twice and they are the most vis
6666

6767
<!-- solution:start -->
6868

69-
### Solution 1
69+
### Solution 1: Considering the Relationship Between Start and End Positions
70+
71+
Since the end position of each stage is the start position of the next stage, and each stage is in a counterclockwise direction, we can determine the number of times each sector is passed based on the relationship between the start and end positions.
72+
73+
If $\textit{rounds}[0] \leq \textit{rounds}[m]$, then the sectors from $\textit{rounds}[0]$ to $\textit{rounds}[m]$ are passed the most times, and we can directly return all sectors within this interval.
74+
75+
Otherwise, the sectors from $1$ to $\textit{rounds}[m]$ and the sectors from $\textit{rounds}[0]$ to $n$ form the union of the most passed sectors, and we can return the union of these two intervals.
76+
77+
The time complexity is $O(n)$, where $n$ is the number of sectors. Ignoring the space consumption of the answer array, the space complexity is $O(1)$.
7078

7179
<!-- tabs:start -->
7280

@@ -113,10 +121,16 @@ public:
113121
int m = rounds.size() - 1;
114122
vector<int> ans;
115123
if (rounds[0] <= rounds[m]) {
116-
for (int i = rounds[0]; i <= rounds[m]; ++i) ans.push_back(i);
124+
for (int i = rounds[0]; i <= rounds[m]; ++i) {
125+
ans.push_back(i);
126+
}
117127
} else {
118-
for (int i = 1; i <= rounds[m]; ++i) ans.push_back(i);
119-
for (int i = rounds[0]; i <= n; ++i) ans.push_back(i);
128+
for (int i = 1; i <= rounds[m]; ++i) {
129+
ans.push_back(i);
130+
}
131+
for (int i = rounds[0]; i <= n; ++i) {
132+
ans.push_back(i);
133+
}
120134
}
121135
return ans;
122136
}
@@ -145,6 +159,28 @@ func mostVisited(n int, rounds []int) []int {
145159
}
146160
```
147161

162+
#### TypeScript
163+
164+
```ts
165+
function mostVisited(n: number, rounds: number[]): number[] {
166+
const ans: number[] = [];
167+
const m = rounds.length - 1;
168+
if (rounds[0] <= rounds[m]) {
169+
for (let i = rounds[0]; i <= rounds[m]; ++i) {
170+
ans.push(i);
171+
}
172+
} else {
173+
for (let i = 1; i <= rounds[m]; ++i) {
174+
ans.push(i);
175+
}
176+
for (let i = rounds[0]; i <= n; ++i) {
177+
ans.push(i);
178+
}
179+
}
180+
return ans;
181+
}
182+
```
183+
148184
<!-- tabs:end -->
149185

150186
<!-- solution:end -->

‎solution/1500-1599/1560.Most Visited Sector in a Circular Track/Solution.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ class Solution {
44
int m = rounds.size() - 1;
55
vector<int> ans;
66
if (rounds[0] <= rounds[m]) {
7-
for (int i = rounds[0]; i <= rounds[m]; ++i) ans.push_back(i);
7+
for (int i = rounds[0]; i <= rounds[m]; ++i) {
8+
ans.push_back(i);
9+
}
810
} else {
9-
for (int i = 1; i <= rounds[m]; ++i) ans.push_back(i);
10-
for (int i = rounds[0]; i <= n; ++i) ans.push_back(i);
11+
for (int i = 1; i <= rounds[m]; ++i) {
12+
ans.push_back(i);
13+
}
14+
for (int i = rounds[0]; i <= n; ++i) {
15+
ans.push_back(i);
16+
}
1117
}
1218
return ans;
1319
}
14-
};
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function mostVisited(n: number, rounds: number[]): number[] {
2+
const ans: number[] = [];
3+
const m = rounds.length - 1;
4+
if (rounds[0] <= rounds[m]) {
5+
for (let i = rounds[0]; i <= rounds[m]; ++i) {
6+
ans.push(i);
7+
}
8+
} else {
9+
for (let i = 1; i <= rounds[m]; ++i) {
10+
ans.push(i);
11+
}
12+
for (let i = rounds[0]; i <= n; ++i) {
13+
ans.push(i);
14+
}
15+
}
16+
return ans;
17+
}

‎solution/1500-1599/1561.Maximum Number of Coins You Can Get/README.md

+23-23
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ tags:
7676

7777
<!-- solution:start -->
7878

79-
### 方法一:贪心
79+
### 方法一:贪心 + 排序
8080

81-
Bob 取走最小的 1/3,剩余的硬币堆由 Alice 和我按硬币数从高到低依次取走每一堆。
81+
为了让我们获得的硬币数量最多,我们可以贪心地让 Bob 拿走最少的 $n$ 堆硬币。我们每次先让 Alice 拿走最多的一堆硬币,然后让我们拿走第二多的一堆硬币,依次循环,直到没有硬币可拿。
82+
83+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是硬币堆数。
8284

8385
<!-- tabs:start -->
8486

@@ -88,18 +90,17 @@ Bob 取走最小的 1/3,剩余的硬币堆由 Alice 和我按硬币数从高
8890
class Solution:
8991
def maxCoins(self, piles: List[int]) -> int:
9092
piles.sort()
91-
return sum(piles[-2 : len(piles) // 3 - 1 : -2])
93+
return sum(piles[len(piles) // 3 :][::2])
9294
```
9395

9496
#### Java
9597

9698
```java
9799
class Solution {
98-
99100
public int maxCoins(int[] piles) {
100101
Arrays.sort(piles);
101102
int ans = 0;
102-
for (int i = piles.length - 2; i >= piles.length / 3; i -= 2) {
103+
for (int i = piles.length / 3; i < piles.length; i += 2) {
103104
ans += piles[i];
104105
}
105106
return ans;
@@ -113,9 +114,11 @@ class Solution {
113114
class Solution {
114115
public:
115116
int maxCoins(vector<int>& piles) {
116-
sort(piles.begin(), piles.end());
117+
ranges::sort(piles);
117118
int ans = 0;
118-
for (int i = piles.size() - 2; i >= (int) piles.size() / 3; i -= 2) ans += piles[i];
119+
for (int i = piles.size() / 3; i < piles.size(); i += 2) {
120+
ans += piles[i];
121+
}
119122
return ans;
120123
}
121124
};
@@ -124,13 +127,12 @@ public:
124127
#### Go
125128
126129
```go
127-
func maxCoins(piles []int) int {
130+
func maxCoins(piles []int) (ans int) {
128131
sort.Ints(piles)
129-
ans, n := 0, len(piles)
130-
for i := n - 2; i >= n/3; i -= 2 {
132+
for i := len(piles) / 3; i < len(piles); i += 2 {
131133
ans += piles[i]
132134
}
133-
return ans
135+
return
134136
}
135137
```
136138

@@ -139,10 +141,9 @@ func maxCoins(piles []int) int {
139141
```ts
140142
function maxCoins(piles: number[]): number {
141143
piles.sort((a, b) => a - b);
142-
const n = piles.length;
143144
let ans = 0;
144-
for (let i = 1; i <= Math.floor(n / 3); i++) {
145-
ans += piles[n - 2 * i];
145+
for (let i = piles.length / 3; i < piles.length; i += 2) {
146+
ans += piles[i];
146147
}
147148
return ans;
148149
}
@@ -154,10 +155,9 @@ function maxCoins(piles: number[]): number {
154155
impl Solution {
155156
pub fn max_coins(mut piles: Vec<i32>) -> i32 {
156157
piles.sort();
157-
let n = piles.len();
158158
let mut ans = 0;
159-
for i in 1..=n / 3 {
160-
ans += piles[n - 2 * i];
159+
for i in (piles.len() / 3..piles.len()).step_by(2) {
160+
ans += piles[i];
161161
}
162162
ans
163163
}
@@ -167,16 +167,16 @@ impl Solution {
167167
#### C
168168

169169
```c
170-
int cmp(const void* a, const void* b) {
171-
return *(int*) a - *(int*) b;
170+
int compare(const void* a, const void* b) {
171+
return (*(int*) a - *(int*) b);
172172
}
173173

174174
int maxCoins(int* piles, int pilesSize) {
175-
qsort(piles, pilesSize, sizeof(int), cmp);
175+
qsort(piles, pilesSize, sizeof(int), compare);
176176
int ans = 0;
177-
for (int i = 1; i <= pilesSize / 3; i++) {
178-
ans += piles[pilesSize - 2 * i];
179-
};
177+
for (int i = pilesSize / 3; i < pilesSize; i += 2) {
178+
ans += piles[i];
179+
}
180180
return ans;
181181
}
182182
```

‎solution/1500-1599/1561.Maximum Number of Coins You Can Get/README_EN.md

+24-22
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ On the other hand if we choose this arrangement (1, <strong>2</strong>, 8), (2,
7777

7878
<!-- solution:start -->
7979

80-
### Solution 1
80+
### Solution 1: Greedy + Sorting
81+
82+
To maximize the number of coins we get, we can greedily let Bob take the smallest $n$ piles of coins. Each time, we let Alice take the largest pile of coins, then we take the second largest pile of coins, and so on, until there are no more coins to take.
83+
84+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the number of piles of coins.
8185

8286
<!-- tabs:start -->
8387

@@ -87,18 +91,17 @@ On the other hand if we choose this arrangement (1, <strong>2</strong>, 8), (2,
8791
class Solution:
8892
def maxCoins(self, piles: List[int]) -> int:
8993
piles.sort()
90-
return sum(piles[-2 : len(piles) // 3 - 1 : -2])
94+
return sum(piles[len(piles) // 3 :][::2])
9195
```
9296

9397
#### Java
9498

9599
```java
96100
class Solution {
97-
98101
public int maxCoins(int[] piles) {
99102
Arrays.sort(piles);
100103
int ans = 0;
101-
for (int i = piles.length - 2; i >= piles.length / 3; i -= 2) {
104+
for (int i = piles.length / 3; i < piles.length; i += 2) {
102105
ans += piles[i];
103106
}
104107
return ans;
@@ -112,9 +115,11 @@ class Solution {
112115
class Solution {
113116
public:
114117
int maxCoins(vector<int>& piles) {
115-
sort(piles.begin(), piles.end());
118+
ranges::sort(piles);
116119
int ans = 0;
117-
for (int i = piles.size() - 2; i >= (int) piles.size() / 3; i -= 2) ans += piles[i];
120+
for (int i = piles.size() / 3; i < piles.size(); i += 2) {
121+
ans += piles[i];
122+
}
118123
return ans;
119124
}
120125
};
@@ -123,13 +128,12 @@ public:
123128
#### Go
124129
125130
```go
126-
func maxCoins(piles []int) int {
131+
func maxCoins(piles []int) (ans int) {
127132
sort.Ints(piles)
128-
ans, n := 0, len(piles)
129-
for i := n - 2; i >= n/3; i -= 2 {
133+
for i := len(piles) / 3; i < len(piles); i += 2 {
130134
ans += piles[i]
131135
}
132-
return ans
136+
return
133137
}
134138
```
135139

@@ -138,10 +142,9 @@ func maxCoins(piles []int) int {
138142
```ts
139143
function maxCoins(piles: number[]): number {
140144
piles.sort((a, b) => a - b);
141-
const n = piles.length;
142145
let ans = 0;
143-
for (let i = 1; i <= Math.floor(n / 3); i++) {
144-
ans += piles[n - 2 * i];
146+
for (let i = piles.length / 3; i < piles.length; i += 2) {
147+
ans += piles[i];
145148
}
146149
return ans;
147150
}
@@ -153,10 +156,9 @@ function maxCoins(piles: number[]): number {
153156
impl Solution {
154157
pub fn max_coins(mut piles: Vec<i32>) -> i32 {
155158
piles.sort();
156-
let n = piles.len();
157159
let mut ans = 0;
158-
for i in 1..=n / 3 {
159-
ans += piles[n - 2 * i];
160+
for i in (piles.len() / 3..piles.len()).step_by(2) {
161+
ans += piles[i];
160162
}
161163
ans
162164
}
@@ -166,16 +168,16 @@ impl Solution {
166168
#### C
167169

168170
```c
169-
int cmp(const void* a, const void* b) {
170-
return *(int*) a - *(int*) b;
171+
int compare(const void* a, const void* b) {
172+
return (*(int*) a - *(int*) b);
171173
}
172174

173175
int maxCoins(int* piles, int pilesSize) {
174-
qsort(piles, pilesSize, sizeof(int), cmp);
176+
qsort(piles, pilesSize, sizeof(int), compare);
175177
int ans = 0;
176-
for (int i = 1; i <= pilesSize / 3; i++) {
177-
ans += piles[pilesSize - 2 * i];
178-
};
178+
for (int i = pilesSize / 3; i < pilesSize; i += 2) {
179+
ans += piles[i];
180+
}
179181
return ans;
180182
}
181183
```

0 commit comments

Comments
 (0)