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. 约瑟夫问题如果使用 ...
随机推荐
- Node.js学习之简介
1.简单的说Node.js就是运行在服务端的javaScript: 2.Node.js是一个基于Chrome javaScript运行时建立的一个平台: 3.Node.js是一个事件驱动I/O服务端J ...
- 【Cocos2d-x游戏开发】Cocos2d-x中的数据存储技术
一.引言 数据存储和网络功能可以说是一款游戏中必不可少的功能,如果一款游戏不能保存进度那么它的可玩性必然大打折扣(试想一下,玩家辛辛苦苦玩了一整天的游戏,结果退出时告诉人家不能保存关卡信息,你明天还得 ...
- maven archetype:generate 命令简化项目模板数量
在maven里使用 mvn archetype:generate 来创建项目是十分方便的,但有时也不尽然.在网络不好时,从网络上加载 archetype-catalog.xml文件(http://re ...
- 邮箱、手机号、中文 js跟php正则验证
邮箱正则: jS: var regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/; //验证 if(regEmail.te ...
- 非递归创建二叉树( C++队列 )
非递归按照 层序 创建二叉树,利用 队列(即可先进先出特点)存放已访问的结点元素的地址. 初始化:front=rear= -1: 每储存一个结点元素 rear+1 ,利用 rear%2==0 来使 f ...
- 使用CallableStatement的用法
package Test; import java.sql.*; public class Test7 { public static void main(String[] args) { Conne ...
- #20145205 《Java程序设计》第4周学习总结
教材学习内容总结 1.面对对象中,因为我们需要设计多个模块,但是有不能像C语言中那样进行分块设计,所以我们用父类和子类进行模块的设计,我们在设计一个较大的程序时,在一个项目中建立不同的文件,用关键字e ...
- python 安装nltk,使用(英文分词处理,词干化等)(Green VPN)
安装pip命令之后: sudo pip install -U pyyaml nltk import nltk nltk.download() 等待ing 目前访问不了,故使用Green VPN htt ...
- DB Connection String
SQL: Data Source=APGZDB04;Initial Catalog=ChinaEDI;Persist Security Info=True;User ID=edi_ac;Passwor ...
- JavaScript自学之数组排序
<html> <head> <title>数组排序</title> <script type="text/javascript" ...