You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/1500-1599/1560.Most Visited Sector in a Circular Track/README_EN.md
+40-4
Original file line number
Diff line number
Diff line change
@@ -66,7 +66,15 @@ We can see that both sectors 1 and 2 are visited twice and they are the most vis
66
66
67
67
<!-- solution:start -->
68
68
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)$.
70
78
71
79
<!-- tabs:start -->
72
80
@@ -113,10 +121,16 @@ public:
113
121
int m = rounds.size() - 1;
114
122
vector<int> ans;
115
123
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
+
}
117
127
} 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);
Copy file name to clipboardexpand all lines: solution/1500-1599/1561.Maximum Number of Coins You Can Get/README_EN.md
+24-22
Original file line number
Diff line number
Diff line change
@@ -77,7 +77,11 @@ On the other hand if we choose this arrangement (1, <strong>2</strong>, 8), (2,
77
77
78
78
<!-- solution:start -->
79
79
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.
81
85
82
86
<!-- tabs:start -->
83
87
@@ -87,18 +91,17 @@ On the other hand if we choose this arrangement (1, <strong>2</strong>, 8), (2,
87
91
classSolution:
88
92
defmaxCoins(self, piles: List[int]) -> int:
89
93
piles.sort()
90
-
returnsum(piles[-2 : len(piles) //3-1 : -2])
94
+
returnsum(piles[len(piles) //3:][::2])
91
95
```
92
96
93
97
#### Java
94
98
95
99
```java
96
100
classSolution {
97
-
98
101
publicintmaxCoins(int[] piles) {
99
102
Arrays.sort(piles);
100
103
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) {
102
105
ans += piles[i];
103
106
}
104
107
return ans;
@@ -112,9 +115,11 @@ class Solution {
112
115
classSolution {
113
116
public:
114
117
int maxCoins(vector<int>& piles) {
115
-
sort(piles.begin(), piles.end());
118
+
ranges::sort(piles);
116
119
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
+
}
118
123
return ans;
119
124
}
120
125
};
@@ -123,13 +128,12 @@ public:
123
128
#### Go
124
129
125
130
```go
126
-
func maxCoins(piles []int) int {
131
+
func maxCoins(piles []int) (ans int) {
127
132
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 {
130
134
ans += piles[i]
131
135
}
132
-
return ans
136
+
return
133
137
}
134
138
```
135
139
@@ -138,10 +142,9 @@ func maxCoins(piles []int) int {
138
142
```ts
139
143
function maxCoins(piles:number[]):number {
140
144
piles.sort((a, b) =>a-b);
141
-
const n =piles.length;
142
145
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];
145
148
}
146
149
returnans;
147
150
}
@@ -153,10 +156,9 @@ function maxCoins(piles: number[]): number {
153
156
implSolution {
154
157
pubfnmax_coins(mutpiles:Vec<i32>) ->i32 {
155
158
piles.sort();
156
-
letn=piles.len();
157
159
letmutans=0;
158
-
foriin1..=n/3 {
159
-
ans+=piles[n-2*i];
160
+
foriin(piles.len()/3..piles.len()).step_by(2) {
161
+
ans+=piles[i];
160
162
}
161
163
ans
162
164
}
@@ -166,16 +168,16 @@ impl Solution {
166
168
#### C
167
169
168
170
```c
169
-
intcmp(const void* a, const void* b) {
170
-
return *(int*) a - *(int*) b;
171
+
intcompare(const void* a, const void* b) {
172
+
return (*(int*) a - *(int*) b);
171
173
}
172
174
173
175
int maxCoins(int* piles, int pilesSize) {
174
-
qsort(piles, pilesSize, sizeof(int), cmp);
176
+
qsort(piles, pilesSize, sizeof(int), compare);
175
177
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) {
0 commit comments