作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/bitwise-ors-of-subarrays/description/

题目描述

We have an array A of non-negative integers.

For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j].

Return the number of possible results. (Results that occur more than once are only counted once in the final answer.)

Example 1:

Input: [0]
Output: 1
Explanation:
There is only one possible result: 0.

Example 2:

Input: [1,1,2]
Output: 3
Explanation:
The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2].
These yield the results 1, 1, 2, 1, 3, 3.
There are 3 unique values, so the answer is 3.

Example 3:

Input: [1,2,4]
Output: 6
Explanation:
The possible results are 1, 2, 3, 4, 6, and 7.

Note:

  1. 1 <= A.length <= 50000
  2. 0 <= A[i] <= 10^9

题目大意

一个数组的所有子数组的异或结果,总共有多少个不同?

解题方法

动态规划

题目不是一般的难啊,如果是普通的DP方法,那么使用二维dp[i][j]表示子数组的起始和结束区间,能做到O(n^2)的时间复杂度,但是题目对时间复杂度要求的很死,必须O(N).

正确的做法也是动态规划,dp[i]表示以A[i]结尾的所有子数组的异或结果,其实是个set。

转移方程式dp[i] = [b | A[i] for b in dp[i - 1]] + A[i],即以A[i]结尾的所有子数组异或结果等于以A[i-1]结尾的所有子数组异或结果,和当前的A[i]异或,再加上A[i]这个结果。

同时使用一个set保存所有的异或结果。最后返回这个结果set的长度。

dp[i]的大小至多是32个,即 |dp[i]| <= 32 的证明:

dp[i] = {A[i], A[i] | A[i - 1], A[i] | A[i - 1] | A[i - 2], … , A[i] | A[i - 1] | … | A[0]},这个序列单调递增,通过把A[i]中的0变成1。A[i]最多有32个0。所以这个集合的大小 <= 32。

举例:最坏情况 A = [8, 4, 2, 1, 0] 都是2^k。
A[5] = 0,dp[5] = {0, 0 | 1, 0 | 1 | 2, 0 | 1 | 2 | 4, 0 | 1 | 2 | 4 | 8} = {0, 1, 3, 7, 15}.

时间复杂度是O(32*N),空间复杂度是O(32N).

class Solution(object):
def subarrayBitwiseORs(self, A):
"""
:type A: List[int]
:rtype: int
"""
res = set()
cur = set()
for a in A:
cur = {n | a for n in cur} | {a}
res |= cur
return len(res)

相似题目

参考资料

https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-898-bitwise-ors-of-subarrays/

日期

2018 年 10 月 28 日 —— 10月份最后一个周一

【LeetCode】898. Bitwise ORs of Subarrays 解题报告(Python)的更多相关文章

  1. [LeetCode] 898. Bitwise ORs of Subarrays 子数组按位或操作

    We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., ...

  2. 898. Bitwise ORs of Subarrays

    We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., ...

  3. LC 898. Bitwise ORs of Subarrays

    We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., ...

  4. 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...

  5. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  6. 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...

  7. 【LeetCode】760. Find Anagram Mappings 解题报告

    [LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...

  8. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  9. 【LeetCode】299. Bulls and Cows 解题报告(Python)

    [LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

随机推荐

  1. Linux终端命令行的快捷键列表

    tab键:命令.文件名等自动补全功能. Ctrl+a:光标回到命令行首.Ctrl+e:光标回到命令行尾.Ctrl+k:删除光标处到行尾的字符.Ctrl+u:删除整个命令行文本字符.ctrl+y: 粘贴 ...

  2. Telink BLE MESH PWM波的小结

    本本针对Telink BLE MESH SDK  灯控的使用进行说明. 1.调整灯光的频率 默认情况下 SDK PWM波的频率是 600HZ的,有时我们需要将它调整频率,例如调整为4K,只需要更改参数 ...

  3. 笔记Mysql(1)

    客户端的登录命令 查看版本 查看设置(数据库的基本设置信息) 查看时间 查看链接数 查看超时的关键字 创建数据库 查看已有数据库 进入数据库,查询链接到的数据库 查询数据目录 创建表 查看表

  4. C#页面缓存设置

    protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Sessioninfo(); } Session.R ...

  5. flink---实时项目--day02-----1. 解析参数工具类 2. Flink工具类封装 3. 日志采集架构图 4. 测流输出 5. 将kafka中数据写入HDFS 6 KafkaProducer的使用 7 练习

    1. 解析参数工具类(ParameterTool) 该类提供了从不同数据源读取和解析程序参数的简单实用方法,其解析args时,只能支持单只参数. 用来解析main方法传入参数的工具类 public c ...

  6. Spring Boot对日志的控制

    一.logback日志技术介绍 Spring Boot中使用的日志技术为logback.其与Log4J都出自同一人,性能要优于Log4J,是Log4J的替代者. 在Spring Boot中若要使用lo ...

  7. 【Linux卷管理】LVM原理

    LVM 简介 每个Linux使用者在安装Linux时 都会遇到这样的困境:在为系统分区时,如何精确评估和分配各个硬盘分区的容量,因为系统管理员不但要考虑到当前某个分区需要的容量,还要预见该分区以后可能 ...

  8. Synchronized和Lock接口

    关于synchronized字段,不管该关键字是修饰方法还是修饰同步代码块,synchronzed拿到的都是对象. 当synchronized修饰的是方法时,synchronized所拿到的是调用该方 ...

  9. .net 5 开发跨平台客户端程序

    介绍下.net 跨平台开发服务端程序的过程, .net 5发布已经有段时间了,.net 5根据微软官方的说法将来只有一个.net版本,也就是不在有core之分.从.net5开始整合.net frame ...

  10. Java 多线程的一次整理

    一天没有出过家门,实属无聊,没事瞎写写 1. 基本概念 1.1 多进程和多线程的概念 程序是由指令和数据组成,指令要运行,数据要加载,指令被 CPU 加载运行,数据被加载到内存,指令运行时可由 CPU ...