查看: 5321| 回复: 22
跳转到指定楼层
上一主题 下一主题
收起左侧

一道Amazon intern面试题有感

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
今天帮一个师弟面下amazon intern, 看以前的经验帖子,觉得amazon intern面试题应该都很简单的,无非就是leetcode的难度,还考一些基本题,所以信心满满。
后来发现出来一道langford pairing的题目
http://en.wikipedia.org/wiki/Langford_pairing
a Langford pairing for n = 3 is given by the sequence 2,3,1,2,1,3.
让你实现这个算法,DFS暴力搜索,其实代码不太难写,只是我开始没听到解法,压根就不知道这个题怎么解,当然他也不可能上来就告诉你这个题是Langford_pairing,
等着你去google 搜答案,感觉amazon intern面试题的难度增加啦,回头网上搜了一个代码,简单改了改发现居然能用Bitset类去写,好强大,下面贴下代码


import java.util.*;
        public class LangfordPairing {
                public static  void langford(int N) {
                        if(N == 0) {
                                System.out.println(" no result ");
                                System.exit(0);
                        }
                        BitSet bs = new BitSet();
                        bs.set(N * 2);
                        int[] a=new int[2 * N];
                        put(bs, N, a);
                        if(bs.nextClearBit(0) >= 0) {
                                System.out.println(" no result ");
                        }
                }
                public  static void put(BitSet bs, int n, int[] arr) {
                       
                        if (n == 0) {
                               
                                System.out.println(Arrays.toString(arr));
                                System.exit(0);
                                //return;
                       
                        }
                        for (int i = -1, L = bs.length() - n - 1;
                                        (i = bs.nextClearBit(i + 1)) < L ;) {


                                final int j = i + n + 1;
                                if (!bs.get(j)) {
                                        arr[i] = n;
                                        arr[j] = n;
                                        bs.flip(i);
                                        bs.flip(j);
                                        put(bs, n - 1, arr);
                                        bs.flip(i);
                                        bs.flip(j);
                                }
                        }
                }
                public static void main(String[] args) {
               
                        //langford(9);
                        langford(4);
                }
}



评分

参与人数 3大米 +48 收起 理由
pure0909 + 15 感谢分享!
weiqitsai + 3 感谢分享!
北美农民 + 30

查看全部评分


上一篇:程序看不懂,求教各位。
下一篇:一道LeetCode算法题求助
推荐
Jonson 2014-10-11 20:57:57 | 只看该作者
全局:
本帖最后由 Jonson 于 2014-10-11 20:59 编辑

Longford Pairing 既然是一种permutation, 自然就是考虑用DFS, 在DFS的过程中,每次尝试加入一个新的数字到solution中时, 都要check是否满足Longford Pairing的feature, 所以这道题的思路也是一种backtracking的想法。注意需要一个List<List<Integer>>保存结果。
  1. public class LongfordSequence {
  2.         public List<List<Integer>> gernerateSequence(int n) {
  3.                 List<List<Integer>> result = new ArrayList<List<Integer>>();
  4.                 int[] count = new int[n + 1];
  5.                 gernerateHelper(result, new ArrayList<Integer>(), count);
  6.                 return result;
  7.         }

  8.         private void gernerateHelper(List<List<Integer>> result, List<Integer> solution, int[] count) {
  9.                 if (solution.size() == 2 * (count.length - 1)) {
  10.                         result.add(new ArrayList<Integer>(solution));
  11.                         return;
  12.                 }

  13.                 for (int i = 1; i <= count.length - 1; i++) {
  14.                         if (count[i] == 0) {
  15.                                 solution.add(i);
  16.                                 count[i] = 1;
  17.                                 gernerateHelper(result, solution, count);
  18.                                 solution.remove(solution.size() - 1);
  19.                                 count[i] = 0;
  20.                         } else if (count[i] == 1) {
  21.                                 int size = solution.size();
  22.                                 if (size - i - 1 >= 0 && solution.get(size - i - 1) == i) {
  23.                                         solution.add(i);
  24.                                         count[i] = 2;
  25.                                         gernerateHelper(result, solution, count);
  26.                                         solution.remove(solution.size() - 1);
  27.                                         count[i] = 1;
  28.                                 }
  29.                         }
  30.                 }
  31.         }
  32. }
复制代码
回复

使用道具 举报

🔗
北美农民 2014-3-1 08:47:21 | 只看该作者
全局:
本帖最后由 北美农民 于 2014-2-28 19:52 编辑

这题完全acceptable嘛, amz想搞个高大上的名字唬人么。bitset也没啥, 自己弄一个a[n/32]的数组bit oper判断就行了。
回复

使用道具 举报

🔗
 楼主| sumingche 2014-3-1 10:28:58 | 只看该作者
全局:
北美农民 发表于 2014-2-28 19:47
这题完全acceptable嘛, amz想搞个高大上的名字唬人么。bitset也没啥, 自己弄一个a[n/32]的数组bit oper判 ...

恩恩,就是个dfs啦,只是开始没看出来是dfs,我在旁边也没听到dfs的思路,只是看到题啦~
回复

使用道具 举报

🔗
shuiguo 2014-3-2 03:43:53 | 只看该作者
全局:
帮面amazon。。。。
回复

使用道具 举报

🔗
austurela 2014-3-2 06:08:32 | 只看该作者
全局:
  1. import java.util.ArrayList;
  2. import java.util.Arrays;


  3. public class LangfordPairing {
  4.         public ArrayList<int[]> langford(int n){
  5.                 ArrayList<int[]> returnVal = new ArrayList<int[]>();
  6.                 int[] record = new int[2 * n];
  7.                 lanfordHelper(returnVal, record, n);
  8.                 return returnVal;
  9.         }
  10.        
  11.         private void lanfordHelper(ArrayList<int[]> returnVal, int[] record, int downNum){
  12.                 // Edge case
  13.                 if(downNum < 0)        return;
  14.                 // Base
  15.                 if(downNum == 0){
  16.                         returnVal.add(Arrays.copyOf(record, record.length));
  17.                         return;
  18.                 }
  19.                 //Normal
  20.                 for(int i = 0; i <= record.length - downNum - 2; i++){
  21.                         if(record[i] == 0 && record[i + downNum + 1] == 0){
  22.                                 record[i] = downNum;
  23.                                 record[i + downNum + 1] = downNum;
  24.                                 lanfordHelper(returnVal, record, downNum - 1);
  25.                                 record[i] = 0;
  26.                                 record[i + downNum + 1] = 0;
  27.                         }
  28.                 }
  29.         }
  30. }
复制代码

回复

使用道具 举报

🔗
austurela 2014-3-2 06:10:32 | 只看该作者
全局:
BTW 帮助面试真的好吗?
回复

使用道具 举报

🔗
 楼主| sumingche 2014-3-2 06:15:52 | 只看该作者
全局:
austurela 发表于 2014-3-1 17:10
BTW 帮助面试真的好吗?

不太好啊,挺掉rp的
回复

使用道具 举报

🔗
austurela 2014-3-2 06:19:06 | 只看该作者
全局:
sumingche 发表于 2014-3-2 06:15
不太好啊,挺掉rp的

那lz怎么还这样呢 万一被发现了以后各公司不就不相信中国人的电面了?
回复

使用道具 举报

🔗
 楼主| sumingche 2014-3-2 06:21:14 | 只看该作者
全局:
austurela 发表于 2014-3-1 17:19
那lz怎么还这样呢 万一被发现了以后各公司不就不相信中国人的电面了?

我向祖国人民检讨~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表