Skip to content

Commit 837a82f

Browse files
committed
feat: add solutions to lc problems: No.2446~2449
* No.2446.Determine if Two Events Have Conflict * No.2447.Number of Subarrays With GCD Equal to K * No.2448.Minimum Cost to Make Array Equal * No.2449.Minimum Number of Operations to Make Arrays Similar
1 parent fe99865 commit 837a82f

File tree

45 files changed

+1694
-37
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1694
-37
lines changed

lcci/03.03.Stack of Plates/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class StackOfPlates {
8686
public StackOfPlates(int cap) {
8787
this.cap = cap;
8888
}
89-
89+
9090
public void push(int val) {
9191
if (cap == 0) {
9292
return;
@@ -96,11 +96,11 @@ class StackOfPlates {
9696
}
9797
stk.get(stk.size() - 1).push(val);
9898
}
99-
99+
100100
public int pop() {
101101
return popAt(stk.size() - 1);
102102
}
103-
103+
104104
public int popAt(int index) {
105105
int ans = -1;
106106
if (index >= 0 && index < stk.size()) {

solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@
44

55
## Description
66

7-
<p>Given a string <code>s</code>, find the length of the <strong>longest substring</strong> without repeating characters.</p>
7+
<p>Given a string <code>s</code>, find the length of the <strong>longest <span data-keyword="substring-nonempty">substring</span></strong> without repeating characters.</p>
88

99
<p>&nbsp;</p>
10-
<p><strong class="example">Example 1:</strong></p>
10+
<p><strong>Example 1:</strong></p>
1111

1212
<pre>
1313
<strong>Input:</strong> s = &quot;abcabcbb&quot;
1414
<strong>Output:</strong> 3
1515
<strong>Explanation:</strong> The answer is &quot;abc&quot;, with the length of 3.
1616
</pre>
1717

18-
<p><strong class="example">Example 2:</strong></p>
18+
<p><strong>Example 2:</strong></p>
1919

2020
<pre>
2121
<strong>Input:</strong> s = &quot;bbbbb&quot;
2222
<strong>Output:</strong> 1
2323
<strong>Explanation:</strong> The answer is &quot;b&quot;, with the length of 1.
2424
</pre>
2525

26-
<p><strong class="example">Example 3:</strong></p>
26+
<p><strong>Example 3:</strong></p>
2727

2828
<pre>
2929
<strong>Input:</strong> s = &quot;pwwkew&quot;

solution/0000-0099/0039.Combination Sum/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>给你一个 <strong>无重复元素</strong> 的整数数组&nbsp;<code>candidates</code> 和一个目标整数&nbsp;<code>target</code>&nbsp;,找出&nbsp;<code>candidates</code>&nbsp;中可以使数字和为目标数&nbsp;<code>target</code> 的 <em>所有&nbsp;</em><strong>不同组合</strong> ,并以列表形式返回。你可以按 <strong>任意顺序</strong> 返回这些组合。</p>
9+
<p>给你一个 <strong>无重复元素</strong> 的整数数组&nbsp;<code>candidates</code> 和一个目标整数&nbsp;<code>target</code>&nbsp;,找出&nbsp;<code>candidates</code>&nbsp;中可以使数字和为目标数&nbsp;<code>target</code> 的 所有<em>&nbsp;</em><strong>不同组合</strong> ,并以列表形式返回。你可以按 <strong>任意顺序</strong> 返回这些组合。</p>
1010

1111
<p><code>candidates</code> 中的 <strong>同一个</strong> 数字可以 <strong>无限制重复被选取</strong> 。如果至少一个数字的被选数量不同,则两种组合是不同的。&nbsp;</p>
1212

@@ -43,9 +43,9 @@
4343

4444
<ul>
4545
<li><code>1 &lt;= candidates.length &lt;= 30</code></li>
46-
<li><code>1 &lt;= candidates[i] &lt;= 200</code></li>
47-
<li><code>candidate</code> 中的每个元素都 <strong>互不相同</strong></li>
48-
<li><code>1 &lt;= target &lt;= 500</code></li>
46+
<li><code>2 &lt;= candidates[i] &lt;= 40</code></li>
47+
<li><code>candidates</code> 的所有元素 <strong>互不相同</strong></li>
48+
<li><code>1 &lt;= target &lt;= 40</code></li>
4949
</ul>
5050

5151
## 解法

solution/0100-0199/0140.Word Break II/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@
99
<p><strong>Note</strong> that the same word in the dictionary may be reused multiple times in the segmentation.</p>
1010

1111
<p>&nbsp;</p>
12-
<p><strong class="example">Example 1:</strong></p>
12+
<p><strong>Example 1:</strong></p>
1313

1414
<pre>
1515
<strong>Input:</strong> s = &quot;catsanddog&quot;, wordDict = [&quot;cat&quot;,&quot;cats&quot;,&quot;and&quot;,&quot;sand&quot;,&quot;dog&quot;]
1616
<strong>Output:</strong> [&quot;cats and dog&quot;,&quot;cat sand dog&quot;]
1717
</pre>
1818

19-
<p><strong class="example">Example 2:</strong></p>
19+
<p><strong>Example 2:</strong></p>
2020

2121
<pre>
2222
<strong>Input:</strong> s = &quot;pineapplepenapple&quot;, wordDict = [&quot;apple&quot;,&quot;pen&quot;,&quot;applepen&quot;,&quot;pine&quot;,&quot;pineapple&quot;]
2323
<strong>Output:</strong> [&quot;pine apple pen apple&quot;,&quot;pineapple pen apple&quot;,&quot;pine applepen apple&quot;]
2424
<strong>Explanation:</strong> Note that you are allowed to reuse a dictionary word.
2525
</pre>
2626

27-
<p><strong class="example">Example 3:</strong></p>
27+
<p><strong>Example 3:</strong></p>
2828

2929
<pre>
3030
<strong>Input:</strong> s = &quot;catsandog&quot;, wordDict = [&quot;cats&quot;,&quot;dog&quot;,&quot;sand&quot;,&quot;and&quot;,&quot;cat&quot;]

solution/0100-0199/0141.Linked List Cycle/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@
1111
<p>Return&nbsp;<code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
1212

1313
<p>&nbsp;</p>
14-
<p><strong class="example">Example 1:</strong></p>
14+
<p><strong>Example 1:</strong></p>
1515
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0141.Linked%20List%20Cycle/images/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
1616
<pre>
1717
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
1818
<strong>Output:</strong> true
1919
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
2020
</pre>
2121

22-
<p><strong class="example">Example 2:</strong></p>
22+
<p><strong>Example 2:</strong></p>
2323
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0141.Linked%20List%20Cycle/images/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
2424
<pre>
2525
<strong>Input:</strong> head = [1,2], pos = 0
2626
<strong>Output:</strong> true
2727
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
2828
</pre>
2929

30-
<p><strong class="example">Example 3:</strong></p>
30+
<p><strong>Example 3:</strong></p>
3131
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0141.Linked%20List%20Cycle/images/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
3232
<pre>
3333
<strong>Input:</strong> head = [1], pos = -1

solution/0100-0199/0162.Find Peak Element/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
<p>You must write an algorithm that runs in <code>O(log n)</code> time.</p>
1414

1515
<p>&nbsp;</p>
16-
<p><strong class="example">Example 1:</strong></p>
16+
<p><strong>Example 1:</strong></p>
1717

1818
<pre>
1919
<strong>Input:</strong> nums = [1,2,3,1]
2020
<strong>Output:</strong> 2
2121
<strong>Explanation:</strong> 3 is a peak element and your function should return the index number 2.</pre>
2222

23-
<p><strong class="example">Example 2:</strong></p>
23+
<p><strong>Example 2:</strong></p>
2424

2525
<pre>
2626
<strong>Input:</strong> nums = [1,2,1,3,5,6,4]

solution/0200-0299/0219.Contains Duplicate II/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@
77
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> if there are two <strong>distinct indices</strong> <code>i</code> and <code>j</code> in the array such that <code>nums[i] == nums[j]</code> and <code>abs(i - j) &lt;= k</code>.</p>
88

99
<p>&nbsp;</p>
10-
<p><strong class="example">Example 1:</strong></p>
10+
<p><strong>Example 1:</strong></p>
1111

1212
<pre>
1313
<strong>Input:</strong> nums = [1,2,3,1], k = 3
1414
<strong>Output:</strong> true
1515
</pre>
1616

17-
<p><strong class="example">Example 2:</strong></p>
17+
<p><strong>Example 2:</strong></p>
1818

1919
<pre>
2020
<strong>Input:</strong> nums = [1,0,1,1], k = 1
2121
<strong>Output:</strong> true
2222
</pre>
2323

24-
<p><strong class="example">Example 3:</strong></p>
24+
<p><strong>Example 3:</strong></p>
2525

2626
<pre>
2727
<strong>Input:</strong> nums = [1,2,3,1,2,3], k = 2

solution/0400-0499/0411.Minimum Unique Word Abbreviation/README_EN.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<p>Given a target string <code>target</code> and an array of strings <code>dictionary</code>, return <em>an <strong>abbreviation</strong> of </em><code>target</code><em> with the <strong>shortest possible length</strong> such that it is <strong>not an abbreviation</strong> of <strong>any</strong> string in </em><code>dictionary</code><em>. If there are multiple shortest abbreviations, return any of them</em>.</p>
2222

2323
<p>&nbsp;</p>
24-
<p><strong class="example">Example 1:</strong></p>
24+
<p><strong>Example 1:</strong></p>
2525

2626
<pre>
2727
<strong>Input:</strong> target = &quot;apple&quot;, dictionary = [&quot;blade&quot;]
@@ -31,7 +31,7 @@ The next shortest abbreviations are &quot;a4&quot; and &quot;4e&quot;. &quot;4e&
3131
Hence, return &quot;a4&quot;.
3232
</pre>
3333

34-
<p><strong class="example">Example 2:</strong></p>
34+
<p><strong>Example 2:</strong></p>
3535

3636
<pre>
3737
<strong>Input:</strong> target = &quot;apple&quot;, dictionary = [&quot;blade&quot;,&quot;plain&quot;,&quot;amber&quot;]
@@ -54,6 +54,7 @@ Since none of them are abbreviations of words in the dictionary, returning any o
5454
<li><code>1 &lt;= dictionary[i].length &lt;= 100</code></li>
5555
<li><code>log<sub>2</sub>(n) + m &lt;= 21</code> if <code>n &gt; 0</code></li>
5656
<li><code>target</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li>
57+
<li><code>dictionary</code> does not contain <code>target</code>.</li>
5758
</ul>
5859

5960
## Solutions

solution/0700-0799/0744.Find Smallest Letter Greater Than Target/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
<pre>
1515
<strong>Input:</strong> letters = [&quot;c&quot;,&quot;f&quot;,&quot;j&quot;], target = &quot;a&quot;
1616
<strong>Output:</strong> &quot;c&quot;
17-
<strong>Explanation:</strong> The smallest character that is lexicogrpahically greater than &#39;a&#39; in letters is &#39;c&#39;.
17+
<strong>Explanation:</strong> The smallest character that is lexicographically greater than &#39;a&#39; in letters is &#39;c&#39;.
1818
</pre>
1919

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

2222
<pre>
2323
<strong>Input:</strong> letters = [&quot;c&quot;,&quot;f&quot;,&quot;j&quot;], target = &quot;c&quot;
2424
<strong>Output:</strong> &quot;f&quot;
25-
<strong>Explanation:</strong> The smallest character that is lexicogrpahically greater than &#39;c&#39; in letters is &#39;f&#39;.
25+
<strong>Explanation:</strong> The smallest character that is lexicographically greater than &#39;c&#39; in letters is &#39;f&#39;.
2626
</pre>
2727

2828
<p><strong class="example">Example 3:</strong></p>

solution/0700-0799/0786.K-th Smallest Prime Fraction/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
<li><code>1 &lt;= k &lt;= arr.length * (arr.length - 1) / 2</code></li>
4444
</ul>
4545

46+
<p>&nbsp;</p>
47+
48+
<p><strong>进阶:</strong>你可以设计并实现时间复杂度小于 <code>O(n<sup>2</sup>)</code> 的算法解决此问题吗?</p>
49+
4650
## 解法
4751

4852
<!-- 这里可写通用的实现逻辑 -->

solution/0900-0999/0901.Online Stock Span/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public:
122122
StockSpanner() {
123123

124124
}
125-
125+
126126
int next(int price) {
127127
int cnt = 1;
128128
while (!stk.empty() && stk.top().first <= price) {

solution/0900-0999/0901.Online Stock Span/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public:
107107
StockSpanner() {
108108

109109
}
110-
110+
111111
int next(int price) {
112112
int cnt = 1;
113113
while (!stk.empty() && stk.top().first <= price) {

solution/1300-1399/1312.Minimum Insertion Steps to Make a String Palindrome/README_EN.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@
1111
<p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p>
1212

1313
<p>&nbsp;</p>
14-
<p><strong class="example">Example 1:</strong></p>
14+
<p><strong>Example 1:</strong></p>
1515

1616
<pre>
1717
<strong>Input:</strong> s = &quot;zzazz&quot;
1818
<strong>Output:</strong> 0
19-
<strong>Explanation:</strong> The string &quot;zzazz&quot; is already palindrome we don&#39;t need any insertions.
19+
<strong>Explanation:</strong> The string &quot;zzazz&quot; is already palindrome we do not need any insertions.
2020
</pre>
2121

22-
<p><strong class="example">Example 2:</strong></p>
22+
<p><strong>Example 2:</strong></p>
2323

2424
<pre>
2525
<strong>Input:</strong> s = &quot;mbadm&quot;
2626
<strong>Output:</strong> 2
2727
<strong>Explanation:</strong> String can be &quot;mbdadbm&quot; or &quot;mdbabdm&quot;.
2828
</pre>
2929

30-
<p><strong class="example">Example 3:</strong></p>
30+
<p><strong>Example 3:</strong></p>
3131

3232
<pre>
3333
<strong>Input:</strong> s = &quot;leetcode&quot;

solution/1700-1799/1755.Closest Subsequence Sum/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class Solution:
110110
return
111111
dfs(arr, res, i + 1, s)
112112
dfs(arr, res, i + 1, s + arr[i])
113-
113+
114114
n = len(nums)
115115
left, right = set(), set()
116116
dfs(nums[: n >> 1], left, 0, 0)

solution/2400-2499/2434.Using a Robot to Print the Lexicographically Smallest String/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<p>Return <em>the lexicographically smallest string that can be written on the paper.</em></p>
1515

1616
<p>&nbsp;</p>
17-
<p><strong class="example">Example 1:</strong></p>
17+
<p><strong>Example 1:</strong></p>
1818

1919
<pre>
2020
<strong>Input:</strong> s = &quot;zza&quot;
@@ -25,7 +25,7 @@ Perform first operation three times p=&quot;&quot;, s=&quot;&quot;, t=&quot;zza&
2525
Perform second operation three times p=&quot;azz&quot;, s=&quot;&quot;, t=&quot;&quot;.
2626
</pre>
2727

28-
<p><strong class="example">Example 2:</strong></p>
28+
<p><strong>Example 2:</strong></p>
2929

3030
<pre>
3131
<strong>Input:</strong> s = &quot;bac&quot;
@@ -37,7 +37,7 @@ Perform first operation p=&quot;ab&quot;, s=&quot;&quot;, t=&quot;c&quot;.
3737
Perform second operation p=&quot;abc&quot;, s=&quot;&quot;, t=&quot;&quot;.
3838
</pre>
3939

40-
<p><strong class="example">Example 3:</strong></p>
40+
<p><strong>Example 3:</strong></p>
4141

4242
<pre>
4343
<strong>Input:</strong> s = &quot;bdda&quot;

0 commit comments

Comments
 (0)