leetcode meituan real exercise 03

Posted by Bogart on Mon, 20 Dec 2021 06:19:05 +0100

meituan-005. Xiaomei's Regional Conference

Title Description

Xiaomei is an executive of meituan headquarters. She wants to call some regional heads of meituan for a meeting. It is known that meituan's business area division can be represented by a tree. There are n nodes on the tree, each node represents a business area of meituan, and each region has a person in charge. The level of the person in charge is A[i]
It is known that Xiaomei must meet the following conditions when convening a meeting:

  • The area where the person in charge convened by Xiaomei must form a non empty connected graph, that is, select a connected subgraph on the tree.
  • . Among these leaders, the difference between the highest level and the lowest level shall not exceed k.

How many ways does Xiaomei have to summon the person in charge? If and only if the selected collection is different, we think the two methods are different. Since the number of schemes may be very large, please take the modulus of 10 ^ 9 + 7.

Input:

  • The first line of input contains two integers n and k, indicating the number of regions and the level that cannot be exceeded.
  • Next, there are n-1 lines, and each line has two positive integers a and b, indicating that there is an edge in area a and area b.
  • The last line has n integers, and the i integer represents the level of the person in charge of area i.
    Output:
  • The output contains only one integer, which represents the result after the number of alternative schemes is modulo 10 ^ 9 + 7.

Example:
Input:
5 1
1 2
2 3
3 4
4 5
2 2 2 2 2
Output: 15
Explanation: obviously, there are {1}, {2}, {3}, {4}, {5} in one region, 4 in two regions, 3 in three regions, 2 in four regions and 1 in five regions, a total of 15.

Problem solving ideas

replay

My idea for this question is based on each node i i i is the root node, dfs traverses, and records from the root node to j j The level difference of j is recorded d p [ i ] [ j ] dp[i][j] dp[i][j].
Then traverse dp to find less than k k The value of k
Later I found out that this was just from i i i to j j A single non repeated path of j is not considered in the case of repeated path

Problem solution

Reference problem solution Enum min + DFS , to the current node i i i create a tree for the root node with the least permission and record the number of possible sets r o o t = ∏ ( n o d e i + 1 ) root=\prod(node_i+1) root = Π (nodei + 1), that is, the number of possible sets of each child node + 1 +1 +Multiply by 1, + 1 +1 +1 is because you can choose not to select the subtree
Note that when there are two nodes that can be connected and have equal permissions, in this case, the set composed of the two nodes is exactly the same, so it needs to be de duplicated

import java.util.*;

public class Solution {
	static Map<Integer,List<Integer>> map = new HashMap<>();
	static int Mod = 1000000000+7;
	
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int k = scan.nextInt();
		for(int i=0;i<n-1;i++) {
			int a = scan.nextInt();
			int b = scan.nextInt();
			List<Integer> l = map.getOrDefault(a, new LinkedList<>());
			l.add(b);
			map.put(a, l);
			l = map.getOrDefault(b, new LinkedList<>());
			l.add(a);
			map.put(b, l);
		}
		int[] A = new int[n+1];
		for(int i=0;i<n;i++) {
			A[i+1] = scan.nextInt();
		}
		int ans=0;
		//Create a tree with 1 as the minimum permission and return the number of possible species
		for(int i=1;i<=n;i++) {
			boolean[] v = new boolean[n+1];
			v[i]=true;
			ans += dfs(i,A,i,k,v);
			ans%=Mod;
		}

		System.out.println(ans);
	}

	private static int dfs(int node, int[] A,int i,int k,boolean[] v) {
		long res=1;
		List<Integer> l = map.getOrDefault(node, null);
		if(l==null)
			return (int) (res%Mod);
		for(int addn:l) {
			if(v[addn]) continue;
			if(A[addn]<A[i]||A[addn]>A[i]+k)
				continue;
			if(A[addn]==A[i]&&addn<i)
				continue;
			v[addn]=true;
			res*=(dfs(addn,A,i,k,v)+1);
			v[addn]=false;
			res%=Mod;
		}
		return (int) (res%Mod);
	}
}

meituan-006. The mysterious code of the small group

Title Description

Xiaotuan is well aware of the importance of confidentiality, so it will use an encryption strategy in some plaintext transmission. If Xiaotuan needs to transmit a string S, it will add a header string and a tail string to the string. The header string must contain at least one "MT" subsequence and end with T. The tail string must contain at least one "MT" subsequence and start with M. For example, both AAAMT and MAAAT are legal header strings, while MTAAA is not a legal header string. Obviously, such a leading and trailing string is not necessarily unique, so we also have a constraint that S is the longest string when the leading and trailing string is legal.
Obviously, this encryption strategy supports decoding. Give an encrypted string. Please find the encrypted string S in the middle.
Input:

  • The first line of input is a positive integer n, which represents the total length of the encrypted string.
  • The second line of input is a string T of length n consisting only of uppercase letters.
    Output:
  • The output contains only one string S.

Example:
Input:
10
MMATSATMMT
Output: SATM

Problem solving ideas

replay

Simple simulation, a little slow

Problem solution

Head and tail traversal simulation

import java.util.*;

public class Solution {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		scan.nextLine();
		String s = scan.nextLine();
		boolean flag1=false,flag2=false;
		int l=0,r=n-1;
		while(l<r){
			//Head tail traversal
			if(!flag1&&s.charAt(l)=='T') {
				flag1 = check(s.substring(0, l+1));
			}
			if(!flag2&&s.charAt(r)=='M') {
				flag2 = check(s.substring(r, n));
			}
			if(!flag1) {
				l++;
			}
			if(!flag2) {
				r--;
			}
			if(flag1&&flag2) {
				System.out.println(s.substring(l+1,r));
				return;
			}
		}
	}

	private static boolean check(String s) {
		boolean flag=false;
		boolean ans=false;
		for(int i=0;i<s.length();i++) {
			if(s.charAt(i)=='M')
				flag=true;
			if(flag&&s.charAt(i)=='T')
				ans=true;
		}
		return ans;
	}
}

Topics: Algorithm leetcode