Skip to content

feat: add solutions to lc problems: No.1560~1561 #3559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ tags:

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

由于每个阶段的结束位置是下一个阶段的开始位置,并且每个阶段都是逆时针方向的,所以我们可以根据开始和结束的位置关系来确定每个扇区的经过次数。

如果 $\textit{rounds}[0] \leq \textit{rounds}[m]$,那么从 $\textit{rounds}[0]$ 开始,到 $\textit{rounds}[m]$ 结束的所有扇区经过的次数是最多的,我们可以直接返回这个区间内的所有扇区。

否则,从 $1$ 开始,到 $\textit{rounds}[m]$ 结束的所有扇区和从 $\textit{rounds}[0]$ 开始,到 $n$ 结束的所有扇区的并集是经过次数最多的,我们可以返回这两个区间的并集。

时间复杂度 $O(n)$,其中 $n$ 是扇区的个数。忽略答案数组的空间消耗,空间复杂度 $O(1)$。

<!-- tabs:start -->

#### Python3
Expand Down Expand Up @@ -114,10 +122,16 @@ public:
int m = rounds.size() - 1;
vector<int> ans;
if (rounds[0] <= rounds[m]) {
for (int i = rounds[0]; i <= rounds[m]; ++i) ans.push_back(i);
for (int i = rounds[0]; i <= rounds[m]; ++i) {
ans.push_back(i);
}
} else {
for (int i = 1; i <= rounds[m]; ++i) ans.push_back(i);
for (int i = rounds[0]; i <= n; ++i) ans.push_back(i);
for (int i = 1; i <= rounds[m]; ++i) {
ans.push_back(i);
}
for (int i = rounds[0]; i <= n; ++i) {
ans.push_back(i);
}
}
return ans;
}
Expand Down Expand Up @@ -146,6 +160,28 @@ func mostVisited(n int, rounds []int) []int {
}
```

#### TypeScript

```ts
function mostVisited(n: number, rounds: number[]): number[] {
const ans: number[] = [];
const m = rounds.length - 1;
if (rounds[0] <= rounds[m]) {
for (let i = rounds[0]; i <= rounds[m]; ++i) {
ans.push(i);
}
} else {
for (let i = 1; i <= rounds[m]; ++i) {
ans.push(i);
}
for (let i = rounds[0]; i <= n; ++i) {
ans.push(i);
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,15 @@ We can see that both sectors 1 and 2 are visited twice and they are the most vis

<!-- solution:start -->

### Solution 1
### Solution 1: Considering the Relationship Between Start and End Positions

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.

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.

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.

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)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -113,10 +121,16 @@ public:
int m = rounds.size() - 1;
vector<int> ans;
if (rounds[0] <= rounds[m]) {
for (int i = rounds[0]; i <= rounds[m]; ++i) ans.push_back(i);
for (int i = rounds[0]; i <= rounds[m]; ++i) {
ans.push_back(i);
}
} else {
for (int i = 1; i <= rounds[m]; ++i) ans.push_back(i);
for (int i = rounds[0]; i <= n; ++i) ans.push_back(i);
for (int i = 1; i <= rounds[m]; ++i) {
ans.push_back(i);
}
for (int i = rounds[0]; i <= n; ++i) {
ans.push_back(i);
}
}
return ans;
}
Expand Down Expand Up @@ -145,6 +159,28 @@ func mostVisited(n int, rounds []int) []int {
}
```

#### TypeScript

```ts
function mostVisited(n: number, rounds: number[]): number[] {
const ans: number[] = [];
const m = rounds.length - 1;
if (rounds[0] <= rounds[m]) {
for (let i = rounds[0]; i <= rounds[m]; ++i) {
ans.push(i);
}
} else {
for (let i = 1; i <= rounds[m]; ++i) {
ans.push(i);
}
for (let i = rounds[0]; i <= n; ++i) {
ans.push(i);
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ class Solution {
int m = rounds.size() - 1;
vector<int> ans;
if (rounds[0] <= rounds[m]) {
for (int i = rounds[0]; i <= rounds[m]; ++i) ans.push_back(i);
for (int i = rounds[0]; i <= rounds[m]; ++i) {
ans.push_back(i);
}
} else {
for (int i = 1; i <= rounds[m]; ++i) ans.push_back(i);
for (int i = rounds[0]; i <= n; ++i) ans.push_back(i);
for (int i = 1; i <= rounds[m]; ++i) {
ans.push_back(i);
}
for (int i = rounds[0]; i <= n; ++i) {
ans.push_back(i);
}
}
return ans;
}
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function mostVisited(n: number, rounds: number[]): number[] {
const ans: number[] = [];
const m = rounds.length - 1;
if (rounds[0] <= rounds[m]) {
for (let i = rounds[0]; i <= rounds[m]; ++i) {
ans.push(i);
}
} else {
for (let i = 1; i <= rounds[m]; ++i) {
ans.push(i);
}
for (let i = rounds[0]; i <= n; ++i) {
ans.push(i);
}
}
return ans;
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ tags:

<!-- solution:start -->

### 方法一:贪心
### 方法一:贪心 + 排序

Bob 取走最小的 1/3,剩余的硬币堆由 Alice 和我按硬币数从高到低依次取走每一堆。
为了让我们获得的硬币数量最多,我们可以贪心地让 Bob 拿走最少的 $n$ 堆硬币。我们每次先让 Alice 拿走最多的一堆硬币,然后让我们拿走第二多的一堆硬币,依次循环,直到没有硬币可拿。

时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是硬币堆数。

<!-- tabs:start -->

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

#### Java

```java
class Solution {

public int maxCoins(int[] piles) {
Arrays.sort(piles);
int ans = 0;
for (int i = piles.length - 2; i >= piles.length / 3; i -= 2) {
for (int i = piles.length / 3; i < piles.length; i += 2) {
ans += piles[i];
}
return ans;
Expand All @@ -113,9 +114,11 @@ class Solution {
class Solution {
public:
int maxCoins(vector<int>& piles) {
sort(piles.begin(), piles.end());
ranges::sort(piles);
int ans = 0;
for (int i = piles.size() - 2; i >= (int) piles.size() / 3; i -= 2) ans += piles[i];
for (int i = piles.size() / 3; i < piles.size(); i += 2) {
ans += piles[i];
}
return ans;
}
};
Expand All @@ -124,13 +127,12 @@ public:
#### Go

```go
func maxCoins(piles []int) int {
func maxCoins(piles []int) (ans int) {
sort.Ints(piles)
ans, n := 0, len(piles)
for i := n - 2; i >= n/3; i -= 2 {
for i := len(piles) / 3; i < len(piles); i += 2 {
ans += piles[i]
}
return ans
return
}
```

Expand All @@ -139,10 +141,9 @@ func maxCoins(piles []int) int {
```ts
function maxCoins(piles: number[]): number {
piles.sort((a, b) => a - b);
const n = piles.length;
let ans = 0;
for (let i = 1; i <= Math.floor(n / 3); i++) {
ans += piles[n - 2 * i];
for (let i = piles.length / 3; i < piles.length; i += 2) {
ans += piles[i];
}
return ans;
}
Expand All @@ -154,10 +155,9 @@ function maxCoins(piles: number[]): number {
impl Solution {
pub fn max_coins(mut piles: Vec<i32>) -> i32 {
piles.sort();
let n = piles.len();
let mut ans = 0;
for i in 1..=n / 3 {
ans += piles[n - 2 * i];
for i in (piles.len() / 3..piles.len()).step_by(2) {
ans += piles[i];
}
ans
}
Expand All @@ -167,16 +167,16 @@ impl Solution {
#### C

```c
int cmp(const void* a, const void* b) {
return *(int*) a - *(int*) b;
int compare(const void* a, const void* b) {
return (*(int*) a - *(int*) b);
}

int maxCoins(int* piles, int pilesSize) {
qsort(piles, pilesSize, sizeof(int), cmp);
qsort(piles, pilesSize, sizeof(int), compare);
int ans = 0;
for (int i = 1; i <= pilesSize / 3; i++) {
ans += piles[pilesSize - 2 * i];
};
for (int i = pilesSize / 3; i < pilesSize; i += 2) {
ans += piles[i];
}
return ans;
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ On the other hand if we choose this arrangement (1, <strong>2</strong>, 8), (2,

<!-- solution:start -->

### Solution 1
### Solution 1: Greedy + Sorting

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.

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.

<!-- tabs:start -->

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

#### Java

```java
class Solution {

public int maxCoins(int[] piles) {
Arrays.sort(piles);
int ans = 0;
for (int i = piles.length - 2; i >= piles.length / 3; i -= 2) {
for (int i = piles.length / 3; i < piles.length; i += 2) {
ans += piles[i];
}
return ans;
Expand All @@ -112,9 +115,11 @@ class Solution {
class Solution {
public:
int maxCoins(vector<int>& piles) {
sort(piles.begin(), piles.end());
ranges::sort(piles);
int ans = 0;
for (int i = piles.size() - 2; i >= (int) piles.size() / 3; i -= 2) ans += piles[i];
for (int i = piles.size() / 3; i < piles.size(); i += 2) {
ans += piles[i];
}
return ans;
}
};
Expand All @@ -123,13 +128,12 @@ public:
#### Go

```go
func maxCoins(piles []int) int {
func maxCoins(piles []int) (ans int) {
sort.Ints(piles)
ans, n := 0, len(piles)
for i := n - 2; i >= n/3; i -= 2 {
for i := len(piles) / 3; i < len(piles); i += 2 {
ans += piles[i]
}
return ans
return
}
```

Expand All @@ -138,10 +142,9 @@ func maxCoins(piles []int) int {
```ts
function maxCoins(piles: number[]): number {
piles.sort((a, b) => a - b);
const n = piles.length;
let ans = 0;
for (let i = 1; i <= Math.floor(n / 3); i++) {
ans += piles[n - 2 * i];
for (let i = piles.length / 3; i < piles.length; i += 2) {
ans += piles[i];
}
return ans;
}
Expand All @@ -153,10 +156,9 @@ function maxCoins(piles: number[]): number {
impl Solution {
pub fn max_coins(mut piles: Vec<i32>) -> i32 {
piles.sort();
let n = piles.len();
let mut ans = 0;
for i in 1..=n / 3 {
ans += piles[n - 2 * i];
for i in (piles.len() / 3..piles.len()).step_by(2) {
ans += piles[i];
}
ans
}
Expand All @@ -166,16 +168,16 @@ impl Solution {
#### C

```c
int cmp(const void* a, const void* b) {
return *(int*) a - *(int*) b;
int compare(const void* a, const void* b) {
return (*(int*) a - *(int*) b);
}

int maxCoins(int* piles, int pilesSize) {
qsort(piles, pilesSize, sizeof(int), cmp);
qsort(piles, pilesSize, sizeof(int), compare);
int ans = 0;
for (int i = 1; i <= pilesSize / 3; i++) {
ans += piles[pilesSize - 2 * i];
};
for (int i = pilesSize / 3; i < pilesSize; i += 2) {
ans += piles[i];
}
return ans;
}
```
Expand Down
Loading
Loading