language-agnostic

Query whether path between two nodes in a tree contains a given value

六眼飞鱼酱① 提交于 2020-01-24 02:03:25
问题 Suppose a tree has N nodes connected by N-1 edges, where 1 < N < 10^5. The nodes are numbered from 1 to N, and each node holds a value, which is also number in the range 1 to N; the values are unrelated to the node numbers, and are not necessarily distinct. The tree is not necessarily balanced. Because it is a tree, we know there is only one path between any two nodes. Then, we are given M queries, where 1 < M < 10^5. Each query gives two nodes, A and B, and a value T. For each query we have

How to correctly diff trees (that is, nested lists of strings)?

余生颓废 提交于 2020-01-23 22:07:46
问题 I'm working in an online editor for a datatype that consists of nested lists of strings. Note that traffic can get unbearable if I am going to transfer the entire structure every time a single value is changed. So, in order to reduce traffic, I've thought in applying a diff tool. Problem is: how do I find and report the diff of two trees? For example: ["ah","bh",["ha","he",["li","no","pz"],"ka",["kat","xe"]],"po","xi"] -> ["ah","bh",["ha","he",["li","no","pz"],"ka",["rag","xe"]],"po","xi"]

Are “65k” and “65KB” the same?

你离开我真会死。 提交于 2020-01-23 07:35:10
问题 Are "65k" and "65KB" the same? 回答1: From xkcd: 回答2: 65KB normally means 66560 bytes. 65k means 65000, and says nothing about what it is 65000 of. If someone says 65k bytes, they might means 65KB...but they're mispeaking if so. Some people argue for the use of KiB to mean 66560 bytes, since k means 1000 in the metric system. Everyone ignores them, though. Note: a lowercase b would mean bit, rather than bytes. 8Kb = 1KB. When talking about transmission rates, bits are usually used. Edit: As

Print a singly-linked list backwards, in constant space and linear time

强颜欢笑 提交于 2020-01-22 14:07:15
问题 I heard an interview question: "Print a singly-linked list backwards, in constant space and linear time." My solution was to reverse the linkedlist in place and then print it like that. Is there another solution that is nondestructive? 回答1: If you reverse it again after printing it will no longer be destructive, since the original order is restored. 回答2: You've already figured out most of the answer: reverse the linked list in place, and traverse the list back to the beginning to print it. To

Print a singly-linked list backwards, in constant space and linear time

北战南征 提交于 2020-01-22 14:07:08
问题 I heard an interview question: "Print a singly-linked list backwards, in constant space and linear time." My solution was to reverse the linkedlist in place and then print it like that. Is there another solution that is nondestructive? 回答1: If you reverse it again after printing it will no longer be destructive, since the original order is restored. 回答2: You've already figured out most of the answer: reverse the linked list in place, and traverse the list back to the beginning to print it. To

How to determine path from noisy X, Y data

心不动则不痛 提交于 2020-01-22 11:46:33
问题 I have an unsorted list of noisy X, Y points. They do, however, form a path through the world. I would like an algorithm to draw an approximation of this data using line segments. This is similar to how you would use a line -fitting algorithm to pick an approximation of linear data. My problem is only harder because the path bends and winds around the world. alt text http://www.praeclarum.org/so/pathfinder.png Does anyone know of any standard / robust / easy to comprehend algorithms to

Best data structure for an immutable persistent 3D grid

大憨熊 提交于 2020-01-22 09:42:06
问题 I'm experimenting with writing a game in a functional programming style, which implies representing the game state with a purely functional, immutable data structures. One of the most important data structures would be a 3D grid representing the world, where objects can be stored at any [x,y,z] grid location. The properties I want for this data structure are: Immutable Fast persistent updates - i.e. creation of a new version of the entire grid with small changes is cheap and achieved through

Best data structure for an immutable persistent 3D grid

别等时光非礼了梦想. 提交于 2020-01-22 09:41:27
问题 I'm experimenting with writing a game in a functional programming style, which implies representing the game state with a purely functional, immutable data structures. One of the most important data structures would be a 3D grid representing the world, where objects can be stored at any [x,y,z] grid location. The properties I want for this data structure are: Immutable Fast persistent updates - i.e. creation of a new version of the entire grid with small changes is cheap and achieved through

How to store a large directed unweighted graph with billions of nodes and vertices

删除回忆录丶 提交于 2020-01-22 06:02:48
问题 The graph size is in the billions of nodes, and tens of billions of vertices. It will store webpages urls, and links between webpages and it will be used for testing ranking algorithms. Any language is fine but java is prefered. Solutions i found so far: neo4j storing in sorted flat files Yes, i have already read Best Way to Store/Access a Directed Graph. Update The data can be distributed on multiple computers and does not need to be fully in-memory. 回答1: Depending on your implementation,

How to store a large directed unweighted graph with billions of nodes and vertices

蓝咒 提交于 2020-01-22 06:01:16
问题 The graph size is in the billions of nodes, and tens of billions of vertices. It will store webpages urls, and links between webpages and it will be used for testing ranking algorithms. Any language is fine but java is prefered. Solutions i found so far: neo4j storing in sorted flat files Yes, i have already read Best Way to Store/Access a Directed Graph. Update The data can be distributed on multiple computers and does not need to be fully in-memory. 回答1: Depending on your implementation,