Find the Thief (Facebook Interview)
来源:互联网 发布:mac开启root权限 编辑:程序博客网 时间:2024/06/14 23:47
Suppose there is a thief and n rooms.
We can only any door to check whether the thief is there or not. during the night, the thief can either move left one room or right one room. Given a sequence of open room.
Check whether we can get the thief using the given sequence.
For example: If there are three rooms, given sequence [1, 1], we can definitely find the thief. if thief was in room[1], we can find him right away. But, if he was in room[0]/room[2], we can find him on the second day since he will have to move one room right/left.
1: first thought, backtracking. if there are n rooms, k sequences, the time complexity will be: k*n^k
#include <vector>#include <iostream>using namespace std;void findThief(int rooms, int thiefPos, vector<int>& sequence, int i, bool& flag) { if(thiefPos < 0 || thiefPos >= rooms || i >= sequence.size()) return; if(flag == true) return; if(thiefPos == sequence[i]) { flag = true; } findThief(rooms, thiefPos + 1, sequence, i + 1, flag); findThief(rooms, thiefPos - 1, sequence, i + 1, flag);}bool findThief(int rooms, int thiefPos, vector<int>& sequence) { bool flag = false; findThief(rooms, thiefPos, sequence, 0, flag); if(flag) return true; return false;}int main(void) { vector<int> sequence {1, 1}; cout << findThief(3, 2, sequence) << endl; cout << findThief(3, 0, sequence) << endl; cout << findThief(3, 1, sequence) << endl;}
Further Optimize....
// using dp to cache the intermediate results.bool ThiefSuriveII(int n, vector<int>& seq) { int m = seq.size(); vector< vector<bool> > dp(m, vector<bool>(n, false)); for(int i = 0; i < n; ++i) dp[0][i] = true; dp[0][seq[0]] = false; // dead in the first day. for(int i = 1; i < m; ++i) { for(int j = 0; j < n; ++j) { bool left = j - 1 >= 0 ? dp[i-1][j-1] : false; bool right = j + 1 < n ? dp[i-1][j+1] : false; dp[i][j] = (left || right) && (seq[i] != j); } } for(int i = 0; i < n; ++i) { if(dp[m-1][i]) return true; } return false;}
Further Optimize....
// further optimize... Since we dont need to preverver the last status.bool thiefSuriveIII(int n, vector<int>& seq) { int m = seq.size(); vector<bool> dp(n, true); dp[seq[0]] = false; for(int i = 1; i < m; ++i) { vector<int> nextDp(n, false); for(int j = 0; j < n; ++j) { bool left = j - 1 >= 0 ? dp[j-1] : false; bool right = j + 1 < n ? dp[j+1] : false; nextDp[j] = (left || right) & (seq[i] != j); } dp = nextDp; } for(int i = 0; i < n; ++i) { if(surrive[i]) return true; } return false;}
Talked with huixuan, she promoted to solve it BFS!!! This idea is fantastic.
Think that each day we can only open one door. If the sequence can guarantee to find the thief out, by the end of the sequence, we will have no door left to check. Each day we can cut a branch. Thus, BFS can search the door most efficiently.
#include "header.h"using namespace std;bool findThief(int n, vector<int>& sequence) { // initialize the first day's states. set<int> prev; for(int i = 0; i < n; ++i) prev.insert(i); for(int i = 0; i < sequence.size(); ++i) { set<int> current; auto iter = prev.begin(); while(iter != prev.end()) { int jth = *iter; if(jth != sequence[i]) { if(jth > 0) current.insert(jth - 1); if(jth < n-1) current.insert(jth + 1); } iter++; } swap(prev, current); } return prev.size() == 0;}int main(void) { vector<int> sequence{1, 1}; cout << findThief(3, sequence) << endl;}
- Find the Thief (Facebook Interview)
- The thief
- hdu catch the thief
- <Sicily>Catch the thief
- sicily Catch the thief
- hdu 3469 Catching the Thief
- zoj 2864 Catch the thief
- ZOJ 2864 Catch the thief
- sicily 5144 catch the thief
- Preparing for an Interview (Facebook)
- Kaitou Kid - The Phantom Thief (1)
- interview practice 1 find the second biggest number from a positive array
- Find The First Non Repeated Character In A String : Technical Interview Question
- Facebook Phone Interview: 3, 4 Sum (Easy)
- Surviving the Technical Interview
- Cracking the Coding Interview
- 《Cracking the coding interview》
- Cracking The Coding Interview
- (java)Tomcat项目启动时内存溢出问题
- linux下的lsof的使用及其在取证分析时的应用
- Mac配置Groovy开发环境
- 在Eclipse中运行JAVA代码远程操作HBase的示例
- Kth Smallest Element in a BST
- Find the Thief (Facebook Interview)
- 《java编程思想》学习笔记(二)
- USB OTG原理+ ID 检测原理
- python常用技巧
- pcap文件解析
- 应用RecyclerView实现Gallery相册效果——注意引入recyclerview-v7的版本
- Android之Activity(一):活动简介
- Linux--内核Uevent事件机制 与 Input子系统
- 初次接触zstack