data-structures

All possible combination of N numbers to sum X

て烟熏妆下的殇ゞ 提交于 2021-02-08 12:01:18
问题 I have to write a program that given n , target and max , returns all the number combinations of size n that sums to target , where no number can be greater than max Example: target = 3 max = 1 n = 4 Output: [0, 1, 1, 1] [1, 0, 1, 1] [1, 1, 0, 1] [1, 1, 1, 0] It is a very simple example, but there can be a very large set of possible combinations for a more complex case. I'm looking for any algorithmic clue, but a Java implementation would be perfect. 回答1: Here a java version: import java.util

All possible combination of N numbers to sum X

自闭症网瘾萝莉.ら 提交于 2021-02-08 12:01:17
问题 I have to write a program that given n , target and max , returns all the number combinations of size n that sums to target , where no number can be greater than max Example: target = 3 max = 1 n = 4 Output: [0, 1, 1, 1] [1, 0, 1, 1] [1, 1, 0, 1] [1, 1, 1, 0] It is a very simple example, but there can be a very large set of possible combinations for a more complex case. I'm looking for any algorithmic clue, but a Java implementation would be perfect. 回答1: Here a java version: import java.util

Why 'set' data structure is told to be unordered?

痞子三分冷 提交于 2021-02-08 11:55:46
问题 Well, I think it's perfectly ordered, because position of the elements depends on their hash function, thus if that objects are immutable, then after you put them in the set, their position will remain unchanged. And everytime you, say, print your set you'll have exact the same elements order. Sure, you can't predict their position until their hash function is calculated, but anyway. 回答1: Generic / abstract data type The definition of set from Aho, Hopcroft, Ullmann : 'Data Structures and

Which node go in left or right on addition of weight while huffman tree creation

大憨熊 提交于 2021-02-08 10:59:47
问题 I am trying to create a Huffman tree and i am bit confused by reading several links on internet. Some add the greater(in terms of weight) child nodes in left or some on right. So my question: (1)Is it really a matter that where to add the nodes(in left or right) ? (2) May i add node with greater weight in right or lower weight in Left ? Thanks for the help. 回答1: As long as you're consistent, it makes no difference. Either you put all the lower weights on left children and all the higher

Which node go in left or right on addition of weight while huffman tree creation

◇◆丶佛笑我妖孽 提交于 2021-02-08 10:57:29
问题 I am trying to create a Huffman tree and i am bit confused by reading several links on internet. Some add the greater(in terms of weight) child nodes in left or some on right. So my question: (1)Is it really a matter that where to add the nodes(in left or right) ? (2) May i add node with greater weight in right or lower weight in Left ? Thanks for the help. 回答1: As long as you're consistent, it makes no difference. Either you put all the lower weights on left children and all the higher

Most efficient (1-loop) way to use Priority Queue in Parallel Jobs task

萝らか妹 提交于 2021-02-08 10:32:08
问题 I am having a hard time with a data-structure related problem. I've tried quite a lot recently, but I don't know how to proceed. The problem is that I have the right output, but the timing is too slow and I don't pass the automated tests. To solve the problem, I am using a min-heap to implement the priority queue with next free times for the workers -- how could I make it more efficient? Efficiency is critical here. Task description You have a program which is parallelized and uses m

How do I append a value to dict key? (AttributeError: 'str' object has no attribute 'append')

天大地大妈咪最大 提交于 2021-02-08 10:13:58
问题 Say I have a dictionary with one key (and a value): dict = {'key': '500'}. Now I want to add a new value '1000' to the same key. However, dict[key].append('1000') just gives me "AttributeError: 'str' object has no attribute 'append'". If I do dict[key] = '1000' it replaces the previous value. I'm guessing I have to create a list as a value and somehow append that list as the key's value but I'm not sure how I would go about this. Thanks for any help! 回答1: I suggest the usage of a defaultdict

Expression evaluation in java [duplicate]

不羁岁月 提交于 2021-02-08 07:29:34
问题 This question already has answers here : How to evaluate a math expression given in string form? (25 answers) Closed 7 years ago . I am working in java, and I have to evaluate mathematical expressions given as a String. For example: "5*8-9/7+5-8" will result in 35.71. I tried for "java eval library" , but found no help. Please tell me how to resolve this sort of problem. I can evaluate expression like these using data structure stack or queue but i have to consider operator precedence as

Data structure for fast lookup with multiple criteria

烂漫一生 提交于 2021-02-08 07:24:43
问题 I have multiple elements that contain n properties. I need to create a datastructure that will return all the matching elements for a given set of criteria. The lookup needs to be fast. To make it a bit clearer, imagine the following class: class Element { let a : Int? // criterion0 let b : Int? // criterion1 let c : Int? // criterion2 let value : String } Now I populate a dataStructure with 3,000 of such elements: dataStructure.add(Element(a:1,b:3,c:4, value:"val0")) // insert 1

Heapq module implementation

风格不统一 提交于 2021-02-08 06:18:09
问题 I was reading the heapq module source because I reviewed a question on CodeReview and I cannot understand something. In the wikipedia article about heap it says: sift-up: move a node up in the tree, as long as needed; used to restore heap condition after insertion. Called "sift" because node moves up the tree until it reaches the correct level, as in a sieve. sift-down: move a node down in the tree, similar to sift-up; used to restore heap condition after deletion or replacement. But the code