Comments (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).
- Most solutions in your link are O(n^3), which are not really O(n) or O(n^2) as they claimed.
- 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
- 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)
- Syntax Error: xrange in case of range HOT 1
- runtime error for single number iii HOT 1
- for statistics from large sample HOT 1
- [Question] 0424 Longest repeating character replacement: Space complexity HOT 2
- How to explain totalCount, validCountInLessLength, validCountInFullLength in confusing-number-ii.py? HOT 3
- What happened to your old Leetcode repository HOT 2
- longest uncommon subsequence ii cpp HOT 1
- around 600-800 files truncated means they are lost? HOT 4
- Can someone explain what this line of code means? TIA! HOT 2
- guess-number-higher-or-lower-ii runtime error HOT 1
- Leetcode 522 longest uncommon subsequence ii Runtime error HOT 2
- Shouldn't this be p[0] -= times? HOT 2
- Leet Code - One Per Day
- 336 palindrome pairs Time limit exceed(C++) HOT 1
- 1558 the [0] case could not passed HOT 1
- Explanation for 1358 number of substrings containing all characters HOT 1
- Line 42 if is wrong HOT 2
- Solutions for weekly contests held last week? HOT 1
- Leetcode 2511 - Fix xrange HOT 1
- Leetcode 232 update HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from leetcode-solutions.