Skip to content

Commit 29928e9

Browse files
committed
feat: add solutions to lc problems: No.1869,1870,1871,1872
1 parent 826d27a commit 29928e9

File tree

23 files changed

+1213
-1
lines changed

23 files changed

+1213
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# [1868. ](https://leetcode-cn.com/problems/product-of-two-run-length-encoded-arrays)
2+
3+
[English Version](/solution/1800-1899/1868.Product%20of%20Two%20Run-Length%20Encoded%20Arrays/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
None
10+
11+
## 解法
12+
13+
<!-- 这里可写通用的实现逻辑 -->
14+
15+
<!-- tabs:start -->
16+
17+
### **Python3**
18+
19+
<!-- 这里可写当前语言的特殊实现逻辑 -->
20+
21+
```python
22+
23+
```
24+
25+
### **Java**
26+
27+
<!-- 这里可写当前语言的特殊实现逻辑 -->
28+
29+
```java
30+
31+
```
32+
33+
### **...**
34+
35+
```
36+
37+
```
38+
39+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# [1868. Product of Two Run-Length Encoded Arrays](https://leetcode.com/problems/product-of-two-run-length-encoded-arrays)
2+
3+
[中文文档](/solution/1800-1899/1868.Product%20of%20Two%20Run-Length%20Encoded%20Arrays/README.md)
4+
5+
## Description
6+
7+
<p><strong>Run-length encoding</strong> is a compression algorithm that allows for an integer array <code>nums</code> with many segments of <strong>consecutive repeated</strong> numbers to be represented by a (generally smaller) 2D array <code>encoded</code>. Each <code>encoded[i] = [val<sub>i</sub>, freq<sub>i</sub>]</code> describes the <code>i<sup>th</sup></code> segment of repeated numbers in <code>nums</code> where <code>val<sub>i</sub></code> is the value that is repeated <code>freq<sub>i</sub></code> times.</p>
8+
9+
<ul>
10+
<li>For example, <code>nums = [1,1,1,2,2,2,2,2]</code> is represented by the <strong>run-length encoded</strong> array <code>encoded = [[1,3],[2,5]]</code>. Another way to read this is &quot;three <code>1</code>s followed by five <code>2</code>s&quot;.</li>
11+
</ul>
12+
13+
<p>The <strong>product</strong> of two run-length encoded arrays <code>encoded1</code> and <code>encoded2</code> can be calculated using the following steps:</p>
14+
15+
<ol>
16+
<li><strong>Expand</strong> both <code>encoded1</code> and <code>encoded2</code> into the full arrays <code>nums1</code> and <code>nums2</code> respectively.</li>
17+
<li>Create a new array <code>prodNums</code> of length <code>nums1.length</code> and set <code>prodNums[i] = nums1[i] * nums2[i]</code>.</li>
18+
<li><strong>Compress</strong> <code>prodNums</code> into a run-length encoded array and return it.</li>
19+
</ol>
20+
21+
<p>You are given two <strong>run-length encoded</strong> arrays <code>encoded1</code> and <code>encoded2</code> representing full arrays <code>nums1</code> and <code>nums2</code> respectively. Both <code>nums1</code> and <code>nums2</code> have the <strong>same length</strong>. Each <code>encoded1[i] = [val<sub>i</sub>, freq<sub>i</sub>]</code> describes the <code>i<sup>th</sup></code> segment of <code>nums1</code>, and each <code>encoded2[j] = [val<sub>j</sub>, freq<sub>j</sub>]</code> describes the <code>j<sup>th</sup></code> segment of <code>nums2</code>.</p>
22+
23+
<p>Return <i>the <strong>product</strong> of </i><code>encoded1</code><em> and </em><code>encoded2</code>.</p>
24+
25+
<p><strong>Note:</strong> Compression should be done such that the run-length encoded array has the <strong>minimum</strong> possible length.</p>
26+
27+
<p>&nbsp;</p>
28+
<p><strong>Example 1:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> encoded1 = [[1,3],[2,3]], encoded2 = [[6,3],[3,3]]
32+
<strong>Output:</strong> [[6,6]]
33+
<strong>Explanation:</strong> encoded1 expands to [1,1,1,2,2,2] and encoded2 expands to [6,6,6,3,3,3].
34+
prodNums = [6,6,6,6,6,6], which is compressed into the run-length encoded array [[6,6]].
35+
</pre>
36+
37+
<p><strong>Example 2:</strong></p>
38+
39+
<pre>
40+
<strong>Input:</strong> encoded1 = [[1,3],[2,1],[3,2]], encoded2 = [[2,3],[3,3]]
41+
<strong>Output:</strong> [[2,3],[6,1],[9,2]]
42+
<strong>Explanation:</strong> encoded1 expands to [1,1,1,2,3,3] and encoded2 expands to [2,2,2,3,3,3].
43+
prodNums = [2,2,2,6,9,9], which is compressed into the run-length encoded array [[2,3],[6,1],[9,2]].
44+
</pre>
45+
46+
<p>&nbsp;</p>
47+
<p><strong>Constraints:</strong></p>
48+
49+
<ul>
50+
<li><code>1 &lt;= encoded1.length, encoded2.length &lt;= 10<sup>5</sup></code></li>
51+
<li><code>encoded1[i].length == 2</code></li>
52+
<li><code>encoded2[j].length == 2</code></li>
53+
<li><code>1 &lt;= val<sub>i</sub>, freq<sub>i</sub> &lt;= 10<sup>4</sup></code> for each <code>encoded1[i]</code>.</li>
54+
<li><code>1 &lt;= val<sub>j</sub>, freq<sub>j</sub> &lt;= 10<sup>4</sup></code> for each <code>encoded2[j]</code>.</li>
55+
<li>The full arrays that <code>encoded1</code> and <code>encoded2</code> represent are the same length.</li>
56+
</ul>
57+
58+
59+
## Solutions
60+
61+
<!-- tabs:start -->
62+
63+
### **Python3**
64+
65+
```python
66+
67+
```
68+
69+
### **Java**
70+
71+
```java
72+
73+
```
74+
75+
### **...**
76+
77+
```
78+
79+
```
80+
81+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# [1869. 哪种连续子字符串更长](https://leetcode-cn.com/problems/longer-contiguous-segments-of-ones-than-zeros)
2+
3+
[English Version](/solution/1800-1899/1869.Longer%20Contiguous%20Segments%20of%20Ones%20than%20Zeros/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个二进制字符串 <code>s</code> 。如果字符串中由 <code>1</code> 组成的 <strong>最长</strong> 连续子字符串 <strong>严格长于</strong> 由 <code>0</code> 组成的 <strong>最长</strong> 连续子字符串,返回 <code>true</code> ;否则,返回 <code>false</code><em> </em>。</p>
10+
11+
<ul>
12+
<li>例如,<code>s = "<strong>11</strong>01<strong>000</strong>10"</code> 中,由 <code>1</code> 组成的最长连续子字符串的长度是 <code>2</code> ,由 <code>0</code> 组成的最长连续子字符串的长度是 <code>3</code> 。</li>
13+
</ul>
14+
15+
<p>注意,如果字符串中不存在 <code>0</code> ,此时认为由 <code>0</code> 组成的最长连续子字符串的长度是 <code>0</code> 。字符串中不存在 <code>1</code> 的情况也适用此规则。</p>
16+
17+
<p> </p>
18+
19+
<p><strong>示例 1:</strong></p>
20+
21+
<pre>
22+
<strong>输入:</strong>s = "1101"
23+
<strong>输出:</strong>true
24+
<strong>解释:</strong>
25+
由 1 组成的最长连续子字符串的长度是 2:"<strong>11</strong>01"
26+
由 0 组成的最长连续子字符串的长度是 1:"11<strong>0</strong>1"
27+
由 1 组成的子字符串更长,故返回 true 。
28+
</pre>
29+
30+
<p><strong>示例 2:</strong></p>
31+
32+
<pre>
33+
<strong>输入:</strong>s = "111000"
34+
<strong>输出:</strong>false
35+
<strong>解释:</strong>
36+
由 1 组成的最长连续子字符串的长度是 3:"<strong>111</strong>000"
37+
由 0 组成的最长连续子字符串的长度是 3:"111<strong>000</strong>"
38+
由 1 组成的子字符串不比由 0 组成的子字符串长,故返回 false 。
39+
</pre>
40+
41+
<p><strong>示例 3:</strong></p>
42+
43+
<pre>
44+
<strong>输入:</strong>s = "110100010"
45+
<strong>输出:</strong>false
46+
<strong>解释:</strong>
47+
由 1 组成的最长连续子字符串的长度是 2:"<strong>11</strong>0100010"
48+
由 0 组成的最长连续子字符串的长度是 3:"1101<strong>000</strong>10"
49+
由 1 组成的子字符串不比由 0 组成的子字符串长,故返回 false 。
50+
</pre>
51+
52+
<p> </p>
53+
54+
<p><strong>提示:</strong></p>
55+
56+
<ul>
57+
<li><code>1 <= s.length <= 100</code></li>
58+
<li><code>s[i]</code> 不是 <code>'0'</code> 就是 <code>'1'</code></li>
59+
</ul>
60+
61+
## 解法
62+
63+
<!-- 这里可写通用的实现逻辑 -->
64+
65+
直接遍历字符串,获取“0 子串”和“1 子串”的最大长度 `len0``len1`
66+
67+
遍历结束后,若 `len1 > len0`,返回 true,否则返回 false。
68+
69+
<!-- tabs:start -->
70+
71+
### **Python3**
72+
73+
<!-- 这里可写当前语言的特殊实现逻辑 -->
74+
75+
```python
76+
class Solution:
77+
def checkZeroOnes(self, s: str) -> bool:
78+
len0 = len1 = 0
79+
t0 = t1 = 0
80+
for c in s:
81+
if c == '0':
82+
t0 += 1
83+
t1 = 0
84+
else:
85+
t0 = 0
86+
t1 += 1
87+
len0 = max(len0, t0)
88+
len1 = max(len1, t1)
89+
return len1 > len0
90+
```
91+
92+
### **Java**
93+
94+
<!-- 这里可写当前语言的特殊实现逻辑 -->
95+
96+
```java
97+
class Solution {
98+
public boolean checkZeroOnes(String s) {
99+
int len0 = 0, len1 = 0;
100+
int t0 = 0, t1 = 0;
101+
for (int i = 0; i < s.length(); ++i) {
102+
if (s.charAt(i) == '0') {
103+
t0 += 1;
104+
t1 = 0;
105+
} else {
106+
t0 = 0;
107+
t1 += 1;
108+
}
109+
len0 = Math.max(len0, t0);
110+
len1 = Math.max(len1, t1);
111+
}
112+
return len1 > len0;
113+
}
114+
}
115+
```
116+
117+
### **...**
118+
119+
```
120+
121+
```
122+
123+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# [1869. Longer Contiguous Segments of Ones than Zeros](https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros)
2+
3+
[中文文档](/solution/1800-1899/1869.Longer%20Contiguous%20Segments%20of%20Ones%20than%20Zeros/README.md)
4+
5+
## Description
6+
7+
<p>Given a binary string <code>s</code>, return <code>true</code><em> if the <strong>longest</strong> contiguous segment of </em><code>1</code><em>s is <strong>strictly longer</strong> than the <strong>longest</strong> contiguous segment of </em><code>0</code><em>s in </em><code>s</code>. Return <code>false</code><em> otherwise</em>.</p>
8+
9+
<ul>
10+
<li>For example, in <code>s = &quot;<u>11</u>01<u>000</u>10&quot;</code> the longest contiguous segment of <code>1</code>s has length <code>2</code>, and the longest contiguous segment of <code>0</code>s has length <code>3</code>.</li>
11+
</ul>
12+
13+
<p>Note that if there are no <code>0</code>s, then the longest contiguous segment of <code>0</code>s is considered to have length <code>0</code>. The same applies if there are no <code>1</code>s.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong>Example 1:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> s = &quot;1101&quot;
20+
<strong>Output:</strong> true
21+
<strong>Explanation:</strong>
22+
The longest contiguous segment of 1s has length 2: &quot;<u>11</u>01&quot;
23+
The longest contiguous segment of 0s has length 1: &quot;11<u>0</u>1&quot;
24+
The segment of 1s is longer, so return true.
25+
</pre>
26+
27+
<p><strong>Example 2:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> s = &quot;111000&quot;
31+
<strong>Output:</strong> false
32+
<strong>Explanation:</strong>
33+
The longest contiguous segment of 1s has length 3: &quot;<u>111</u>000&quot;
34+
The longest contiguous segment of 0s has length 3: &quot;111<u>000</u>&quot;
35+
The segment of 1s is not longer, so return false.
36+
</pre>
37+
38+
<p><strong>Example 3:</strong></p>
39+
40+
<pre>
41+
<strong>Input:</strong> s = &quot;110100010&quot;
42+
<strong>Output:</strong> false
43+
<strong>Explanation:</strong>
44+
The longest contiguous segment of 1s has length 2: &quot;<u>11</u>0100010&quot;
45+
The longest contiguous segment of 0s has length 3: &quot;1101<u>000</u>10&quot;
46+
The segment of 1s is not longer, so return false.
47+
</pre>
48+
49+
<p>&nbsp;</p>
50+
<p><strong>Constraints:</strong></p>
51+
52+
<ul>
53+
<li><code>1 &lt;= s.length &lt;= 100</code></li>
54+
<li><code>s[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>
55+
</ul>
56+
57+
## Solutions
58+
59+
<!-- tabs:start -->
60+
61+
### **Python3**
62+
63+
```python
64+
class Solution:
65+
def checkZeroOnes(self, s: str) -> bool:
66+
len0 = len1 = 0
67+
t0 = t1 = 0
68+
for c in s:
69+
if c == '0':
70+
t0 += 1
71+
t1 = 0
72+
else:
73+
t0 = 0
74+
t1 += 1
75+
len0 = max(len0, t0)
76+
len1 = max(len1, t1)
77+
return len1 > len0
78+
```
79+
80+
### **Java**
81+
82+
```java
83+
class Solution {
84+
public boolean checkZeroOnes(String s) {
85+
int len0 = 0, len1 = 0;
86+
int t0 = 0, t1 = 0;
87+
for (int i = 0; i < s.length(); ++i) {
88+
if (s.charAt(i) == '0') {
89+
t0 += 1;
90+
t1 = 0;
91+
} else {
92+
t0 = 0;
93+
t1 += 1;
94+
}
95+
len0 = Math.max(len0, t0);
96+
len1 = Math.max(len1, t1);
97+
}
98+
return len1 > len0;
99+
}
100+
}
101+
```
102+
103+
### **...**
104+
105+
```
106+
107+
```
108+
109+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public boolean checkZeroOnes(String s) {
3+
int len0 = 0, len1 = 0;
4+
int t0 = 0, t1 = 0;
5+
for (int i = 0; i < s.length(); ++i) {
6+
if (s.charAt(i) == '0') {
7+
t0 += 1;
8+
t1 = 0;
9+
} else {
10+
t0 = 0;
11+
t1 += 1;
12+
}
13+
len0 = Math.max(len0, t0);
14+
len1 = Math.max(len1, t1);
15+
}
16+
return len1 > len0;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def checkZeroOnes(self, s: str) -> bool:
3+
len0 = len1 = 0
4+
t0 = t1 = 0
5+
for c in s:
6+
if c == '0':
7+
t0 += 1
8+
t1 = 0
9+
else:
10+
t0 = 0
11+
t1 += 1
12+
len0 = max(len0, t0)
13+
len1 = max(len1, t1)
14+
return len1 > len0

0 commit comments

Comments
 (0)