Interview: 2022 Spring Recruitment Internship Meituan Interview Questions

I did a set of questions for Meituan yesterday, and recorded the questions. Share it, and write out your own ideas and process. Of course, there should be mistakes. If there are mistakes, you can comment directly to me. Or if you have your own ideas, I hope you can share them with me and learn from them.

Problem 1: Sum of Hamming distances

Xiaomei has two 01 skewers s, t. She wants to find all lengths of sand equal to ∣ s ∣ |s|ts sum of Hamming distances of substrings (contiguous). That is, she wants to know the Hamming distanceHam ( ti , s ) \text{Ham}(t_i , s)Ham(ti,s ) , whereti t_itifinger number iiThe length starting from i characters is ∣ s ∣ |s|s substring. For equal-length 01 stringsa, b, the definition of the Hamming distance between them is:
Ham ( a , b ) = ∑ i = 1 ∣ a ∣ ∣ ai − bi ∣ \text{Ham}(a, b) = \sum_{i =1}^{|a|}|a_i-b_i|Ham(a,b)=i=1aaibi

The Hamming distance between two integers refers to the number of positions in which the corresponding binary bits of the two numbers differ.

Input: For each set of data, it contains two rows of data, the first row is s, the second row is t; 1 ≤ ∣ s ∣ ≤ ∣ t ∣ ≤ 50000 1\le|s|\le|t|\le500001st50000.

Output: Output an integer representing the sum of the Hamming distances

样例输入:
01
00111

样例输出:
3

My own thoughts: hash table

  • First use a sliding window with a length of to straverse the entire string t;
  • Use a hash table, the key is the substring in the sliding window, the value is the number of times the substring tappears in , traverse the entire ttt string;
  • sCalculate the Hamming distance between each key in the hash table and the number of occurrences, and add them up to get the result;
def distance(t, s):  # 汉明距离
    n = len(s)
    ans = 0
    for i in range(n):
        if t[i] != s[i]:
            ans += 1
    return ans

while True:
    try:
        s = input()
        t = input()
        ns, nt = len(s), len(t)  # 获得两个字符串的长度
        cntHash = dict()   # 记录不同类型子串的数量的哈希表
        l, r = 0, ns  # 滑动窗口去记录所有的子串数量
        while r <= nt:
            subStr = t[l:r]  # 获得当前子串
            if subStr not in cntHash:
                cntHash[subStr] = 1
            else:  # 如果子串在 哈希表 中
                cntHash[subStr] += 1
            l += 1  # 窗口往后移一格
            r += 1
        distSum = 0  # 汉明距离的和
        # 计算子串之间汉明距离之和
        for ti in cntHash.keys():
            distSum += cntHash[ti] * distance(ti, s)
        print(distSum)
    except: break

Question 2: Diamond Diagram

A rhombus is a very graceful figure: its opposite sides are symmetrical, and its four sides are equal in length. For example, a square is a special kind of rhombus. For graph theory, a diamond graph refers to a ring formed by an undirected graph, satisfying that the number of points is greater than or equal to 4, in which four different points can be found, so that the distances of , , a,b,c,d, a => bare b => call c => dequal d => a.

Xiaomei just finished learning graph theory. She casually created a graph with npoints mand undirected edges (without multiple edges and self-loops, guaranteed connectivity, that is, any two points are reachable to each other), and she wanted to know if this graph is a rhombus graph. (The default distance between two points is 1)

Input : a positive integer in the first line T, indicating that there are Tgroups of data.

For each set of data, two positive integers in the first line n, mrepresent the number of points and edges of the undirected graph;

The second line mis a positive integer u_i; the third line mis a positive integer v_i, indicating that there is an undirected edge between and u_i.v_i

The data is guaranteed to be free of multiple edges and self-loops and the graph is connected. Numbers are separated by spaces.

4 ≤ n ≤ m ≤ 1 0 4 , 1 ≤ ui , vi ≤ n , 1 ≤ T ≤ 8 4\le n\le m\le 10^4,1\le u_i,vi\le n,1\le T \the 84nm1041ui,vin1T8

Output : For each set of data, if it is a diamond graph, output a row Yes; otherwise, output a row No.

样例输入:
3
8 8
1 3 7 8 5 6 2 4
3 7 8 5 6 2 4 1
9 9
1 3 7 8 5 6 2 4 3
3 7 8 5 6 2 4 1 9
7 7
1 5 6 7 3 4 2
5 6 7 3 4 2 1


样例输出:
Yes
No
No

This question was not done.

Question 3: Rice Warehouse

Xiaomei's hometown encountered a torrential rain. To this end she established a rescue rice warehouse. There are wagons bringing in and taking away the rice. Now there is a caravan, and each car has to bring or take a certain amount of rice. Assuming that Xiaomei's warehouse initially has M kilograms of rice, it will open the warehouse when a car passes by, so that this car and the vehicles behind it can enter the warehouse to transport or take away the rice. If a car cannot get the rice it wanted, Xiaomei will close the warehouse in advance, and this car and the cars behind it will no longer be able to enter the warehouse. (That is, Xiaomei's warehouse will be open to a continuous substring of the fleet, and the rice in the warehouse will not be negative)

May I ask how many cars can enter Xiaomei's warehouse at most.

Input: For each set of data, it contains two lines of data, the first line is the number of vehicles in the fleet nand the rice that Xiaomei’s warehouse originally has m, and the second line is the amount that the team wants to take away (negative value) or deliver (positive value) rice ai a_iai, numbers are separated by spaces.

1 ≤ n ≤ 50000 , − 1 0 9 ≤ ai ≤ 1 0 9 , 0 ≤ m ≤ 1 0 9 1\and n \and 50000, -10^9\and a_i\and10^9,0\and m\le 10^91n50000109ai1090m109

Output: Output an integer indicating how many vehicles enter at most (the longest continuous substring).

样例输入:
4 10
-16 2 -6 8

样例输出:
3

My train of thought:

  • Use two variables to simulate the entry of vehicles one by one, restrecord the remaining of the current warehouse (initial value m), maxCarrecord the vehicles that have entered the warehouse currently, traverse the array from the beginning a, and the following three situations will occur when the next vehicle enters:
    • The surplus of the warehouse is still not negative, that is rest + a[i] >= 0, let maxCar := maxCar + 1;
    • If the current warehouse is not enough for the next vehicle to pick up, that is rest + a[i] < 0, the vehicle in front of the current position cannot be allowed to enter. There are two situations at this time:
      • If the amount of rice that the car that is currently going to enter the warehouse wants to take is less than or equal to the initial amount of the warehouse, that is, m + a[i] >= 0then this car can enter, order rest = m + a[i], maxCar = 1;
      • Otherwise, the amount of rice that this car wants to fetch is greater than the initial amount of the warehouse, that is m + a[i] < 0, then it cannot be allowed to come in, skip this car and rest = m, maxCar = 0start from the beginning;
while True:
    try:
        n, m = map(int, input().split())  # 第一行:车队数量,仓库大米存量
        a = list(map(int, input().split()))  # 车队预计要运输的大米
        n = len(a)
        maxCar = 0  # 
        rest = m
        for i in range(n):
            rest += a[i]
            if rest >= 0:
                maxCar += 1
            elif m + a[i] >= 0:
                rest = m + a[i]
                maxCar = 1
            else:
                rest = m
                maxCar = 0
        print(maxCar)
    except: break

Question 4: Buy Juice

Xiaomei's class is organizing a class party! The teacher gave Xiaomei a task: to buy juice drinks for the party.

Xiaomei came to the supermarket and found that there are a total nof different juice drinks in the supermarket, and the types are marked as 1 , 2 , 3 , … , n 1,2,3,\dots,n1,2,3,,n , each drink has a delicacyai a_iai a i a_i aiThe bigger it is, the better the drink. However, since some juice drinks may taste strange, ai a_iaiIt may be less than or equal to 0.

Because she didn't know the taste of each student, Xiaomei bought a bottle of each drink. At this time, Xiaomei was thinking: Can you find such a pair that l, rsatisfies 1 ≤ l ≤ r ≤ n 1\le l\le r \le n1lrn , and does not satisfy[ l , r ] = [ 1 , n ] [l,r]=[1,n][l,r]=[1,n ] (i.e. select all), such that[ l , r ] [l,r][l,r ] Buy a bottle of each drink in this category, and the sum of its delicacy is greater than or equal to the sum of the delicacy of buying a bottle of each drink?

Input: a positive integer in the first line T, indicating that there are Tgroups of data.

For each set of data, the first line is a positive integer n; the second line nis an integer a 1 , a 2 , … , an a_1,a_2,\dots ,a_na1,a2,,an

1 ≤ T ≤ 5 , 3 ≤ n ≤ 5 × 1 0 4 , − 100 ≤ ai ≤ 100 1\and T\and 5, 3\and n\and 5\times 10^4, -100\and a_i\and 1001T5,3n5×104,100ai100

Output: For each set of data, if such is found, output Yes; otherwise, output No.

样例
2
4
1 2 3 4
3
-5 5 -5

答案
No
Yes

My train of thought:

  • Accumulate afrom left to right and accumulate from right to left on
  • Then as long as there is a negative number or 0, it means that there is such a buying method described in the title
T = int(input())  # 有 T 组数据
for _ in range(T):
    n = int(input())
    a = list(map(int, input().split()))
    s = 0
    res = "No"
    for i in range(n):
        s += a[i]
        if s <= 0:
            res = "Yes"
            break
    s = 0
    if res == "No":
        for i in range(n-1, -1, -1):
            s += a[i]
            if s <= 0:
                res = "Yes"
                break
    print(res)

Guess you like

Origin blog.csdn.net/weixin_39679367/article/details/124529682