josephus

Take every k-th element from the (1 .. n) natural numbers series

六月ゝ 毕业季﹏ 提交于 2019-11-29 10:28:12
For example, we have series 1, 2, 3, 4, 5. We take every 3 element => 3, 1, 5, 2, 4 (chosen element shouldn't remain, we can take while series is not empty). Naive implementation by circle doubly linked list is not good idea cause of performance. Can you give me an advice which data structures and algorithms are more applicable? Build a complete binary tree containing the numbers 1 to n, e.g. for n=15 that would be: In each branch, store the number of nodes to the left of it; this will allow us to quickly find the i-th node. (You'll see that this tree has a very predictable structure and

Take every k-th element from the (1 .. n) natural numbers series

倖福魔咒の 提交于 2019-11-28 03:42:35
问题 For example, we have series 1, 2, 3, 4, 5. We take every 3 element => 3, 1, 5, 2, 4 (chosen element shouldn't remain, we can take while series is not empty). Naive implementation by circle doubly linked list is not good idea cause of performance. Can you give me an advice which data structures and algorithms are more applicable? 回答1: Build a complete binary tree containing the numbers 1 to n, e.g. for n=15 that would be: In each branch, store the number of nodes to the left of it; this will