Skip to content

feat: add weekly contest 443 #4311

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 2 commits into from
Mar 30, 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ public int maxActiveSectionsAfterTrade(String s) {
int n = s.length();
int ans = 0, i = 0;
int pre = Integer.MIN_VALUE, mx = 0;

while (i < n) {
int j = i + 1;
while (j < n && s.charAt(j) == s.charAt(i)) {
Expand All @@ -18,7 +18,7 @@ public int maxActiveSectionsAfterTrade(String s) {
}
i = j;
}

ans += mx;
return ans;
}
Expand Down
112 changes: 112 additions & 0 deletions solution/3500-3599/3502.Minimum Cost to Reach Every Position/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
---
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3502.Minimum%20Cost%20to%20Reach%20Every%20Position/README.md
---

<!-- problem:start -->

# [3502. 到达每个位置的最小费用](https://leetcode.cn/problems/minimum-cost-to-reach-every-position)

[English Version](/solution/3500-3599/3502.Minimum%20Cost%20to%20Reach%20Every%20Position/README_EN.md)

## 题目描述

<!-- description:start -->

<p data-end="438" data-start="104">给你一个长度为 <code>n</code> 的整数数组 <code data-end="119" data-start="113">cost</code> 。当前你位于位置 <code data-end="166" data-start="163">n</code>(队伍的末尾),队伍中共有 <code data-end="187" data-start="180">n + 1</code> 人,编号从 0 到 <code>n</code> 。</p>

<p data-end="438" data-start="104">你希望在队伍中向前移动,但队伍中每个人都会收取一定的费用才能与你 <strong>交换</strong>位置。与编号 <code data-end="375" data-start="372">i</code> 的人交换位置的费用为 <code data-end="397" data-start="388">cost[i]</code> 。</p>

<p data-end="487" data-start="440">你可以按照以下规则与他人交换位置:</p>

<ul data-end="632" data-start="488">
<li data-end="572" data-start="488">如果对方在你前面,你 <strong>必须</strong> 支付 <code data-end="546" data-start="537">cost[i]</code> 费用与他们交换位置。</li>
<li data-end="632" data-start="573">如果对方在你后面,他们可以免费与你交换位置。</li>
</ul>

<p data-end="755" data-start="634">返回一个大小为 <code>n</code> 的数组 <code>answer</code>,其中 <code>answer[i]</code> 表示到达队伍中每个位置 <code>i</code> 所需的 <strong data-end="680" data-start="664">最小</strong> 总费用。</p>

<p>&nbsp;</p>

<p><strong class="example">示例 1:</strong></p>

<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">cost = [5,3,4,1,3,2]</span></p>

<p><strong>输出:</strong> <span class="example-io">[5,3,3,1,1,1]</span></p>

<p><strong>解释:</strong></p>

<p>我们可以通过以下方式到达每个位置:</p>

<ul>
<li><code>i = 0</code>。可以花费 5 费用与编号 0 的人交换位置。</li>
<li><span class="example-io"><code>i = 1</code>。可以花费 3 费用与编号 1 的人交换位置。</span></li>
<li><span class="example-io"><code>i = 2</code>。可以花费 3 费用与编号 1 的人交换位置,然后免费与编号 2 的人交换位置。</span></li>
<li><span class="example-io"><code>i = 3</code>。可以花费 1 费用与编号 3 的人交换位置。</span></li>
<li><span class="example-io"><code>i = 4</code>。可以花费 1 费用与编号 3 的人交换位置,然后免费与编号 4 的人交换位置。</span></li>
<li><span class="example-io"><code>i = 5</code>。可以花费 1 费用与编号 3 的人交换位置,然后免费与编号 5 的人交换位置。</span></li>
</ul>
</div>

<p><strong class="example">示例 2:</strong></p>

<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">cost = [1,2,4,6,7]</span></p>

<p><strong>输出:</strong> <span class="example-io">[1,1,1,1,1]</span></p>

<p><strong>解释:</strong></p>

<p>可以花费 1 费用与编号 0 的人交换位置,然后可以免费到达队伍中的任何位置 <code>i</code>。</p>
</div>

<p>&nbsp;</p>

<p><strong>提示</strong></p>

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

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一

<!-- tabs:start -->

#### Python3

```python

```

#### Java

```java

```

#### C++

```cpp

```

#### Go

```go

```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
---
comments: true
difficulty: Easy
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3502.Minimum%20Cost%20to%20Reach%20Every%20Position/README_EN.md
---

<!-- problem:start -->

# [3502. Minimum Cost to Reach Every Position](https://leetcode.com/problems/minimum-cost-to-reach-every-position)

[中文文档](/solution/3500-3599/3502.Minimum%20Cost%20to%20Reach%20Every%20Position/README.md)

## Description

<!-- description:start -->

<p data-end="438" data-start="104">You are given an integer array <code data-end="119" data-start="113">cost</code> of size <code data-end="131" data-start="128">n</code>. You are currently at position <code data-end="166" data-start="163">n</code> (at the end of the line) in a line of <code data-end="187" data-start="180">n + 1</code> people (numbered from 0 to <code data-end="218" data-start="215">n</code>).</p>

<p data-end="438" data-start="104">You wish to move forward in the line, but each person in front of you charges a specific amount to <strong>swap</strong> places. The cost to swap with person <code data-end="375" data-start="372">i</code> is given by <code data-end="397" data-start="388">cost[i]</code>.</p>

<p data-end="487" data-start="440">You are allowed to swap places with people as follows:</p>

<ul data-end="632" data-start="488">
<li data-end="572" data-start="488">If they are in front of you, you <strong>must</strong> pay them <code data-end="546" data-start="537">cost[i]</code> to swap with them.</li>
<li data-end="632" data-start="573">If they are behind you, they can swap with you for free.</li>
</ul>

<p data-end="755" data-start="634">Return an array <code>answer</code> of size <code>n</code>, where <code>answer[i]</code> is the <strong data-end="680" data-start="664">minimum</strong> total cost to reach each position <code>i</code> in the line<font face="monospace">.</font></p>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">cost = [5,3,4,1,3,2]</span></p>

<p><strong>Output:</strong> <span class="example-io">[5,3,3,1,1,1]</span></p>

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

<p>We can get to each position in the following way:</p>

<ul>
<li><code>i = 0</code>. We can swap with person 0 for a cost of 5.</li>
<li><span class="example-io"><code><font face="monospace">i = </font>1</code>. We can swap with person 1 for a cost of 3.</span></li>
<li><span class="example-io"><code>i = 2</code>. We can swap with person 1 for a cost of 3, then swap with person 2 for free.</span></li>
<li><span class="example-io"><code>i = 3</code>. We can swap with person 3 for a cost of 1.</span></li>
<li><span class="example-io"><code>i = 4</code>. We can swap with person 3 for a cost of 1, then swap with person 4 for free.</span></li>
<li><span class="example-io"><code>i = 5</code>. We can swap with person 3 for a cost of 1, then swap with person 5 for free.</span></li>
</ul>
</div>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">cost = [1,2,4,6,7]</span></p>

<p><strong>Output:</strong> <span class="example-io">[1,1,1,1,1]</span></p>

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

<p>We can swap with person 0 for a cost of <span class="example-io">1, then we will be able to reach any position <code>i</code> for free.</span></p>
</div>

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

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

<!-- description:end -->

## Solutions

<!-- solution:start -->

### Solution 1

<!-- tabs:start -->

#### Python3

```python

```

#### Java

```java

```

#### C++

```cpp

```

#### Go

```go

```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
---
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3503.Longest%20Palindrome%20After%20Substring%20Concatenation%20I/README.md
---

<!-- problem:start -->

# [3503. 子字符串连接后的最长回文串 I](https://leetcode.cn/problems/longest-palindrome-after-substring-concatenation-i)

[English Version](/solution/3500-3599/3503.Longest%20Palindrome%20After%20Substring%20Concatenation%20I/README_EN.md)

## 题目描述

<!-- description:start -->

<p>给你两个字符串 <code>s</code> 和 <code>t</code>。</p>

<p>你可以从 <code>s</code> 中选择一个子串(可以为空)以及从 <code>t</code> 中选择一个子串(可以为空),然后将它们<strong> 按顺序 </strong>连接,得到一个新的字符串。</p>

<p>返回可以由上述方法构造出的<strong> 最长</strong> 回文串的长度。</p>

<p><strong>回文串</strong> 是指正着读和反着读都相同的字符串。</p>

<p><strong>子字符串 </strong>是指字符串中的一个连续字符序列。</p>

<p>&nbsp;</p>

<p><strong class="example">示例 1:</strong></p>

<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">s = "a", t = "a"</span></p>

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

<p><strong>解释:</strong></p>

<p>从 <code>s</code> 中选择 <code>"a"</code>,从 <code>t</code> 中选择 <code>"a"</code>,拼接得到 <code>"aa"</code>,这是一个长度为 2 的回文串。</p>
</div>

<p><strong class="example">示例 2:</strong></p>

<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">s = "abc", t = "def"</span></p>

<p><strong>输出:</strong> <span class="example-io">1</span></p>

<p><strong>解释:</strong></p>

<p>由于两个字符串的所有字符都不同,最长的回文串只能是任意一个单独的字符,因此答案是 1。</p>
</div>

<p><strong class="example">示例 3:</strong></p>

<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">s = "b", t = "aaaa"</span></p>

<p><strong>输出:</strong> 4</p>

<p><strong>解释:</strong></p>

<p>可以选择 <code>"aaaa"</code> 作为回文串,其长度为 4。</p>
</div>

<p><strong class="example">示例 4:</strong></p>

<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">s = "abcde", t = "ecdba"</span></p>

<p><strong>输出:</strong> 5</p>

<p><strong>解释:</strong></p>

<p>从 <code>s</code> 中选择 <code>"abc"</code>,从 <code>t</code> 中选择 <code>"ba"</code>,拼接得到 <code>"abcba"</code>,这是一个长度为 5 的回文串。</p>
</div>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>1 &lt;= s.length, t.length &lt;= 30</code></li>
<li><code>s</code> 和 <code>t</code> 仅由小写英文字母组成。</li>
</ul>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一

<!-- tabs:start -->

#### Python3

```python

```

#### Java

```java

```

#### C++

```cpp

```

#### Go

```go

```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading