algorithm

Red-Black tree: Split/Concatenate in log(n) time

我的未来我决定 提交于 2021-02-19 07:38:59
问题 According to Ron Wein your able to do split and concatenation of red-black tree's in O(log(n)) time. See his artikle: Efficient Implementation of Red-Black Trees with Split and Catenate Operations However I'm still not convinced that the running time of split really is true. The idea is that split uses worst-case log(n) concatenatations. These concat's is done fast as we can find the node, p, by remembering the p, from last concatenate. The problem is that concatenation starts the fix-up

Red-Black tree: Split/Concatenate in log(n) time

安稳与你 提交于 2021-02-19 07:38:30
问题 According to Ron Wein your able to do split and concatenation of red-black tree's in O(log(n)) time. See his artikle: Efficient Implementation of Red-Black Trees with Split and Catenate Operations However I'm still not convinced that the running time of split really is true. The idea is that split uses worst-case log(n) concatenatations. These concat's is done fast as we can find the node, p, by remembering the p, from last concatenate. The problem is that concatenation starts the fix-up

Finding coordinates of Koch Curve

时间秒杀一切 提交于 2021-02-19 07:37:40
问题 Sorry for my language since English is my second language. I am trying to convert a straight line into a fractal known as Koch curve. The 2 points of the straight line are given and then I need to create the Koch curve where I divide the line to 3 segments and then make the second segment an equilateral triangle. See http://www.tgmdev.be/curvevonkoch.php. So far we convert the straight line to 4 equally segments, and I need to figure out all the coordinates of the Koch curve. I have thought

Convert BFS code for a graph into a DFS code

*爱你&永不变心* 提交于 2021-02-19 07:19:59
问题 I'm sorry if this question sounds ambiguous but I was asked this in an interview. Write a program for BFS in a graph/tree. I wrote the popular code using a queue. Now he asked me to convert it to a DFS code by modifying just one line of the BFS code I had just written. The only answer I could think of was to use a stack for DFS. Then I implemented the stack using 2 queues. So in the end my answer was: use 1 queue for BFS. for DFS use 2 queues instead. He did not give me any feedback . Wasn't

Convert BFS code for a graph into a DFS code

半城伤御伤魂 提交于 2021-02-19 07:18:25
问题 I'm sorry if this question sounds ambiguous but I was asked this in an interview. Write a program for BFS in a graph/tree. I wrote the popular code using a queue. Now he asked me to convert it to a DFS code by modifying just one line of the BFS code I had just written. The only answer I could think of was to use a stack for DFS. Then I implemented the stack using 2 queues. So in the end my answer was: use 1 queue for BFS. for DFS use 2 queues instead. He did not give me any feedback . Wasn't

Fast bit permutation

青春壹個敷衍的年華 提交于 2021-02-19 06:51:38
问题 I need to store and apply permutations to 16-bit integers. The best solution I came up with is to store permutation as 64-bit integer where each 4 bits correspond to the new position of i-th bit, the application would look like: int16 permute(int16 bits, int64 perm) { int16 result = 0; for(int i = 0; i < 16; ++i) result |= ((bits >> i) & 1) * (1 << int( (perm >> (i*4))&0xf )); return result; } is there a faster way to do this? Thank you. 回答1: There are alternatives. Any permutation can be

Fast bit permutation

ⅰ亾dé卋堺 提交于 2021-02-19 06:51:11
问题 I need to store and apply permutations to 16-bit integers. The best solution I came up with is to store permutation as 64-bit integer where each 4 bits correspond to the new position of i-th bit, the application would look like: int16 permute(int16 bits, int64 perm) { int16 result = 0; for(int i = 0; i < 16; ++i) result |= ((bits >> i) & 1) * (1 << int( (perm >> (i*4))&0xf )); return result; } is there a faster way to do this? Thank you. 回答1: There are alternatives. Any permutation can be

python too many self in class

丶灬走出姿态 提交于 2021-02-19 06:19:06
问题 I'm learning Python OOP and trying to convert a Java class to a Python class See page 15 in this PDF for Java code google doc link class QuickFindUF: """docstring for QuickFindUF""" def __init__(self, n): self.id = [] for e in range(n): self.id.append(e) def connected(self,p,q): return self.id[p]==self.id[q] def union(self,p,q): self.pid = self.id[p] self.qid = self.id[q] for i in range(len(self.id)): if(self.id[i]==self.pid): self.id[i]=self.qid quf = QuickFindUF(9) quf.union(3,4) print quf

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

╄→гoц情女王★ 提交于 2021-02-19 06:14:48
问题 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),

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

自闭症网瘾萝莉.ら 提交于 2021-02-19 06:13:19
问题 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),