楼主: cjlm007
跳转到指定楼层
上一主题 下一主题
收起左侧

帮朋友发google onsite面经

🔗
Effiel 2016-4-30 11:11:38 | 只看该作者
全局:
bobzhang2004 发表于 2016-2-4 11:43
不太懂c++ code,这个countMap对应的是java中的hashmap? 那vector是array, 如果这样的话这key有点奇怪啊

Java版,楼主的算法,比楼主的长很多,因为JAVA的数组的相等不是比值,

        public int count(int n) {
                Map<List<Integer>, Integer> countMap = new HashMap<>();
                countMap.put(new ArrayList<>(), 1);
                for (int i = 0; i < n * n; i++) {
                        Map<List<Integer>, Integer> newCountMap = new HashMap<>();
                        for (Map.Entry<List<Integer>, Integer> entry : countMap.entrySet()) {
                                List<Integer> currList = entry.getKey();
                                int currCount = entry.getValue();
                                for (int j = 0; j < currList.size(); j++) {
                                        if ((j == 0 || currList.get(j) < currList.get(j - 1)) && currList.get(j) < n) {
                                                List<Integer> newList = new ArrayList<>(currList);
                                                newList.set(j, newList.get(j) + 1);
                                                addToNewMap(newCountMap, currCount, newList);
                                        }
                                }
                                if (currList.size() < n) {
                                        List<Integer> newList = new ArrayList<>(currList);
                                        newList.add(1);
                                        addToNewMap(newCountMap, currCount, newList);
                                }
                        }
                        countMap = newCountMap;
                }
                return countMap.entrySet().iterator().next().getValue();
        }

        private void addToNewMap(Map<List<Integer>, Integer> newCountMap, int currCount, List<Integer> newList) {
                if (newCountMap.containsKey(newList)) {
                        newCountMap.put(newList, newCountMap.get(newList) + currCount);
                } else {
                        newCountMap.put(newList, currCount);
                }
        }

补充内容 (2016-4-30 11:19):
不是楼主, 是163
回复

使用道具 举报

🔗
antiquity 2016-5-8 23:16:57 | 只看该作者
全局:
The following is my JAVA solution, similar to 163'.


  1. import java.util.*;
  2. class Solution{
  3.     static long solve(int n){
  4.         Map<List<Integer>,Long> map=new HashMap<>(), alt=new HashMap<>(), temp;
  5.         List<Integer> stat=new ArrayList<>();
  6.         for(int i=0; i<n; i++) stat.add(0);
  7.         map.put(stat,1l);
  8.         for(int j=1; j<=n*n; j++){
  9.             alt.clear();
  10.             for(Map.Entry<List<Integer>,Long> entry:map.entrySet()){
  11.                 stat=entry.getKey();
  12.                 long count=entry.getValue();
  13.                 for(int i=0; i<n; i++){
  14.                     if((i==0 && stat.get(i)<n) || (i>0 && stat.get(i)<stat.get(i-1))){
  15.                         List<Integer> newStat=new ArrayList<>(stat);
  16.                         newStat.set(i,stat.get(i)+1);
  17.                         Long newC=alt.get(newStat);
  18.                         if(newC==null)
  19.                             alt.put(newStat,count);
  20.                         else
  21.                             alt.put(newStat,newC+count);
  22.                     }
  23.                 }
  24.             }
  25.             temp=alt; alt=map; map=temp;
  26.         }
  27.         System.out.println(map);
  28.         return 1;
  29.     }
  30.     public static void main(String[] args){
  31.         System.out.format("%d: %d\n", 5, solve(5));

  32.     }
  33. }
复制代码

回复

使用道具 举报

🔗
Altynai 2016-5-8 23:42:19 | 只看该作者
全局:
第一题follow up是不是最小费用流?

每个点拆成i->i',流量1(起点流量为2),费用为格子数值的负数
相邻的格子连边i'->j,流量1,费用为0
源点就是左上角的点,汇点就是最右下角的点
答案就是: abs(最小费用) - 原点格子的值 - 终点格子的值(后两个算了两遍,所以要减掉)
回复

使用道具 举报

🔗
pushazhiniao 2016-8-28 09:15:33 | 只看该作者
全局:
基于163给个python解法 存的tuple为key
def waysToFillMatrix(n):
        # store current state and number of ways to get to current state
        # number of filled cells in each row
        dic = {(0,) * n:1}
        for i in range(n * n):
                newDic = {}
                for state in dic:
                        for j in range(n):
                                # row j-1 must have more cells filled than row j
                                if (j == 0 or state[j - 1] > state[j]) and state[j] < n:
                                        newState = state[:j] + (state[j] + 1,) + state[j + 1:]
                                        newDic[newState] = newDic.get(newState, 0) + dic[state]
                dic = newDic
        return dic[dic.keys()[0]]

print waysToFillMatrix(1)
print waysToFillMatrix(2)
print waysToFillMatrix(3)
print waysToFillMatrix(4)
print waysToFillMatrix(5)
print waysToFillMatrix(6)
回复

使用道具 举报

🔗
pushazhiniao 2016-8-28 10:21:01 | 只看该作者
全局:
163的记录状态方法好好用 也写了两个人走grid的那题

def twoPeoplePath(grid):
        m, n = len(grid), len(grid[0])
        # positions of two people and max gain
        dic = {(0, 0): grid[0][0]}
        for i in range(m + n - 2):
                newDic = {}
                for state in dic:
                        a, b = state
                        x1, y1 = a / n, a % n
                        x2, y2 = b / n, b % n
                        for i1, j1 in (x1 + 1, y1), (x1, y1 + 1):
                                if -1 < i1 < m and -1 < j1 < n:
                                        for i2, j2 in (x2 + 1, y2), (x2, y2 + 1):
                                                if -1 < i2 < m and -1 < j2 < n:
                                                        newState = (i1 * n + j1, i2 * n + j2)
                                                        total = dic[state] + grid[i1][j1] + (0 if i1 == i2 and j1 == j2 else grid[i2][j2])
                                                        if newState not in newDic:
                                                                newDic[newState] = total
                                                        else:
                                                                if newDic[newState] < total:
                                                                        newDic[newState] = total
                dic = newDic
        return dic[dic.keys()[0]]
回复

使用道具 举报

🔗
lvvvvv 2016-8-28 10:24:04 | 只看该作者
全局:
163 发表于 2016-1-11 01:51
我觉得我的这个dp应该是work了,结果有701149020种(7亿种,确实很多)。不过是秒出的结果。所以应该是work ...

有点不太明白,  开始一个没填 [0,0,0,0,0] , count =1,
第一行填一个数, 只能填1(最小), [1,0,0,0,0], count=1 也可以理解,
下一个循环, [2,0,0,0,0] [1,1,0,0,0] count 为啥也是 1 啊, 有很多种填法呀。
回复

使用道具 举报

🔗
pushazhiniao 2016-8-29 01:41:10 | 只看该作者
全局:
lvvvvv 发表于 2016-8-28 10:24
有点不太明白,  开始一个没填 [0,0,0,0,0] , count =1,
第一行填一个数, 只能填1(最小), [1,0,0,0 ...

每个hash里的value指的是到当前状态的填法,一个都不填的填法是1
i-1排填的数量必须大于等于i排,你试着手填一下就发现了
回复

使用道具 举报

🔗
lvvvvv 2016-8-29 06:02:48 | 只看该作者
全局:
pushazhiniao 发表于 2016-8-29 01:41
每个hash里的value指的是到当前状态的填法,一个都不填的填法是1
i-1排填的数量必须大于等于i排,你试着 ...

我明白了, i 是当前要往里填的数。

谢谢
回复

使用道具 举报

🔗
南慕伦 2016-8-30 07:02:14 | 只看该作者
全局:
第一题是双调欧几里得回路。
当做两个人同时往下走就好了,状态很好推。状态划分是斜线。
回复

使用道具 举报

🔗
william_gong 2016-9-16 21:36:25 | 只看该作者
全局:
南慕伦 发表于 2016-8-30 07:02
第一题是双调欧几里得回路。
当做两个人同时往下走就好了,状态很好推。状态划分是斜线。

能不能给稍微详细一点的解法呢?多谢!
回复

使用道具 举报

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

本版积分规则

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