Bloomberg面经准备: Josephus problem
Given a circular single linked list.Write a program that deletes every kth node until only one node is left.
After kth node is deleted, start the procedure from (k+1)th node.
e.g.list is 1->2->3->4->5->1
k=3
1. You are at 1, delete 3.
List is: 1->2->4->5->1
2. You are at 4, delete 1
List is: 2->4->5->2
3. You are at 2,delete 5
List is: 2->4->2
4. You are at 2, delete 2
List is: 4
Return 4. How efficient you can do it?
Solution 1: Math
Let f(n,k) denote the position of the survivor. After the kth person is killed, we're left with a circle of n-1, and we start the next count with the person whose number in the original problem was (k mod n)+1. The position of the survivor in the remaining circle would be f(n-1,k), if we start counting at 1; shifting this to account for the fact that we're starting at (k mod n)+1 yields the recurrence
f(n, k) = ((f(n-1, k) + k - 1) mod n) + 1,
f(1, k) = 1
这样理解:
新数组len = n-1, 其中1th num在原数组的位置是 (k mod n)+1, 也就是 (1 + (k-1))mod n+ 1
2nd num 在原数组位置是 (2 + (k-1))mod n + 1
3rd num 在原数组位置是 (3 + (k-1))mod n + 1
After the first person (kth from begining) is killed, n-1 persons are left. So we call josephus(n – 1, k) to get the position with n-1 persons. But the position returned by josephus(n – 1, k) will consider the position starting from k%n + 1. So, we must make adjustments to the position returned by josephus(n – 1, k).
Following is simple recursive implementation of the Josephus problem. The implementation simply follows the recursive structure mentioned above.
Time Complexity, O(N), DP
#include <stdio.h> int josephus(int n, int k)
{
if (n == 1)
return 1;
else
/* The position returned by josephus(n - 1, k) is adjusted because the
recursive call josephus(n - 1, k) considers the original position
k%n + 1 as position 1 */
return (josephus(n - 1, k) + k-1) % n + 1;
} // Driver Program to test above function
int main()
{
int n = 14;
int k = 2;
printf("The chosen place is %d", josephus(n, k));
return 0;
}
Solution 2: Simulate the process
Singly LinkedList
先扫一遍,确定有环,并且找到head上一跳节点,然后loop直到head.next == head
package bloomberg;
public class Josephus {
public class ListNode {
ListNode next;
int val;
public ListNode(int val) {
this.val = val;
this.next = null;
}
}
public int survive(ListNode head, int k) {
if (head == null) return -1;
ListNode prev = head;
while (prev!=null && prev.next!=head) {
prev = prev.next;
}
if (prev == null) return -1; //not a loop
//now prev is the previous node of head
int count = 1;
while (head.next != head) {
if (count == k) {
prev.next = head.next;
head = head.next;
count = 1;
}
else {
head = head.next;
prev = prev.next;
count++;
}
}
return head.val;
}
public static void main(String[] args) {
Josephus sol = new Josephus();
ListNode node1 = sol.new ListNode(1);
ListNode node2 = sol.new ListNode(2);
ListNode node3 = sol.new ListNode(3);
ListNode node4 = sol.new ListNode(4);
ListNode node5 = sol.new ListNode(5);
ListNode node6 = sol.new ListNode(6);
node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
node5.next = node6;
node6.next = node1;
int res = sol.survive(node1, 2);
System.out.println(res);
}
}
Doubly LinkedList
可以一边loop一边确定是不是有环,while loop结束条件是 head.next != null && head.next != head, 如果出现head.next == null说明无环, 如果
head.next == head说明仅剩一个元素,正常结束
Bloomberg面经准备: Josephus problem的更多相关文章
- josephus Problem 中级(使用数组模拟链表,提升效率)
问题描写叙述: 在<josephus Problem 0基础(使用数组)>中.我们提出了一种最简单直接的解决方式. 可是,细致审视代码之后.发现此种方案的效率并不高,详细体如今.当有人出局 ...
- 约瑟夫问题(Josephus Problem)的两种快速递归算法
博文链接:http://haoyuanliu.github.io/2016/04/18/Josephus/ 对,我是来骗访问量的!O(∩_∩)O~~ 约瑟夫问题(Josephus Problem)也称 ...
- Josephus Problem的详细算法及其Python、Java实现
笔者昨天看电视,偶尔看到一集讲述古罗马人与犹太人的战争--马萨达战争,深为震撼,有兴趣的同学可以移步:http://finance.ifeng.com/a/20170627/15491157_0. ...
- 算法Sedgewick第四版-第1章基础-017一约瑟夫问题(Josephus Problem)
/************************************************************************* * * Josephus problem * * ...
- 约瑟夫(环)问题(Josephus problem)
问题描述:皇帝决定找出全国中最幸运的一个人,于是从全国选拔出 n 个很幸运的人,让这 n 个人围着圆桌进餐,可是怎么选择出其中最幸运的一个人呢?皇帝决定:从其中一个人从 1 开始报数,按顺序数到第 k ...
- LightOJ - 1179 Josephus Problem(约瑟夫环)
题目链接:https://vjudge.net/contest/28079#problem/G 题目大意:约瑟夫环问题,给你n和k(分别代表总人数和每次要数到k),求最后一个人的位置. 解题思路:因为 ...
- Java-约瑟夫问题(Josephus Problem)
题目: 据说着名犹太历史学家 Josephus有过以下的故事:在罗马人占领乔塔帕特后,39 个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人到,于是决定了一个自杀方 ...
- Josephus problem(约瑟夫问题,丢手绢问题)
约瑟夫问题 约瑟夫环问题是一个数学应用题:已知n个人(以编号1,2,3.....,n)围坐在一张圆桌的周围.从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又出列 ...
- 谁能笑到最后,约瑟夫环-Josephus问题求解
一. 简述Josephus问题 N个人站成一环,从1号开始,用刀将环中后面一个人“消灭“”掉,之后再将刀递给下一个人,这样依次处理,最后留下一个幸存者. 二. 求解方法 1. 约瑟夫问题如果使用 ...
随机推荐
- BestCoder 2nd Anniversary
A题 Oracle http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=703&pid=1001 大数相加: ...
- NoSQL-Redis【2】-HDEL给我的一个惊喜
用HDEL删除Hash最后一个域的时候,Redis奇迹般的帮我清除了Key,真是考虑周到啊! 127.0.0.1:6673[2]> HMSET USER:Zhangshan Name 'Zhan ...
- Spatial Transformer Networks(空间变换神经网络)
Reference:Spatial Transformer Networks [Google.DeepMind]Reference:[Theano源码,基于Lasagne] 闲扯:大数据不如小数据 这 ...
- Linux CentOS中使用SQL*Plus启动和关闭数据库
启动和关闭数据库的常用工具有三个 一.SQL*Plus 在SQL*Plus环境中,用户以SYSDBA身份连接到Oracle后,可以通过命令行方式启动或关闭数据库. 二.OEM(企业管理器) 利用OEM ...
- 一些简单编程练习题P【持续更新】
Q1.写程序将“Hello World”打印到屏幕. A1. public class Test { public static void main(String[] args) { System.o ...
- jenkins,dns错误log过大
http://stackoverflow.com/questions/31719756/how-to-stop-jenkins-log-from-becoming-huge Recently my j ...
- mount windows-linux文件共享
. (2)在linux下访问windows共享: smbclient -L 192.168.2.12 -U admin //查看共享了那些目录,由此知道主机名为XIAOXING-PC smbcli ...
- 一般处理程序如何获取session值
1.要在一般处理程序中获取其他页面的session值,需要引用名空间: using System.Web.SessionState; 2.然后继承一个接口:IRequiresSessionState ...
- Oracel EBS - Response
Oracle ERP Response:
- 在MVC架构中使用CodeSmith生成NHibernate映射对象和实体类
第一步:找到生成模板,如下图 第二步:配置数据库连接(如下图),然后右击第一步找到的模板,点击Excute 第三步:执行操做(如下图) 第四步: 找到之前配置生成的文件夹,找到如下文件(图中标记的文件 ...