Skip to content

Commit 15001cc

Browse files
committed
feat: add solutions to lc problems
* No.2273.Find Resultant Array After Removing Anagrams * No.2274.Maximum Consecutive Floors Without Special Floors * No.2275.Largest Combination With Bitwise AND Greater Than Zero * No.2276.Count Integers in Intervals
1 parent 6b05a90 commit 15001cc

File tree

13 files changed

+408
-7
lines changed

13 files changed

+408
-7
lines changed

solution/1300-1399/1377.Frog Position After T Seconds/Solution.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Solution:
2-
def frogPosition(self, n: int, edges: List[List[int]], t: int, target: int) -> float:
2+
def frogPosition(
3+
self, n: int, edges: List[List[int]], t: int, target: int
4+
) -> float:
35
g = defaultdict(list)
46
for u, v in edges:
57
g[u].append(v)

solution/2200-2299/2273.Find Resultant Array After Removing Anagrams/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ words 中不存在互为字母异位词的两个相邻字符串,所以无需
6565
<!-- 这里可写当前语言的特殊实现逻辑 -->
6666

6767
```python
68-
68+
class Solution:
69+
def removeAnagrams(self, words: List[str]) -> List[str]:
70+
return [w for i, w in enumerate(words) if i == 0 or sorted(w) != sorted(words[i - 1])]
6971
```
7072

7173
### **Java**

solution/2200-2299/2273.Find Resultant Array After Removing Anagrams/README_EN.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ No two adjacent strings in words are anagrams of each other, so no operations ar
5252
### **Python3**
5353

5454
```python
55-
55+
class Solution:
56+
def removeAnagrams(self, words: List[str]) -> List[str]:
57+
return [w for i, w in enumerate(words) if i == 0 or sorted(w) != sorted(words[i - 1])]
5658
```
5759

5860
### **Java**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def removeAnagrams(self, words: List[str]) -> List[str]:
3+
return [
4+
w
5+
for i, w in enumerate(words)
6+
if i == 0 or sorted(w) != sorted(words[i - 1])
7+
]

solution/2200-2299/2274.Maximum Consecutive Floors Without Special Floors/README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@
5555
<!-- 这里可写当前语言的特殊实现逻辑 -->
5656

5757
```python
58-
58+
class Solution:
59+
def maxConsecutive(self, bottom: int, top: int, special: List[int]) -> int:
60+
special.sort()
61+
ans = max(special[0] - bottom, top - special[-1])
62+
for i in range(1, len(special)):
63+
ans = max(ans, special[i] - special[i - 1] - 1)
64+
return ans
5965
```
6066

6167
### **Java**

solution/2200-2299/2274.Maximum Consecutive Floors Without Special Floors/README_EN.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ Therefore, we return the maximum number which is 3 floors.
4747
### **Python3**
4848

4949
```python
50-
50+
class Solution:
51+
def maxConsecutive(self, bottom: int, top: int, special: List[int]) -> int:
52+
special.sort()
53+
ans = max(special[0] - bottom, top - special[-1])
54+
for i in range(1, len(special)):
55+
ans = max(ans, special[i] - special[i - 1] - 1)
56+
return ans
5157
```
5258

5359
### **Java**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def maxConsecutive(self, bottom: int, top: int, special: List[int]) -> int:
3+
special.sort()
4+
ans = max(special[0] - bottom, top - special[-1])
5+
for i in range(1, len(special)):
6+
ans = max(ans, special[i] - special[i - 1] - 1)
7+
return ans

solution/2200-2299/2275.Largest Combination With Bitwise AND Greater Than Zero/README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,26 @@
5353

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

56+
**方法一:位运算**
57+
58+
大于 0,实际上就是要求存在某个二进制位(0-31),满足所有数字的这一位均为 1。
59+
5660
<!-- tabs:start -->
5761

5862
### **Python3**
5963

6064
<!-- 这里可写当前语言的特殊实现逻辑 -->
6165

6266
```python
63-
67+
class Solution:
68+
def largestCombination(self, candidates: List[int]) -> int:
69+
ans = 0
70+
for i in range(32):
71+
t = 0
72+
for x in candidates:
73+
t += (x >> i) & 1
74+
ans = max(ans, t)
75+
return ans
6476
```
6577

6678
### **Java**

solution/2200-2299/2275.Largest Combination With Bitwise AND Greater Than Zero/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,15 @@ The size of the combination is 2, so we return 2.
5252
### **Python3**
5353

5454
```python
55-
55+
class Solution:
56+
def largestCombination(self, candidates: List[int]) -> int:
57+
ans = 0
58+
for i in range(32):
59+
t = 0
60+
for x in candidates:
61+
t += (x >> i) & 1
62+
ans = max(ans, t)
63+
return ans
5664
```
5765

5866
### **Java**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def largestCombination(self, candidates: List[int]) -> int:
3+
ans = 0
4+
for i in range(32):
5+
t = 0
6+
for x in candidates:
7+
t += (x >> i) & 1
8+
ans = max(ans, t)
9+
return ans

solution/2200-2299/2276.Count Integers in Intervals/README.md

+116
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ countIntervals.count(); // 返回 8
6262

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

65+
**方法一:线段树**
66+
67+
区间求和问题,且值域较大,采用动态开点线段树。
68+
6569
<!-- tabs:start -->
6670

6771
### **Python3**
@@ -77,6 +81,118 @@ countIntervals.count(); // 返回 8
7781
<!-- 这里可写当前语言的特殊实现逻辑 -->
7882

7983
```java
84+
class Node {
85+
Node left;
86+
Node right;
87+
int l;
88+
int r;
89+
int mid;
90+
int v;
91+
int add;
92+
93+
public Node(int l, int r) {
94+
this.l = l;
95+
this.r = r;
96+
this.mid = (l + r) >> 1;
97+
}
98+
}
99+
100+
class SegmentTree {
101+
private Node root = new Node(1, (int) 1e9 + 1);
102+
103+
public SegmentTree() {
104+
105+
}
106+
107+
public void modify(int l, int r, int v) {
108+
modify(l, r, v, root);
109+
}
110+
111+
public void modify(int l, int r, int v, Node node) {
112+
if (l > r) {
113+
return;
114+
}
115+
if (node.l >= l && node.r <= r) {
116+
node.v = node.r - node.l + 1;
117+
node.add = v;
118+
return;
119+
}
120+
pushdown(node);
121+
if (l <= node.mid) {
122+
modify(l, r, v, node.left);
123+
}
124+
if (r > node.mid) {
125+
modify(l, r, v, node.right);
126+
}
127+
pushup(node);
128+
}
129+
130+
public int query(int l, int r) {
131+
return query(l, r, root);
132+
}
133+
134+
public int query(int l, int r, Node node) {
135+
if (l > r) {
136+
return 0;
137+
}
138+
if (node.l >= l && node.r <= r) {
139+
return node.v;
140+
}
141+
pushdown(node);
142+
int v = 0;
143+
if (l <= node.mid) {
144+
v += query(l, r, node.left);
145+
}
146+
if (r > node.mid) {
147+
v += query(l, r, node.right);
148+
}
149+
return v;
150+
}
151+
152+
public void pushup(Node node) {
153+
node.v = node.left.v + node.right.v;
154+
}
155+
156+
public void pushdown(Node node) {
157+
if (node.left == null) {
158+
node.left = new Node(node.l, node.mid);
159+
}
160+
if (node.right == null) {
161+
node.right = new Node(node.mid + 1, node.r);
162+
}
163+
if (node.add != 0) {
164+
Node left = node.left, right = node.right;
165+
left.add = node.add;
166+
right.add = node.add;
167+
left.v = left.r - left.l + 1;
168+
right.v = right.r - right.l + 1;
169+
node.add = 0;
170+
}
171+
}
172+
}
173+
174+
class CountIntervals {
175+
private SegmentTree tree = new SegmentTree();
176+
177+
public CountIntervals() {
178+
179+
}
180+
181+
public void add(int left, int right) {
182+
tree.modify(left, right, 1);
183+
}
184+
185+
public int count() {
186+
return tree.query(1, (int) 1e9);
187+
}
188+
}
189+
190+
/**
191+
* Your CountIntervals object will be instantiated and called as such:
192+
* CountIntervals obj = new CountIntervals();
193+
* obj.add(left,right);
194+
* int param_2 = obj.count();
195+
*/
80196

81197
```
82198

solution/2200-2299/2276.Count Integers in Intervals/README_EN.md

+112
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,118 @@ countIntervals.count(); // return 8
6868
### **Java**
6969

7070
```java
71+
class Node {
72+
Node left;
73+
Node right;
74+
int l;
75+
int r;
76+
int mid;
77+
int v;
78+
int add;
79+
80+
public Node(int l, int r) {
81+
this.l = l;
82+
this.r = r;
83+
this.mid = (l + r) >> 1;
84+
}
85+
}
86+
87+
class SegmentTree {
88+
private Node root = new Node(1, (int) 1e9 + 1);
89+
90+
public SegmentTree() {
91+
92+
}
93+
94+
public void modify(int l, int r, int v) {
95+
modify(l, r, v, root);
96+
}
97+
98+
public void modify(int l, int r, int v, Node node) {
99+
if (l > r) {
100+
return;
101+
}
102+
if (node.l >= l && node.r <= r) {
103+
node.v = node.r - node.l + 1;
104+
node.add = v;
105+
return;
106+
}
107+
pushdown(node);
108+
if (l <= node.mid) {
109+
modify(l, r, v, node.left);
110+
}
111+
if (r > node.mid) {
112+
modify(l, r, v, node.right);
113+
}
114+
pushup(node);
115+
}
116+
117+
public int query(int l, int r) {
118+
return query(l, r, root);
119+
}
120+
121+
public int query(int l, int r, Node node) {
122+
if (l > r) {
123+
return 0;
124+
}
125+
if (node.l >= l && node.r <= r) {
126+
return node.v;
127+
}
128+
pushdown(node);
129+
int v = 0;
130+
if (l <= node.mid) {
131+
v += query(l, r, node.left);
132+
}
133+
if (r > node.mid) {
134+
v += query(l, r, node.right);
135+
}
136+
return v;
137+
}
138+
139+
public void pushup(Node node) {
140+
node.v = node.left.v + node.right.v;
141+
}
142+
143+
public void pushdown(Node node) {
144+
if (node.left == null) {
145+
node.left = new Node(node.l, node.mid);
146+
}
147+
if (node.right == null) {
148+
node.right = new Node(node.mid + 1, node.r);
149+
}
150+
if (node.add != 0) {
151+
Node left = node.left, right = node.right;
152+
left.add = node.add;
153+
right.add = node.add;
154+
left.v = left.r - left.l + 1;
155+
right.v = right.r - right.l + 1;
156+
node.add = 0;
157+
}
158+
}
159+
}
160+
161+
class CountIntervals {
162+
private SegmentTree tree = new SegmentTree();
163+
164+
public CountIntervals() {
165+
166+
}
167+
168+
public void add(int left, int right) {
169+
tree.modify(left, right, 1);
170+
}
171+
172+
public int count() {
173+
return tree.query(1, (int) 1e9);
174+
}
175+
}
176+
177+
/**
178+
* Your CountIntervals object will be instantiated and called as such:
179+
* CountIntervals obj = new CountIntervals();
180+
* obj.add(left,right);
181+
* int param_2 = obj.count();
182+
*/
71183

72184
```
73185

0 commit comments

Comments
 (0)