华为诺亚方舟实验室实习面试

前言

记录一下自己遇到的实习面试经历,希望能对后来人有用。面试总共分为两轮:第一轮是项目面试,主要讲自己做的研究工作以及论文相关情况;第二轮主要是CodingTest测试;
这里主要讲讲自己的CodingTest,其实代码测试的题目和LeetCode差不多,水平差不多,题目可能不一定相同。给了我两个题目,一道是困难,一道是简单的。

一、题目说明

题目1:
A为一个十进制数(以整数为例),k位,k<100。求B使得B为大于A的最小整数,且A各位的和等于B各位的和。

题目2:
给一定数量的信封,带有整数对 (w, h) 分别代表信封宽度和高度。一个信封的宽高均大于另一个信封时可以放下另一个信封。求最大的信封嵌套层数。
样例
给一些信封 [[5,4],[6,4],[6,7],[2,3]] ,最大的信封嵌套层数是 3([2,3] => [5,4] => [6,7])。

二、题目解答

(1)题目一解答:
class Solution:
    # Find the sum of the bits of each number
    def sumAB(self, a):
        n = abs(int(a))
        sum = 0
        while n != 0:
            sum += n % 10
            n //= 10
        return sum

    # Find the B value
    def findB(self,A):
        for i in range(A,10**99):
            if self.sumAB(A) == self.sumAB(i) and i > A:
                break
        return i

if __name__ == '__main__':
    solution = Solution()
    # model.findB()
    A = int(input())  # input A value
    print("The value of B is %d" %(solution.findB(A)))
(2)题目二解答
from typing import List

class Solution:
    def maxEnvelopes(self, envelopes: List[List[int]]) -> int:
        size = len(envelopes)
        #print(envelopes)
        if size < 2:  # if the size of envelopes is smaller than 2, return
            return size
        envelopes.sort(key=lambda x: x[0])  # sort the envelopes according the width
        dp = [1 for _ in range(size)]  #record the number of envelope layers
        #print(dp)
        for i in range(1, size):
            for j in range(i):
                # compare the width and length of envelopes
                if envelopes[j][0] < envelopes[i][0] and envelopes[j][1] < envelopes[i][1]:
                    dp[i] = max(dp[i], dp[j] + 1)
        return max(dp)

if __name__ == '__main__':
    envelopes = [[5,4],[6,4],[6,7],[2,3],[99,100]]  #input the matrix of envelopes
    solution = Solution()
    res = solution.maxEnvelopes(envelopes)
    print("The maximum envelope nesting layer is %d" %(res))
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值