Skip to content

Commit 6cecb7f

Browse files
committedDec 4, 2022
feat: add solutions to lc problems: No.2490~2493
* No.2490.Circular Sentence * No.2491.Divide Players Into Teams of Equal Skill * No.2492.Minimum Score of a Path Between Two Cities * No.2493.Divide Nodes Into the Maximum Number of Groups
1 parent f6ef05b commit 6cecb7f

File tree

34 files changed

+2499
-22
lines changed

34 files changed

+2499
-22
lines changed
 

‎solution/1300-1399/1381.Design a Stack With Increment Operation/README_EN.md

+20-22
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
## Description
66

7-
<p>Design a stack which supports the following operations.</p>
7+
<p>Design a stack that supports increment operations on its elements.</p>
88

99
<p>Implement the <code>CustomStack</code> class:</p>
1010

1111
<ul>
12-
<li><code>CustomStack(int maxSize)</code> Initializes the object with <code>maxSize</code> which is the maximum number of elements in the stack or do nothing if the stack reached the <code>maxSize</code>.</li>
13-
<li><code>void push(int x)</code>&nbsp;Adds <code>x</code> to the top of the stack if the stack hasn&#39;t reached the <code>maxSize</code>.</li>
14-
<li><code>int pop()</code>&nbsp;Pops and returns the top of stack or <strong>-1</strong> if the stack is empty.</li>
15-
<li><code>void inc(int k, int val)</code> Increments the bottom <code>k</code> elements of the stack by <code>val</code>. If there are less than <code>k</code> elements in the stack, just increment all the elements in the stack.</li>
12+
<li><code>CustomStack(int maxSize)</code> Initializes the object with <code>maxSize</code> which is the maximum number of elements in the stack.</li>
13+
<li><code>void push(int x)</code> Adds <code>x</code> to the top of the stack if the stack has not reached the <code>maxSize</code>.</li>
14+
<li><code>int pop()</code> Pops and returns the top of the stack or <code>-1</code> if the stack is empty.</li>
15+
<li><code>void inc(int k, int val)</code> Increments the bottom <code>k</code> elements of the stack by <code>val</code>. If there are less than <code>k</code> elements in the stack, increment all the elements in the stack.</li>
1616
</ul>
1717

1818
<p>&nbsp;</p>
@@ -25,30 +25,28 @@
2525
<strong>Output</strong>
2626
[null,null,null,2,null,null,null,null,null,103,202,201,-1]
2727
<strong>Explanation</strong>
28-
CustomStack customStack = new CustomStack(3); // Stack is Empty []
29-
customStack.push(1); // stack becomes [1]
30-
customStack.push(2); // stack becomes [1, 2]
31-
customStack.pop(); // return 2 --&gt; Return top of the stack 2, stack becomes [1]
32-
customStack.push(2); // stack becomes [1, 2]
33-
customStack.push(3); // stack becomes [1, 2, 3]
34-
customStack.push(4); // stack still [1, 2, 3], Don&#39;t add another elements as size is 4
35-
customStack.increment(5, 100); // stack becomes [101, 102, 103]
36-
customStack.increment(2, 100); // stack becomes [201, 202, 103]
37-
customStack.pop(); // return 103 --&gt; Return top of the stack 103, stack becomes [201, 202]
38-
customStack.pop(); // return 202 --&gt; Return top of the stack 102, stack becomes [201]
39-
customStack.pop(); // return 201 --&gt; Return top of the stack 101, stack becomes []
40-
customStack.pop(); // return -1 --&gt; Stack is empty return -1.
28+
CustomStack stk = new CustomStack(3); // Stack is Empty []
29+
stk.push(1); // stack becomes [1]
30+
stk.push(2); // stack becomes [1, 2]
31+
stk.pop(); // return 2 --&gt; Return top of the stack 2, stack becomes [1]
32+
stk.push(2); // stack becomes [1, 2]
33+
stk.push(3); // stack becomes [1, 2, 3]
34+
stk.push(4); // stack still [1, 2, 3], Do not add another elements as size is 4
35+
stk.increment(5, 100); // stack becomes [101, 102, 103]
36+
stk.increment(2, 100); // stack becomes [201, 202, 103]
37+
stk.pop(); // return 103 --&gt; Return top of the stack 103, stack becomes [201, 202]
38+
stk.pop(); // return 202 --&gt; Return top of the stack 202, stack becomes [201]
39+
stk.pop(); // return 201 --&gt; Return top of the stack 201, stack becomes []
40+
stk.pop(); // return -1 --&gt; Stack is empty return -1.
4141
</pre>
4242

4343
<p>&nbsp;</p>
4444
<p><strong>Constraints:</strong></p>
4545

4646
<ul>
47-
<li><code>1 &lt;= maxSize &lt;= 1000</code></li>
48-
<li><code>1 &lt;= x &lt;= 1000</code></li>
49-
<li><code>1 &lt;= k &lt;= 1000</code></li>
47+
<li><code>1 &lt;= maxSize, x, k &lt;= 1000</code></li>
5048
<li><code>0 &lt;= val &lt;= 100</code></li>
51-
<li>At most&nbsp;<code>1000</code>&nbsp;calls will be made to each method of <code>increment</code>, <code>push</code> and <code>pop</code> each separately.</li>
49+
<li>At most <code>1000</code> calls will be made to each method of <code>increment</code>, <code>push</code> and <code>pop</code> each separately.</li>
5250
</ul>
5351

5452
## Solutions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# [2490. 回环句](https://leetcode.cn/problems/circular-sentence)
2+
3+
[English Version](/solution/2400-2499/2490.Circular%20Sentence/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p><strong>句子</strong> 是由单个空格分隔的一组单词,且不含前导或尾随空格。</p>
10+
11+
<ul>
12+
<li>例如,<code>"Hello World"</code>、<code>"HELLO"</code>、<code>"hello world hello world"</code> 都是符合要求的句子。</li>
13+
</ul>
14+
15+
<p>单词 <strong>仅</strong> 由大写和小写英文字母组成。且大写和小写字母会视作不同字符。</p>
16+
17+
<p>如果句子满足下述全部条件,则认为它是一个 <strong>回环句</strong> :</p>
18+
19+
<ul>
20+
<li>单词的最后一个字符和下一个单词的第一个字符相等。</li>
21+
<li>最后一个单词的最后一个字符和第一个单词的第一个字符相等。</li>
22+
</ul>
23+
24+
<p>例如,<code>"leetcode exercises sound delightful"</code>、<code>"eetcode"</code>、<code>"leetcode eats soul"</code> 都是回环句。然而,<code>"Leetcode is cool"</code>、<code>"happy Leetcode"</code>、<code>"Leetcode"</code> 和 <code>"I like Leetcode"</code> 都 <strong>不</strong> 是回环句。</p>
25+
26+
<p>给你一个字符串 <code>sentence</code> ,请你判断它是不是一个回环句。如果是,返回 <code>true</code><em> </em>;否则,返回 <code>false</code> 。</p>
27+
28+
<p>&nbsp;</p>
29+
30+
<p><strong>示例 1:</strong></p>
31+
32+
<pre>
33+
<strong>输入:</strong>sentence = "leetcode exercises sound delightful"
34+
<strong>输出:</strong>true
35+
<strong>解释:</strong>句子中的单词是 ["leetcode", "exercises", "sound", "delightful"] 。
36+
- leetcod<strong><em>e</em></strong> 的最后一个字符和 <strong><em>e</em></strong>xercises 的第一个字符相等。
37+
- exercise<em><strong>s</strong></em> 的最后一个字符和 <em><strong>s</strong></em>ound 的第一个字符相等。
38+
- <em><strong>s</strong></em>ound 的最后一个字符和 delightfu<em><strong>l</strong></em> 的第一个字符相等。
39+
- delightfu<em><strong>l</strong></em> 的最后一个字符和 <em><strong>l</strong></em>eetcode 的第一个字符相等。
40+
这个句子是回环句。</pre>
41+
42+
<p><strong>示例 2:</strong></p>
43+
44+
<pre>
45+
<strong>输入:</strong>sentence = "eetcode"
46+
<strong>输出:</strong>true
47+
<strong>解释:</strong>句子中的单词是 ["eetcode"] 。
48+
- eetcod<em><strong>e</strong></em> 的最后一个字符和 eetcod<em><strong>e</strong></em> 的第一个字符相等。
49+
这个句子是回环句。</pre>
50+
51+
<p><strong>示例 3:</strong></p>
52+
53+
<pre>
54+
<strong>输入:</strong>sentence = "Leetcode is cool"
55+
<strong>输出:</strong>false
56+
<strong>解释:</strong>句子中的单词是 ["Leetcode", "is", "cool"] 。
57+
- Leetcod<em><strong>e</strong></em>&nbsp;的最后一个字符和 i<strong><em>s</em></strong> 的第一个字符 <strong>不</strong> 相等。
58+
这个句子 <strong>不</strong> 是回环句。</pre>
59+
60+
<p>&nbsp;</p>
61+
62+
<p><strong>提示:</strong></p>
63+
64+
<ul>
65+
<li><code>1 &lt;= sentence.length &lt;= 500</code></li>
66+
<li><code>sentence</code> 仅由大小写英文字母和空格组成</li>
67+
<li><code>sentence</code> 中的单词由单个空格进行分隔</li>
68+
<li>不含任何前导或尾随空格</li>
69+
</ul>
70+
71+
## 解法
72+
73+
<!-- 这里可写通用的实现逻辑 -->
74+
75+
**方法一:模拟**
76+
77+
根据题意模拟即可。
78+
79+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串的长度。
80+
81+
<!-- tabs:start -->
82+
83+
### **Python3**
84+
85+
<!-- 这里可写当前语言的特殊实现逻辑 -->
86+
87+
```python
88+
class Solution:
89+
def isCircularSentence(self, sentence: str) -> bool:
90+
sentence = sentence.split()
91+
return all(s[0] == sentence[i - 1][-1] for i, s in enumerate(sentence))
92+
```
93+
94+
### **Java**
95+
96+
<!-- 这里可写当前语言的特殊实现逻辑 -->
97+
98+
```java
99+
class Solution {
100+
public boolean isCircularSentence(String sentence) {
101+
if (sentence.charAt(0) != sentence.charAt(sentence.length() - 1)) {
102+
return false;
103+
}
104+
String[] ss = sentence.split(" ");
105+
for (int i = 1; i < ss.length; ++i) {
106+
if (ss[i].charAt(0) != ss[i - 1].charAt(ss[i - 1].length() - 1)) {
107+
return false;
108+
}
109+
}
110+
return true;
111+
}
112+
}
113+
```
114+
115+
### **C++**
116+
117+
```cpp
118+
class Solution {
119+
public:
120+
bool isCircularSentence(string sentence) {
121+
if (sentence[0] != sentence[sentence.size() - 1]) return false;
122+
istringstream is(sentence);
123+
vector<string> ss;
124+
string s;
125+
while (is >> s) ss.emplace_back(s);
126+
for (int i = 1; i < ss.size(); ++i) {
127+
if (ss[i][0] != ss[i - 1][ss[i - 1].size() - 1]) {
128+
return false;
129+
}
130+
}
131+
return true;
132+
}
133+
};
134+
```
135+
136+
### **Go**
137+
138+
```go
139+
func isCircularSentence(sentence string) bool {
140+
if sentence[0] != sentence[len(sentence)-1] {
141+
return false
142+
}
143+
ss := strings.Split(sentence, " ")
144+
for i := 1; i < len(ss); i++ {
145+
if ss[i][0] != ss[i-1][len(ss[i-1])-1] {
146+
return false
147+
}
148+
}
149+
return true
150+
}
151+
```
152+
153+
### **...**
154+
155+
```
156+
157+
```
158+
159+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# [2490. Circular Sentence](https://leetcode.com/problems/circular-sentence)
2+
3+
[中文文档](/solution/2400-2499/2490.Circular%20Sentence/README.md)
4+
5+
## Description
6+
7+
<p>A <strong>sentence</strong> is a list of words that are separated by a<strong> single</strong> space with no leading or trailing spaces.</p>
8+
9+
<ul>
10+
<li>For example, <code>&quot;Hello World&quot;</code>, <code>&quot;HELLO&quot;</code>, <code>&quot;hello world hello world&quot;</code> are all sentences.</li>
11+
</ul>
12+
13+
<p>Words consist of <strong>only</strong> uppercase and lowercase English letters. Uppercase and lowercase English letters are considered different.</p>
14+
15+
<p>A sentence is <strong>circular </strong>if:</p>
16+
17+
<ul>
18+
<li>The last character of a word is equal to the first character of the next word.</li>
19+
<li>The last character of the last word is equal to the first character of the first word.</li>
20+
</ul>
21+
22+
<p>For example, <code>&quot;leetcode exercises sound delightful&quot;</code>, <code>&quot;eetcode&quot;</code>, <code>&quot;leetcode eats soul&quot; </code>are all circular sentences. However, <code>&quot;Leetcode is cool&quot;</code>, <code>&quot;happy Leetcode&quot;</code>, <code>&quot;Leetcode&quot;</code> and <code>&quot;I like Leetcode&quot;</code> are <strong>not</strong> circular sentences.</p>
23+
24+
<p>Given a string <code>sentence</code>, return <code>true</code><em> if it is circular</em>. Otherwise, return <code>false</code>.</p>
25+
26+
<p>&nbsp;</p>
27+
<p><strong class="example">Example 1:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> sentence = &quot;leetcode exercises sound delightful&quot;
31+
<strong>Output:</strong> true
32+
<strong>Explanation:</strong> The words in sentence are [&quot;leetcode&quot;, &quot;exercises&quot;, &quot;sound&quot;, &quot;delightful&quot;].
33+
- leetcod<u>e</u>&#39;s&nbsp;last character is equal to <u>e</u>xercises&#39;s first character.
34+
- exercise<u>s</u>&#39;s&nbsp;last character is equal to <u>s</u>ound&#39;s first character.
35+
- soun<u>d</u>&#39;s&nbsp;last character is equal to <u>d</u>elightful&#39;s first character.
36+
- delightfu<u>l</u>&#39;s&nbsp;last character is equal to <u>l</u>eetcode&#39;s first character.
37+
The sentence is circular.</pre>
38+
39+
<p><strong class="example">Example 2:</strong></p>
40+
41+
<pre>
42+
<strong>Input:</strong> sentence = &quot;eetcode&quot;
43+
<strong>Output:</strong> true
44+
<strong>Explanation:</strong> The words in sentence are [&quot;eetcode&quot;].
45+
- eetcod<u>e</u>&#39;s&nbsp;last character is equal to <u>e</u>etcode&#39;s first character.
46+
The sentence is circular.</pre>
47+
48+
<p><strong class="example">Example 3:</strong></p>
49+
50+
<pre>
51+
<strong>Input:</strong> sentence = &quot;Leetcode is cool&quot;
52+
<strong>Output:</strong> false
53+
<strong>Explanation:</strong> The words in sentence are [&quot;Leetcode&quot;, &quot;is&quot;, &quot;cool&quot;].
54+
- Leetcod<u>e</u>&#39;s&nbsp;last character is <strong>not</strong> equal to <u>i</u>s&#39;s first character.
55+
The sentence is <strong>not</strong> circular.</pre>
56+
57+
<p>&nbsp;</p>
58+
<p><strong>Constraints:</strong></p>
59+
60+
<ul>
61+
<li><code>1 &lt;= sentence.length &lt;= 500</code></li>
62+
<li><code>sentence</code> consist of only lowercase and uppercase English letters and spaces.</li>
63+
<li>The words in <code>sentence</code> are separated by a single space.</li>
64+
<li>There are no leading or trailing spaces.</li>
65+
</ul>
66+
67+
## Solutions
68+
69+
<!-- tabs:start -->
70+
71+
### **Python3**
72+
73+
```python
74+
class Solution:
75+
def isCircularSentence(self, sentence: str) -> bool:
76+
sentence = sentence.split()
77+
return all(s[0] == sentence[i - 1][-1] for i, s in enumerate(sentence))
78+
```
79+
80+
### **Java**
81+
82+
```java
83+
class Solution {
84+
public boolean isCircularSentence(String sentence) {
85+
if (sentence.charAt(0) != sentence.charAt(sentence.length() - 1)) {
86+
return false;
87+
}
88+
String[] ss = sentence.split(" ");
89+
for (int i = 1; i < ss.length; ++i) {
90+
if (ss[i].charAt(0) != ss[i - 1].charAt(ss[i - 1].length() - 1)) {
91+
return false;
92+
}
93+
}
94+
return true;
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
bool isCircularSentence(string sentence) {
105+
if (sentence[0] != sentence[sentence.size() - 1]) return false;
106+
istringstream is(sentence);
107+
vector<string> ss;
108+
string s;
109+
while (is >> s) ss.emplace_back(s);
110+
for (int i = 1; i < ss.size(); ++i) {
111+
if (ss[i][0] != ss[i - 1][ss[i - 1].size() - 1]) {
112+
return false;
113+
}
114+
}
115+
return true;
116+
}
117+
};
118+
```
119+
120+
### **Go**
121+
122+
```go
123+
func isCircularSentence(sentence string) bool {
124+
if sentence[0] != sentence[len(sentence)-1] {
125+
return false
126+
}
127+
ss := strings.Split(sentence, " ")
128+
for i := 1; i < len(ss); i++ {
129+
if ss[i][0] != ss[i-1][len(ss[i-1])-1] {
130+
return false
131+
}
132+
}
133+
return true
134+
}
135+
```
136+
137+
### **...**
138+
139+
```
140+
141+
```
142+
143+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
bool isCircularSentence(string sentence) {
4+
if (sentence[0] != sentence[sentence.size() - 1]) return false;
5+
istringstream is(sentence);
6+
vector<string> ss;
7+
string s;
8+
while (is >> s) ss.emplace_back(s);
9+
for (int i = 1; i < ss.size(); ++i) {
10+
if (ss[i][0] != ss[i - 1][ss[i - 1].size() - 1]) {
11+
return false;
12+
}
13+
}
14+
return true;
15+
}
16+
};

0 commit comments

Comments
 (0)
Please sign in to comment.