Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2aa99c4

Browse files
🐱(dynamic): 898. 子数组按位或操作
1 parent 0ba6ec2 commit 2aa99c4

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

‎docs/algorithm/dynamic/README.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,4 +725,26 @@ class Solution(object):
725725
# 选择买入或不变
726726
hold = max(hold, cash - prices[i])
727727
return cash
728+
```
729+
730+
## 898. 子数组按位或操作
731+
732+
[原题链接](https://leetcode-cn.com/problems/bitwise-ors-of-subarrays/)
733+
734+
### 思路
735+
736+
动态规划,`dp[i]` 存储所有以 `i` 结尾的子数组的或结果(集合)。
737+
738+
由于数据规模为 `1 <= A.length <= 50000`,因此无法使用 `O(n^2)` 的算法。参考 [花花酱 LeetCode 898. Bitwise ORs of Subarrays - 刷题找工作 EP222](https://www.bilibili.com/video/av31142168) 的讲解,说的非常详细了,也大致了解到如何根据问题规模确定复杂度了。
739+
740+
```python
741+
class Solution:
742+
def subarrayBitwiseORs(self, A: List[int]) -> int:
743+
cur = set()
744+
res = set()
745+
# cur 存储以 a 结尾的所有子数组的或结果
746+
for a in A:
747+
cur = {n | a for n in cur} | {a}
748+
res |= cur
749+
return len(res)
728750
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /