微软笔试8.13

1. 堆排序,时间复杂度O(nlogn),空间复杂度O(1)
def solution(A):
    # write your code in Python (Python 3.6)
    target = sum(A) / 2
    cur = 0
    cnt = 0
    n = len(A)
    def shift(i, n):
        while i * 2 + 1 < n&nbs***bsp;i * 2 + 2 < n:
            if i * 2 + 2 >= n&nbs***bsp;A[i * 2 + 1] >= A[i * 2 + 2]:
                nxt = i * 2 + 1
            else:
                nxt = i * 2 + 2
            if A[nxt] > A[i]:
                A[i], A[nxt] = A[nxt], A[i]
            i = nxt
    for i in range(n // 2, -1, -1):
        shift(i, n)
    while cur < target:
        A[0] /= 2
        cur += A[0] 
        shift(0, n)
        cnt += 1
    return cnt
2. 类似两数之和,多一个通分的操作 时间复杂度O(n),空间复杂度O(n)
def gdc(a, b):
    mod = 1
    while mod != 0:
        mod = a % b
        a = b
        b = mod
    return a
def solution(X, Y):
    # write your code in Python (Python 3.6)
    from collections import defaultdict
    hash_map = defaultdict(int)
    n = len(X)
    for i in range(n):
        a, b = X[i], Y[i]
        m = gdc(a, b)
        hash_map[(a//m, b//m)] += 1
    ans = 0
    for key, val in hash_map.items():
        a, b = key[0], key[1]
        if a * 2 < b:
            ans += hash_map[(b - a, b)] * val
        elif a * 2 == b:
            ans += val * (val - 1) // 2
    return ans % (10 ** 9 + 7)
3. 外循环遍历Y次,内循环遍历(X + (N - X) / Y)次(滑动窗口),输入范围(X - 1) * Y < N,故时间复杂度O(n),空间复杂度O(1)
def solution(A, X, Y):
    # write your code in Python (Python 3.6)
    n = len(A)
    ans = 2 ** 31 - 1
    for i in range(Y):
        left = i
        curSum = 0
        for j in range(X):
            if left + j * Y >= n:
                return ans
            curSum += A[left + j * Y]
        ans = min(ans, curSum)    
        right = left + X * Y
        while right < n:
            curSum = curSum - A[left] + A[right]
            ans = min(ans, curSum)
            left += Y
            right += Y
    return ans




#秋招#
全部评论
请问有题目吗😂
1 回复 分享
发布于 2022-08-13 14:38
请问考试的时候,题目可以跳转别的页面翻译吗
点赞 回复 分享
发布于 2022-08-25 12:09 黑龙江
我第二题题目没读明白,看成了所有和为1的组合。没想到是特指的两个数的和。
点赞 回复 分享
发布于 2022-08-17 09:59 北京

相关推荐

1.自我介绍2.介绍一下mcp,&nbsp;skills3.了解react哪些状态管理库4.对话是sse还是什么?是用fetch还是EventSource?5.ts中的any&nbsp;和&nbsp;unknown讲一讲6.是直接用组件库的组件还是自己封装了一些别的7.代码输出题1function&nbsp;main()&nbsp;{{var&nbsp;a&nbsp;=&nbsp;1let&nbsp;b&nbsp;=&nbsp;2}console.log(a);console.log(b);}main()console.log(a);8.什么是块级作用域&nbsp;全局作用域&nbsp;函数作用域9.代码输出题2for&nbsp;(var&nbsp;i&nbsp;=&nbsp;0;i&nbsp;&amp;lt;&nbsp;5;i++)&nbsp;{setTimeout(()&nbsp;=&amp;gt;&nbsp;{console.log(i);},&nbsp;100);}10.代码输出题3for&nbsp;(var&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&amp;lt;&nbsp;5;&nbsp;i++){function&nbsp;printText(temp)&nbsp;{setTimeout(()&nbsp;=&amp;gt;&nbsp;{console.log(temp);},&nbsp;100);}printText(i)}11.代码输出题4for(var&nbsp;i&nbsp;=&nbsp;0;i&nbsp;&amp;lt;&nbsp;5;i++){function&nbsp;printText(temp)&nbsp;{var&nbsp;temp&nbsp;=&nbsp;isetTimeout(()&nbsp;=&amp;gt;&nbsp;{console.log(temp);},&nbsp;100);}printText(i)}12.代码输出题5for(var&nbsp;i&nbsp;=&nbsp;0;i&nbsp;&amp;lt;&nbsp;5;i++){function&nbsp;printText(temp)&nbsp;{setTimeout(()&nbsp;=&amp;gt;&nbsp;{var&nbsp;temp&nbsp;=&nbsp;iconsole.log(temp);},&nbsp;100);}printText(i)}13.点击控制台输出题export&nbsp;default&nbsp;function&nbsp;App()&nbsp;{const&nbsp;[count,&nbsp;setCount]&nbsp;=&nbsp;useState(0)console.log('render',count)return&nbsp;(&lt;div&gt;&lt;h1&gt;{count}&lt;/h1&gt;{setCount(count&nbsp;+&nbsp;1)setTimeout(()&nbsp;=&amp;gt;&nbsp;console.log('setTimeout',&nbsp;count),&nbsp;1000)}}&amp;gt;+1&lt;/div&gt;)}//这个组件点击按钮后,控制台的输出顺序和值如下://&nbsp;1.&nbsp;render&nbsp;1&nbsp;(组件重新渲染,&nbsp;count&nbsp;更新为&nbsp;1)//&nbsp;2.&nbsp;setTimeout&nbsp;0&nbsp;(1秒后输出,注意这里是&nbsp;0&nbsp;而不是&nbsp;1)14.算法:给有序数组arr&nbsp;=&nbsp;[-4,&nbsp;-1,&nbsp;0,&nbsp;3,&nbsp;5],返回平方后的排序//&nbsp;有序数组平方后排序const&nbsp;arr&nbsp;=&nbsp;[-4,&nbsp;-1,&nbsp;0,&nbsp;3,&nbsp;5]function&nbsp;solution(arr)&nbsp;{const&nbsp;len&nbsp;=&nbsp;arr.lengthconst&nbsp;result&nbsp;=&nbsp;new&nbsp;Array(len)let&nbsp;left&nbsp;=&nbsp;0let&nbsp;right&nbsp;=&nbsp;len&nbsp;-&nbsp;1let&nbsp;index&nbsp;=&nbsp;len&nbsp;-&nbsp;1while&nbsp;(left&nbsp;&amp;lt;=&nbsp;right)&nbsp;{if&nbsp;(arr[left]&nbsp;*&nbsp;arr[left]&nbsp;&amp;gt;&nbsp;arr[right]&nbsp;*&nbsp;arr[right])&nbsp;{result[index]&nbsp;=&nbsp;arr[left]&nbsp;*&nbsp;arr[left]left++}&nbsp;else&nbsp;{result[index]&nbsp;=&nbsp;arr[right]&nbsp;*&nbsp;arr[right]right--}index--}return&nbsp;result}console.log(solution(arr));15.反问
查看14道真题和解析
点赞 评论 收藏
分享
评论
3
10
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务