zoukankan      html  css  js  c++  java
  • [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], ..., 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

    The O(N^2) brute force solution will cause TLE. So we need to optimize this somehow. If we try to count each A[i]'s contribution as the last element of a subarray and if we assume we can acheive this step efficiently, then we can solve this problem. In order to do this, we need to keep all the unique numbers contributed by the previous element A[i - 1] as the last subarray element. Why? Because we are only interested with continuous subarray.  If the contribution count of A[i - 1] is in order of O(N), we'll hit an dead end here. But is it? Since we are doing OR bitwise operation, if we consider A[i - 1] as the last subarray element and expand from right to left, then we'll have a strictly increasing sequence of unique numbers. Each number will have at least 1 more set bit that its predecessor in this sequence. There are only 32 bits in total, so this sequence can not be bigger than 32. Ahh haa! 

    Now the solution is pretty clear: 

    1. Keep a set of unique values seen so far, call it unique;

    2. Keep another set that stores all the unique numbers contributed by the previous element being the last subarray element, call it prev.

    3. For each new element, use the set in step2 to compute all the all the unique numbers contributed by the current element being the last subarray element, call it curr.

    4. add curr to unique and set prev to curr.

    class Solution {
        public int subarrayBitwiseORs(int[] A) {
            Set<Integer> unique = new HashSet<>();
            Set<Integer> prev = new HashSet<>();
            unique.add(A[0]);
            prev.add(A[0]);
            for(int i = 1; i < A.length; i++) {
                Set<Integer> curr = new HashSet<>();
                for(int v : prev) {
                    curr.add(v | A[i]);
                }
                curr.add(A[i]);
                unique.addAll(curr);
                prev = curr;
            }
            return unique.size();
        }
    }
  • 相关阅读:
    如何在intellj Idea中给新建的项目添加jar包?
    sell
    3D立体方块旋转图册
    npm run eject 命令后出现This git repository has untracked files or uncommitted changes错误
    video标签使用积累+背景视频+遇到问题(视频无法显示,不能自动播放,video自适应div,控件隐藏)
    webpack——react
    webpack——bable-loader,core,preset,编译es6
    webpack——打包JS
    简单的前端上传图片代码
    node——文件写入,文件读取
  • 原文地址:https://www.cnblogs.com/lz87/p/14088412.html
Copyright © 2011-2022 走看看