algorithm

What is the fastest correct way to detect that there are no duplicates in a JSON array?

 ̄綄美尐妖づ 提交于 2021-01-27 22:19:21
问题 I need to check if all items are unique in an array of serde_json::Value . Since this type does not implement Hash I came up with the following solution: use serde_json::{json, Value}; use std::collections::HashSet; fn is_unique(items: &[Value]) -> bool { let mut seen = HashSet::with_capacity(items.len()); for item in items.iter() { if !seen.insert(item.to_string()) { return false; } } true } fn main() { let value1 = json!([1, 2]); assert!(is_unique(&value1.as_array().unwrap())); let value2 =

Why is it legit to take the next two commands to fill gaps between paxos events?

落花浮王杯 提交于 2021-01-27 21:25:22
问题 There is a point in Paxos algorithm (http://research.microsoft.com/en-us/um/people/lamport/pubs/paxos-simple.pdf) that I do not understand. It's about how to deal with the gaps, the paper describe two ways as below: The leader, as well as any other server that learns all the commands the leader knows, can now execute commands 1–135. However, it can’t execute commands 138–140, which it also knows, because commands 136 and 137 have yet to be chosen. T he leader could take the next two commands

Find a subset of k most distant point each other

自作多情 提交于 2021-01-27 21:04:31
问题 I have a set of N points (in particular this point are binary string) and for each of them I have a discrete metric (the Hamming distance) such that given two points, i and j, Dij is the distance between the i-th and the j-th point. I want to find a subset of k elements (with k < N of course) such that the distance between this k points is the maximum as possibile. In other words what I want is to find a sort of "border points" that cover the maximum area in the space of the points. If k = 2

Counting number of primes within a given range of long int using C

主宰稳场 提交于 2021-01-27 20:22:05
问题 Well, there are lots of such questions available in SO as well as other forums. However, none of these helped. I wrote a program in "C" to find number of primes within a range. The range i in long int. I am using Sieve of Eratosthenes" algorithm. I am using an array of long ints to store all the numbers from 1 till the limit. I could not think of a better approach to achieve without using an array. The code works fine, till 10000000. But after that, it runs out of memory and exits. Below is

Steinhaus–Johnson–Trotter algorithm with an arbitrary initial state

蓝咒 提交于 2021-01-27 20:21:01
问题 What must be changed in a standard Steinhaus–Johnson–Trotter if values in the initial array are not ordered? For example, my initial array is 312 and I want to generate the following result: 312 321 231 213 123 132 I could introduce an additional array which defines the initial weight of each number, e.g. w[3]=1, w[1]=2, and w[2]=3 and then compare weights instead of values in the algorithm, but is it possible to do without this - I want to apply the algorithm to a problem where this

Binary search tree filter values in a range

这一生的挚爱 提交于 2021-01-27 20:19:22
问题 I have a tree(RBT) consisting of N elements. Let's imagine I have this tree (N = 7): 4 2 6 1 3 5 7 How do I filter values in some range (e.g. print all values between 3 and 6) with better performance than O(N)? Is there any specific algorithm? I'm imagining it something like find position of value 3 [log(N)], somehow continue until you get to the 6 [O(M)]. 回答1: If you have Sedgevick's Algorithms, 4 ed., look at the end of chapter 3.2 on BST's. Also book companion has implementation in Java.

special sorting algorithm and generic signature

南楼画角 提交于 2021-01-27 18:48:09
问题 I have a strong use-case to define my own sorting algorithm, which is faster than the fastest in stl and by exploiting some nice properties of the underlying data I basically can sort in O(n) . So far so good, now the problem is that I would like to offer a generic interface which will fit any type of container e.g. T* or std::vector<T> etc, as long as couple of key concepts apply e.g. there is a valid operator [] available to access the elements of the collection the elements of the

Implementing Delay Queue using one or more standard FIFO Queues [closed]

女生的网名这么多〃 提交于 2021-01-27 18:41:05
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 8 years ago . A delay queue is a queue in which each message has a delay time associated with it and a message can only be taken when its delay has expired. The head of the queue is that message whose delay expired furthest in

How to advance past a deflate byte sequence contained in a byte stream?

半腔热情 提交于 2021-01-27 16:15:45
问题 I have a byte stream that is a concatenation of sections, where each section is composed of a header plus a deflated byte stream. I need to split this byte stream sections but the header only contains information about the data in uncompressed form, no hint about the compressed data length so I can advance properly in the stream and parse the next section. So far the only way I found to advance past the deflated byte sequece is to parse it according to the this specification. From what I

How to advance past a deflate byte sequence contained in a byte stream?

怎甘沉沦 提交于 2021-01-27 16:07:22
问题 I have a byte stream that is a concatenation of sections, where each section is composed of a header plus a deflated byte stream. I need to split this byte stream sections but the header only contains information about the data in uncompressed form, no hint about the compressed data length so I can advance properly in the stream and parse the next section. So far the only way I found to advance past the deflated byte sequece is to parse it according to the this specification. From what I