Skip to content

Commit 03d8307

Browse files
committed
feat: add solutions to lc problem: No.1773
No.1773.Count Items Matching a Rule
1 parent f11560d commit 03d8307

File tree

9 files changed

+136
-46
lines changed

9 files changed

+136
-46
lines changed

solution/0900-0999/0907.Sum of Subarray Minimums/README.md

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,33 @@
4545

4646
**方法一:单调栈**
4747

48-
枚举 $arr[i]$ 作为子数组的最小值,找出左侧第一个比 $arr[i]$ 小的位置 $left[i]$,右侧第一个不大于 $arr[i]$ 的位置 $right[i]$。由此可以算出最小值 $arr[i]$ 可以出现在多少个子数组中
48+
题目要求的是每个子数组的最小值之和,实际上相当于,对于每个元素 $arr[i]$,求以 $arr[i]$ 为最小值的子数组的个数,然后乘以 $arr[i]$,最后求和
4949

50-
计算每个 $arr[i]$ 的贡献 $(i-left[i])*(right[i]-i)*arr[i]$,累加得到结果
50+
因此,题目的重点转换为:求以 $arr[i]$ 为最小值的子数组的个数。对于 $arr[i]$,我们找出其左边第一个小于 $arr[i]$ 的位置 $left[i]$,右侧第一个小于等于 $arr[i]$ 的位置 $right[i]$,则以 $arr[i]$ 为最小值的子数组的个数为 $(i - left[i]) \times (right[i] - i)$
5151

52-
注意数据溢出与取模操作。
52+
注意,这里为什么要求右侧第一个小于等于 $arr[i]$ 的位置 $right[i]$,而不是小于 $arr[i]$ 的位置呢?这是因为,如果是右侧第一个小于 $arr[i]$ 的位置 $right[i]$,则会导致重复计算。
53+
54+
我们可以举个例子来说明,对于以下数组:
55+
56+
下标为 $3$ 的元素大小为 $2$,左侧第一个小于 $2$ 的元素下标为 $0$,如果我们求右侧第一个小于 $2$ 的元素下标,可以得到下标为 $7$。也即是说,子数组区间为 $(0, 7)$。注意,这里是开区间。
57+
58+
```
59+
0 4 3 2 5 3 2 1
60+
* ^ *
61+
```
62+
63+
按照同样的方法,我们可以求出下标为 $6$ 的元素的子数组区间,可以发现,其子数组区间也为 $(0, 7)$,也即是说,下标为 $3$ 和下标为 $6$ 的元素的子数组区间是重复的。这样就造成了重复计算。
64+
65+
```
66+
0 4 3 2 5 3 2 1
67+
* ^ *
68+
```
69+
70+
如果我们求的是右侧第一个小于等于其值的下标,就不会有重复问题,因为下标为 $3$ 的子数组区间变为 $(0, 6)$,下标为 $6$ 的子数组区间为 $(0, 7)$,两者不重复。
71+
72+
回到这道题上,我们只需要遍历数组,对于每个元素 $arr[i]$,利用单调栈求出其左侧第一个小于 $arr[i]$ 的位置 $left[i]$,右侧第一个小于等于 $arr[i]$ 的位置 $right[i]$,则以 $arr[i]$ 为最小值的子数组的个数为 $(i - left[i]) \times (right[i] - i)$,然后乘以 $arr[i]$,最后求和即可。
73+
74+
注意数据的溢出以及取模操作。
5375

5476
时间复杂度 $O(n)$,其中 $n$ 表示数组 $arr$ 的长度。
5577

@@ -72,14 +94,15 @@ class Solution:
7294
if stk:
7395
left[i] = stk[-1]
7496
stk.append(i)
97+
7598
stk = []
7699
for i in range(n - 1, -1, -1):
77100
while stk and arr[stk[-1]] > arr[i]:
78101
stk.pop()
79102
if stk:
80103
right[i] = stk[-1]
81104
stk.append(i)
82-
mod = int(1e9) + 7
105+
mod = 10**9 + 7
83106
return sum((i - left[i]) * (right[i] - i) * v for i, v in enumerate(arr)) % mod
84107
```
85108

solution/0900-0999/0907.Sum of Subarray Minimums/README_EN.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,15 @@ class Solution:
5252
if stk:
5353
left[i] = stk[-1]
5454
stk.append(i)
55+
5556
stk = []
5657
for i in range(n - 1, -1, -1):
5758
while stk and arr[stk[-1]] > arr[i]:
5859
stk.pop()
5960
if stk:
6061
right[i] = stk[-1]
6162
stk.append(i)
62-
mod = int(1e9) + 7
63+
mod = 10**9 + 7
6364
return sum((i - left[i]) * (right[i] - i) * v for i, v in enumerate(arr)) % mod
6465
```
6566

solution/0900-0999/0907.Sum of Subarray Minimums/Solution.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ def sumSubarrayMins(self, arr: List[int]) -> int:
1010
if stk:
1111
left[i] = stk[-1]
1212
stk.append(i)
13+
1314
stk = []
1415
for i in range(n - 1, -1, -1):
1516
while stk and arr[stk[-1]] > arr[i]:
1617
stk.pop()
1718
if stk:
1819
right[i] = stk[-1]
1920
stk.append(i)
20-
mod = int(1e9) + 7
21-
return sum((i - left[i]) * (right[i] - i) * v for i, v in enumerate(arr)) % mod
21+
mod = 10**9 + 7
22+
return sum((i - left[i]) * (right[i] - i) * v for i, v in enumerate(arr)) % mod

solution/1700-1799/1773.Count Items Matching a Rule/README.md

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@
5252

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

55+
**方法一:直接模拟**
56+
57+
由于 `ruleKey` 只可能是 `"type"``"color"``"name"`,我们可以直接取 `ruleKey` 的第一个字符 $x$ 来确定 `item` 的下标 $i$。然后遍历 `items` 数组,统计 `item[i] == ruleValue` 的个数即可。
58+
59+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为 `items` 的长度。
60+
5561
<!-- tabs:start -->
5662

5763
### **Python3**
@@ -61,9 +67,8 @@
6167
```python
6268
class Solution:
6369
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
64-
count = 0
65-
m = {'type': 0, 'color': 1, 'name': 2}
66-
return sum([item[m[ruleKey]] == ruleValue for item in items])
70+
i = 0 if ruleKey[0] == 't' else (1 if ruleKey[0] == 'c' else 2)
71+
return sum(v[i] == ruleValue for v in items)
6772
```
6873

6974
### **Java**
@@ -73,22 +78,46 @@ class Solution:
7378
```java
7479
class Solution {
7580
public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
76-
int count = 0;
77-
for (List<String> item : items) {
78-
String t = item.get(0), c = item.get(1), n = item.get(2);
79-
if ("type".equals(ruleKey) && t.equals(ruleValue)) {
80-
++count;
81-
} else if ("color".equals(ruleKey) && c.equals(ruleValue)) {
82-
++count;
83-
} else if ("name".equals(ruleKey) && n.equals(ruleValue)) {
84-
++count;
81+
char x = ruleKey.charAt(0);
82+
int i = x == 't' ? 0 : (x == 'c' ? 1 : 2);
83+
int ans = 0;
84+
for (var v : items) {
85+
if (v.get(i).equals(ruleValue)) {
86+
++ans;
8587
}
8688
}
87-
return count;
89+
return ans;
8890
}
8991
}
9092
```
9193

94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
int countMatches(vector<vector<string>>& items, string ruleKey, string ruleValue) {
100+
char x = ruleKey[0];
101+
int i = x == 't' ? 0 : (x == 'c' ? 1 : 2);
102+
return count_if(items.begin(), items.end(), [&](auto& v) { return v[i] == ruleValue; });
103+
}
104+
};
105+
```
106+
107+
### **Go**
108+
109+
```go
110+
func countMatches(items [][]string, ruleKey string, ruleValue string) (ans int) {
111+
i := map[byte]int{'t': 0, 'c': 1, 'n': 2}[ruleKey[0]]
112+
for _, v := range items {
113+
if v[i] == ruleValue {
114+
ans++
115+
}
116+
}
117+
return
118+
}
119+
```
120+
92121
### **...**
93122

94123
```

solution/1700-1799/1773.Count Items Matching a Rule/README_EN.md

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,32 +51,55 @@
5151
```python
5252
class Solution:
5353
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
54-
count = 0
55-
m = {'type': 0, 'color': 1, 'name': 2}
56-
return sum([item[m[ruleKey]] == ruleValue for item in items])
54+
i = 0 if ruleKey[0] == 't' else (1 if ruleKey[0] == 'c' else 2)
55+
return sum(v[i] == ruleValue for v in items)
5756
```
5857

5958
### **Java**
6059

6160
```java
6261
class Solution {
6362
public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
64-
int count = 0;
65-
for (List<String> item : items) {
66-
String t = item.get(0), c = item.get(1), n = item.get(2);
67-
if ("type".equals(ruleKey) && t.equals(ruleValue)) {
68-
++count;
69-
} else if ("color".equals(ruleKey) && c.equals(ruleValue)) {
70-
++count;
71-
} else if ("name".equals(ruleKey) && n.equals(ruleValue)) {
72-
++count;
63+
char x = ruleKey.charAt(0);
64+
int i = x == 't' ? 0 : (x == 'c' ? 1 : 2);
65+
int ans = 0;
66+
for (var v : items) {
67+
if (v.get(i).equals(ruleValue)) {
68+
++ans;
7369
}
7470
}
75-
return count;
71+
return ans;
7672
}
7773
}
7874
```
7975

76+
### **C++**
77+
78+
```cpp
79+
class Solution {
80+
public:
81+
int countMatches(vector<vector<string>>& items, string ruleKey, string ruleValue) {
82+
char x = ruleKey[0];
83+
int i = x == 't' ? 0 : (x == 'c' ? 1 : 2);
84+
return count_if(items.begin(), items.end(), [&](auto& v) { return v[i] == ruleValue; });
85+
}
86+
};
87+
```
88+
89+
### **Go**
90+
91+
```go
92+
func countMatches(items [][]string, ruleKey string, ruleValue string) (ans int) {
93+
i := map[byte]int{'t': 0, 'c': 1, 'n': 2}[ruleKey[0]]
94+
for _, v := range items {
95+
if v[i] == ruleValue {
96+
ans++
97+
}
98+
}
99+
return
100+
}
101+
```
102+
80103
### **...**
81104

82105
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public:
3+
int countMatches(vector<vector<string>>& items, string ruleKey, string ruleValue) {
4+
char x = ruleKey[0];
5+
int i = x == 't' ? 0 : (x == 'c' ? 1 : 2);
6+
return count_if(items.begin(), items.end(), [&](auto& v) { return v[i] == ruleValue; });
7+
}
8+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func countMatches(items [][]string, ruleKey string, ruleValue string) (ans int) {
2+
i := map[byte]int{'t': 0, 'c': 1, 'n': 2}[ruleKey[0]]
3+
for _, v := range items {
4+
if v[i] == ruleValue {
5+
ans++
6+
}
7+
}
8+
return
9+
}
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
class Solution {
22
public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
3-
int count = 0;
4-
for (List<String> item : items) {
5-
String t = item.get(0), c = item.get(1), n = item.get(2);
6-
if ("type".equals(ruleKey) && t.equals(ruleValue)) {
7-
++count;
8-
} else if ("color".equals(ruleKey) && c.equals(ruleValue)) {
9-
++count;
10-
} else if ("name".equals(ruleKey) && n.equals(ruleValue)) {
11-
++count;
3+
char x = ruleKey.charAt(0);
4+
int i = x == 't' ? 0 : (x == 'c' ? 1 : 2);
5+
int ans = 0;
6+
for (var v : items) {
7+
if (v.get(i).equals(ruleValue)) {
8+
++ans;
129
}
1310
}
14-
return count;
11+
return ans;
1512
}
1613
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class Solution:
22
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
3-
count = 0
4-
m = {'type': 0, 'color': 1, 'name': 2}
5-
return sum([item[m[ruleKey]] == ruleValue for item in items])
3+
i = 0 if ruleKey[0] == 't' else (1 if ruleKey[0] == 'c' else 2)
4+
return sum(v[i] == ruleValue for v in items)

0 commit comments

Comments
 (0)