Last active
December 1, 2020 14:41
-
-
Save hoffa/c2964aeae74342f708c169d15b68c06d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CQueue: | |
def __init__(self, k=0.5): | |
self.q = [] | |
self.m = 0 | |
self.k = k | |
def enqueue(self, v): | |
self.q.append(v) | |
def dequeue(self): | |
if not self.q: | |
return None | |
V = self.k * self.q[0] | |
for i, v in enumerate(self.q[1:]): | |
if self.m + v < V: | |
self.m += v | |
return self.q.pop(i + 1) | |
self.m = 0 | |
return self.q.pop(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[strike]Do you think this can be O(logn) push and O(logn) pop? Some complex data structures and functions are not implemented.[/strike]
Update: it's still O(n) push, and O(1) pop. Simplified my code.