Giter VIP home page Giter VIP logo

Comments (1)

kamyu104 avatar kamyu104 commented on May 9, 2024
  1. There are at most O(n^2) distinct subarrays if all elemtents are distinct odds and k is n, then the worst case of complexity would be at least O(n^2).
  2. Most solutions in your link are O(n^3), which are not really O(n) or O(n^2) as they claimed.
  3. Here I provide a simple and real O(n^2) solution which is extended from 992 Subarrays with K Different Integers as you wished:
# Time:  O(n^2)
# Space: O(t), t is the size of trie

import collections


class Solution(object):
    def distinctSubarraysWithAtMostKOddIntegers(self, A, K):
        def countDistinct(A, left, right, trie):  # Time: O(n), Space: O(t)
            result = 0
            for i in reversed(xrange(left, right+1)):
                if A[i] not in trie:
                    result += 1
                trie = trie[A[i]]
            return result
        
        _trie = lambda: collections.defaultdict(_trie)
        trie = _trie()
        result, left, count = 0, 0, 0
        for right in xrange(len(A)):
            count += A[right]%2
            while count > K:
                count -= A[left]%2
                left += 1
            result += countDistinct(A, left, right, trie)
        return result
  1. Another solution may be like what zed_b and TheSithPadawan said in the link:
    • Find all distinct subarrays by building suffix tree in O(n^2), which could be improved by Ukkonen's Algorithm in O(nlogd), where d is the number of distinct integers
    • Traverse the suffix tree until all paths with at most k odd elements are visited in O(r)
    • Time: O(nlogd + r), r is the number of the result, which is between O(1) ~ O(n^2), thus total time is at best Ω(nlogd), and at worst O(n^2)
    • Space: O(nd)

from leetcode-solutions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.