📣 独立日限时特惠: VIP通行证立减$68
楼主: guzi
跳转到指定楼层
上一主题 下一主题
收起左侧

Google 2015-10-2 MTV 另求教

🔗
 楼主| guzi 2015-10-13 08:17:48 | 只看该作者
全局:

挂了,今天刚收到的通知。祝大家好运吧~~
回复

使用道具 举报

🔗
bobzhang2004 2015-10-19 09:22:05 | 只看该作者
全局:
bitware 发表于 2015-10-3 16:06
第六道能不能这么做:取1/4,2/4,3/4,4/4这四个数,对每个数,分别找到第一个大于它的数的位置和最后一个 ...

//{1, 2, 3, 4, 5, 6, 6} 这个case可以过吗?
回复

使用道具 举报

🔗
newlxnewlx 2015-10-24 06:02:21 | 只看该作者
全局:
mark.  Thanks a lot !

# 1
# assume it is valid
def get_itinerary(tickets):
    ins = {}
    outs = {}
    cnt = {}
    for s, e in tickets:
        ins[e] = ins.get(e, []) + [s]
        outs[s] = outs.get(s, []) + [e]
        cnt[(s, e)] = cnt.get((s, e), 0) + 1

    start = filter(lambda k : len(outs[k]) - len(ins.get(k, 0)) == 1, outs.keys())[0]
    itinerary = []

    def dfs(k, path):
        path.append(k)
        if len(cnt) == 0:
            itinerary.append(path[:])
        else:
            for n in outs.get(k, []):
                if (k, n) in cnt:
                    dec(k, n)
                    dfs(n, path)
                    inc(k, n)
        path.pop()

    def dec(s, e):
        cnt[(s, e)] -= 1
        if cnt[(s, e)] == 0:
            del cnt[(s, e)]

    def inc(s, e):
        cnt[(s, e)] = cnt.get((s, e), 0) + 1

    dfs(start, [])
    return itinerary

tickets = (('A', 'B'), ('B', 'C'), ('C', 'A'), ('A', 'D'), ('D', 'C'))
print get_itinerary(tickets)
回复

使用道具 举报

🔗
newlxnewlx 2015-10-24 18:42:42 | 只看该作者
全局:
# question 6
import bisect
def find_element_more_than_one_forth(nums):
    return find_element_more_than_half(nums, 0, len(nums) / 2 - 1) or \
            find_element_more_than_half(nums, len(nums) / 2 + 1, len(nums) - 1) or \
             cnt(nums, 0, len(nums) - 1, nums[len(nums) / 2]) > len(nums) / 4

def find_element_more_than_half(nums, low, high):
    return cnt(nums, low, high, nums[(high + low) / 2]) > (high - low + 1) / 2

def cnt(nums, low, high, target):
    return bisect.bisect_right(nums, target, low, high + 1) - bisect.bisect_left(nums, target, low, high + 1)
回复

使用道具 举报

🔗
 楼主| guzi 2015-10-28 10:57:24 | 只看该作者
全局:
newlxnewlx 发表于 2015-10-24 18:42
# question 6
import bisect
def find_element_more_than_one_forth(nums):

LZ用的是Java没这福利 谢谢你的代码,学习了~
回复

使用道具 举报

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

本版积分规则

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