Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add solutions to lc problems: No.3687~3689 #4738

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
yanglbme merged 1 commit into main from dev
Sep 21, 2025
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
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ tags:

<pre>
<strong>输入: </strong>s = "aabbcc", k = 3
<strong>输出: </strong>"abcabc"
<strong>输出: </strong>"abcabc"
<strong>解释: </strong>相同的字母在新的字符串中间隔至少 3 个单位距离。
</pre>

<p><strong>示例 2:</strong></p>

<pre>
<strong>输入: </strong>s = "aaabc", k = 3
<strong>输出: </strong>""
<strong>输出: </strong>""
<strong>解释:</strong> 没有办法找到可能的重排结果。
</pre>

Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ tags:
- (3, 3) 是一组非互质数,且 LCM(3, 3) = 3 。得到 nums = [2,2,1,1,<em><strong>3</strong></em>,3] 。
- (3, 3) 是一组非互质数,且 LCM(3, 3) = 3 。得到 nums = [2,2,1,1,<em><strong>3</strong></em>] 。
- (2, 2) 是一组非互质数,且 LCM(2, 2) = 2 。得到 nums = [<em><strong>2</strong></em>,1,1,3] 。
现在,nums 中不存在相邻的非互质数。
因此,修改后得到的最终数组是 [2,1,1,3] 。
现在,nums 中不存在相邻的非互质数。
因此,修改后得到的最终数组是 [2,1,1,3] 。
注意,存在其他方法可以获得相同的最终数组。
</pre>

Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ tags:
<pre>
<strong>Input:</strong> nums = [6,4,3,2,7,6,2]
<strong>Output:</strong> [12,7,6]
<strong>Explanation:</strong>
<strong>Explanation:</strong>
- (6, 4) are non-coprime with LCM(6, 4) = 12. Now, nums = [<strong><u>12</u></strong>,3,2,7,6,2].
- (12, 3) are non-coprime with LCM(12, 3) = 12. Now, nums = [<strong><u>12</u></strong>,2,7,6,2].
- (12, 2) are non-coprime with LCM(12, 2) = 12. Now, nums = [<strong><u>12</u></strong>,7,6,2].
Expand All @@ -57,7 +57,7 @@ Note that there are other ways to obtain the same resultant array.
<pre>
<strong>Input:</strong> nums = [2,2,1,1,3,3,3]
<strong>Output:</strong> [2,1,1,3]
<strong>Explanation:</strong>
<strong>Explanation:</strong>
- (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,<u><strong>3</strong></u>,3].
- (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,<u><strong>3</strong></u>].
- (2, 2) are non-coprime with LCM(2, 2) = 2. Now, nums = [<u><strong>2</strong></u>,1,1,3].
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3678.Smallest%20Absent%20Positive%20Greater%20Than%20Average/README.md
rating: 1306
source: 第 165 场双周赛 Q1
---

<!-- problem:start -->
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Easy
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3678.Smallest%20Absent%20Positive%20Greater%20Than%20Average/README_EN.md
rating: 1306
source: Biweekly Contest 165 Q1
---

<!-- problem:start -->
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3679.Minimum%20Discards%20to%20Balance%20Inventory/README.md
rating: 1638
source: 第 165 场双周赛 Q2
---

<!-- problem:start -->
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3679.Minimum%20Discards%20to%20Balance%20Inventory/README_EN.md
rating: 1638
source: Biweekly Contest 165 Q2
---

<!-- problem:start -->
Expand Down
2 changes: 2 additions & 0 deletions solution/3600-3699/3680.Generate Schedule/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3680.Generate%20Schedule/README.md
rating: 2377
source: 第 165 场双周赛 Q3
---

<!-- problem:start -->
Expand Down
2 changes: 2 additions & 0 deletions solution/3600-3699/3680.Generate Schedule/README_EN.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3680.Generate%20Schedule/README_EN.md
rating: 2377
source: Biweekly Contest 165 Q3
---

<!-- problem:start -->
Expand Down
2 changes: 2 additions & 0 deletions solution/3600-3699/3681.Maximum XOR of Subsequences/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3681.Maximum%20XOR%20of%20Subsequences/README.md
rating: 2026
source: 第 165 场双周赛 Q4
---

<!-- problem:start -->
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3681.Maximum%20XOR%20of%20Subsequences/README_EN.md
rating: 2026
source: Biweekly Contest 165 Q4
---

<!-- problem:start -->
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3683.Earliest%20Time%20to%20Finish%20One%20Task/README.md
rating: 1198
source: 第 467 场周赛 Q1
---

<!-- problem:start -->
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Easy
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3683.Earliest%20Time%20to%20Finish%20One%20Task/README_EN.md
rating: 1198
source: Weekly Contest 467 Q1
---

<!-- problem:start -->
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3684.Maximize%20Sum%20of%20At%20Most%20K%20Distinct%20Elements/README.md
rating: 1298
source: 第 467 场周赛 Q2
---

<!-- problem:start -->
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Easy
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3684.Maximize%20Sum%20of%20At%20Most%20K%20Distinct%20Elements/README_EN.md
rating: 1298
source: Weekly Contest 467 Q2
---

<!-- problem:start -->
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3685.Subsequence%20Sum%20After%20Capping%20Elements/README.md
rating: 2073
source: 第 467 场周赛 Q3
---

<!-- problem:start -->
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3685.Subsequence%20Sum%20After%20Capping%20Elements/README_EN.md
rating: 2073
source: Weekly Contest 467 Q3
---

<!-- problem:start -->
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3686.Number%20of%20Stable%20Subsequences/README.md
rating: 1969
source: 第 467 场周赛 Q4
---

<!-- problem:start -->
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3686.Number%20of%20Stable%20Subsequences/README_EN.md
rating: 1969
source: Weekly Contest 467 Q4
---

<!-- problem:start -->
Expand Down
198 changes: 198 additions & 0 deletions solution/3600-3699/3687.Library Late Fee Calculator/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
---
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3687.Library%20Late%20Fee%20Calculator/README.md
---

<!-- problem:start -->

# [3687. Library Late Fee Calculator 🔒](https://leetcode.cn/problems/library-late-fee-calculator)

[English Version](/solution/3600-3699/3687.Library%20Late%20Fee%20Calculator/README_EN.md)

## 题目描述

<!-- description:start -->

<p>You are given an integer array <code>daysLate</code> where <code>daysLate[i]</code> indicates how many days late the <code>i<sup>th</sup></code> book was returned.</p>

<p>The penalty is calculated as follows:</p>

<ul>
<li>If <code>daysLate[i] == 1</code>, penalty is 1.</li>
<li>If <code>2 &lt;= daysLate[i] &lt;= 5</code>, penalty is <code>2 * daysLate[i]</code>.</li>
<li>If <code>daysLate[i] &gt; 5</code>, penalty is <code>3 * daysLate[i]</code>.</li>
</ul>

<p>Return the total penalty for all books.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">daysLate = [5,1,7]</span></p>

<p><strong>Output:</strong> <span class="example-io">32</span></p>

<p><strong>Explanation:</strong></p>

<ul>
<li><code>daysLate[0] = 5</code>: Penalty is <code>2 * daysLate[0] = 2 * 5 = 10</code>.</li>
<li><code>daysLate[1] = 1</code>: Penalty is <code>1</code>.</li>
<li><code>daysLate[2] = 7</code>: Penalty is <code>3 * daysLate[2] = 3 * 7 = 21</code>.</li>
<li>Thus, the total penalty is <code>10 + 1 + 21 = 32</code>.</li>
</ul>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">daysLate = [1,1]</span></p>

<p><strong>Output:</strong> <span class="example-io">2</span></p>

<p><strong>Explanation:</strong></p>

<ul>
<li><code>daysLate[0] = 1</code>: Penalty is <code>1</code>.</li>
<li><code>daysLate[1] = 1</code>: Penalty is <code>1</code>.</li>
<li>Thus, the total penalty is <code>1 + 1 = 2</code>.</li>
</ul>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= daysLate.length &lt;= 100</code></li>
<li><code>1 &lt;= daysLate[i] &lt;= 100</code></li>
</ul>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一:模拟

我们定义一个函数 $\text{f}(x)$ 来计算每本书的罚款:

$$
\text{f}(x) = \begin{cases}
1 & x = 1 \\
2x & 2 \leq x \leq 5 \\
3x & x > 5
\end{cases}
$$

然后我们对数组 $\textit{daysLate}$ 中的每个元素 $x$ 计算 $\text{f}(x)$ 并累加得到总罚款。

时间复杂度 $O(n),ドル其中 $n$ 是数组 $\textit{daysLate}$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

#### Python3

```python
class Solution:
def lateFee(self, daysLate: List[int]) -> int:
def f(x: int) -> int:
if x == 1:
return 1
if x > 5:
return 3 * x
return 2 * x

return sum(f(x) for x in daysLate)
```

#### Java

```java
class Solution {
public int lateFee(int[] daysLate) {
IntUnaryOperator f = x -> {
if (x == 1) {
return 1;
} else if (x > 5) {
return 3 * x;
} else {
return 2 * x;
}
};

int ans = 0;
for (int x : daysLate) {
ans += f.applyAsInt(x);
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
int lateFee(vector<int>& daysLate) {
auto f = [](int x) {
if (x == 1) {
return 1;
} else if (x > 5) {
return 3 * x;
} else {
return 2 * x;
}
};

int ans = 0;
for (int x : daysLate) {
ans += f(x);
}
return ans;
}
};
```

#### Go

```go
func lateFee(daysLate []int) (ans int) {
f := func(x int) int {
if x == 1 {
return 1
} else if x > 5 {
return 3 * x
}
return 2 * x
}
for _, x := range daysLate {
ans += f(x)
}
return
}
```

#### TypeScript

```ts
function lateFee(daysLate: number[]): number {
const f = (x: number): number => {
if (x === 1) {
return 1;
} else if (x > 5) {
return 3 * x;
}
return 2 * x;
};
return daysLate.reduce((acc, days) => acc + f(days), 0);
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading

AltStyle によって変換されたページ (->オリジナル) /