algorithm

Quickly find subset of list of lists with greatest total distinct elements

安稳与你 提交于 2021-02-19 06:12:05
问题 Given a list of lists of tuples, I would like to find the subset of lists which maximize the number of distinct integer values without any integer being repeated. The list looks something like this: x = [ [(1,2,3), (8,9,10), (15,16)], [(2,3), (10,11)], [(9,10,11), (17,18,19), (20,21,22)], [(4,5), (11,12,13), (18,19,20)] ] The internal tuples are always sequential --> (1,2,3) or (15,16), but they may be of any length. In this case, the expected return would be: maximized_list = [ [(1, 2, 3),

Interpolating missing contour lines between existing contour lines

不问归期 提交于 2021-02-19 05:41:47
问题 Contour lines (aka isolines) are curves that trace constant values across a 2D scalar field. For example, in a geographical map you might have contour lines to illustrate the elevation of the terrain by showing where the elevation is constant. In this case, let's store contour lines as lists of points on the map. Suppose you have map that has several contour lines at known elevations, and otherwise you know nothing about the elevations of the map. What algorithm would you use to fill in

Knapsack 0-1 path reconstruction (which items to take) [duplicate]

情到浓时终转凉″ 提交于 2021-02-19 05:37:20
问题 This question already has answers here : How to find which elements are in the bag, using Knapsack Algorithm [and not only the bag's value]? (4 answers) Closed 6 days ago . I know how to solve knapsack 0-1 problem with dynamic programming approach, but I am having troubles figuring out which items to take without compromising the complexity of O(N * C) (N items, C capacity). Any ideas (I would prefer a bottom-up approach)? 回答1: Suppose, right now you're storing results in array bool[] a ,

Why naive primality test algorithm is not polynomial

余生颓废 提交于 2021-02-19 05:34:37
问题 I would like to understand why the following naive primality test algorithm is not polynomial. IsPrime (n: an integer) Begin For i=2 to n-1 do If (n % i == 0) then return (no) EndIf EndFor return (yes) End This algorithm is said to be exponential in the size of the input n . Why is it true? And why the following sorting test algorithm is said polynomial and not exponential? IsSorted (T[n]: an array of n integer) Begin For i = 1 to n-1 do If (T[i] > T[i+1]) then return (no) EndIf EndFor return

Why n bitwise and -n always return the right most bit (last bit)

依然范特西╮ 提交于 2021-02-19 05:26:36
问题 Here is the python code snippet: 1 & -1 # 1 2 & -2 # 2 3 & -3 # 1 ... It seems any n & -n always return right most (last) bit, I don't really know why. Can someone help me to understand this? 回答1: It's due to the way that negative numbers are represented in binary, which is called two's complement representation. To create the two's complement of some number n (in other words, to create the representation of -n): Invert all the bits Add 1 So in other words, when you write 1 & -1 it really

Why n bitwise and -n always return the right most bit (last bit)

南笙酒味 提交于 2021-02-19 05:26:05
问题 Here is the python code snippet: 1 & -1 # 1 2 & -2 # 2 3 & -3 # 1 ... It seems any n & -n always return right most (last) bit, I don't really know why. Can someone help me to understand this? 回答1: It's due to the way that negative numbers are represented in binary, which is called two's complement representation. To create the two's complement of some number n (in other words, to create the representation of -n): Invert all the bits Add 1 So in other words, when you write 1 & -1 it really

(java) - Hash function to distribute Strings uniformly in a given range?

断了今生、忘了曾经 提交于 2021-02-19 05:22:09
问题 So ... I'm looking for a hash function that -- assuming no input skew -- will distribute nonempty Strings of (up to) 16 bytes "reasonably uniformly" onto a range [0..n] where n is user input but does not change over time. And I should be able to argue why the function should provide that "resonably uniform" distribution. In the end, all I need is a Java implementation of the hash function for use in a server and a reason "why" this hash function is suitable. So I'm looking less for "perfect

(java) - Hash function to distribute Strings uniformly in a given range?

半城伤御伤魂 提交于 2021-02-19 05:22:03
问题 So ... I'm looking for a hash function that -- assuming no input skew -- will distribute nonempty Strings of (up to) 16 bytes "reasonably uniformly" onto a range [0..n] where n is user input but does not change over time. And I should be able to argue why the function should provide that "resonably uniform" distribution. In the end, all I need is a Java implementation of the hash function for use in a server and a reason "why" this hash function is suitable. So I'm looking less for "perfect

Benefit of a sentinel node in a red black tree?

半城伤御伤魂 提交于 2021-02-19 04:27:06
问题 I created a doubly-linked list, and the benefits of a sentinel node were clear - no null checks or special cases at list boundaries. Now I'm writing a red black tree, and trying to figure out if there is any benefit to such a concept. My implementation is based on the last two functions in this article (top down insertion/deletion). The author uses a "dummy tree root" or "head" to avoid special cases at the root for his insertion/deletion algorithms. The author's head node is scoped to the

Point inside an irregular shape

余生长醉 提交于 2021-02-19 04:22:07
问题 I'm in no way a professional programmer so, pls don't expect a sophisticated approach or language here. I'll however appreciate your advice and recommendations to materialize an algorithm which, at a later stage, I could programmatically add to my project... Here is the problem: Imagine an arbitrary point (Point X) in space with the following properties: has coordinates lies on a 2D-surface is stationary belongs to a single area (boundary coordinates of which are also known) at any given time