Notice: HUAWEI, Ali's latest autumn test written test questions, ideas and reference codes have been arranged well in WeChat official account, TechGuide, and the official account of the private letter is returned to HUAWEI or Ali to get the most real-time and detailed written test questions.
Title Description
Give a binary tree, each node has a number and a value, which may be negative. Please find an optimal node (except the root node), so that after the node divides the tree into two trees (the original tree removes this node and its children, and the new tree takes this node as the root node), the absolute value of the difference between the sum of each node of the two trees is the largest.
Please output the node number. If there are multiple same differences, output the node with the smallest number.
Enter description
4
4 9 -7 -8
0 1
0 3
1 2
The first row, four nodes, number 0-3, range 1-10000
The second line is the weight of node 0-3
The third to fifth lines represent the parent-child relationship between the nodes of the binary tree
0 1 / / the left node of node 0 is 1
0 3 / / the right node of node 0 is 3
1 2 / / the left node of node 1 is 2
Note: the left node always appears before the right node
4 4 9 -7 -8 0 1 0 3 1 2
Output description
Node number. In the example, the node with number 3 is the optimal node
3
Reference code
Using dfs, you can find the sum of each node and all its child nodes. Note that you need to use long long
// Follow TechGuide! Dachang pen classics are delivered by lightning express!Second track: Plum Blossom pile (100%)
Title Description
There is an M*N plum blossom pile array, and each pile has the maximum number of steps allowed to jump. The user can jump from the position of 0,0, and can jump in the right and down directions. Find out how many times it takes to jump at least to reach the positions of M-1 and N-1. - 1 is returned when the destination cannot be reached.
M < = 100, n < = 100, the maximum number of steps allowed to jump on each pile is a positive integer less than 10, and 0 means that jumping to this position is not allowed
Enter Description:
1. The first line is M and N, separated by ",";
2. The second line is M*N plum blossom pile (refer to the example for format). The array position is the maximum number of steps allowed to jump. 0 means that the position is empty and cannot jump to this position.
3,3 3 2 2 0 1 0 1 1 1
Output description
Minimum jump steps
2
Reference code
Violence, update the shortest steps of all points it can reach for each point
public class TechGuide{ static class Node{ int row; int col; public Node(int row, int col){ this.row = row; this.col = col; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Node node = (Node) o; return row == node.row && col == node.col; } @Override public int hashCode() { return Objects.hash(row, col); } } static int distance(Node node1, Node node2){ return Math.abs(node2.row - node1.row) + Math.abs(node2.col - node1.col); } public static void main(String[] args) { Scanner in = new Scanner(System.in); String line1 = in.nextLine(); String line2 = in.nextLine(); String[] line1s = line1.split(","); String[] line2s = line2.split(" "); int m = Integer.parseInt(line1s[0]); int n = Integer.parseInt(line1s[1]); int[][] nums = new int[m][n]; for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++) nums[i][j] = Integer.parseInt(line2s[i*m + j]); } Queue<Node> nodes = new ArrayDeque<>(); int[][] dp = new int[m][n]; for(int i =0; i < m; i++){ Arrays.fill(dp[i], Integer.MAX_VALUE); } dp[0][0] = 0; nodes.add(new Node(0,0)); while(!nodes.isEmpty()){ Node node = nodes.poll(); int step = nums[node.row][node.col]; for(int i = step; i > 0; i--){ for(int row = node.row + i; row >= node.row; row--) { int col = i - (row - node.row) + node.col ; if (row < m && col < n && nums[row][col] != 0) { dp[row][col] = Math.min(dp[row][col], dp[node.row][node.col] + 1); Node temp = new Node(row, col); if(!nodes.contains(temp)) nodes.add(new Node(row, col)); if(temp.row == m -1 && temp.col ==n - 1){ System.out.println(dp[m - 1][n - 1]); System.exit(0); } } } } } System.out.println(-1); } } // Follow TechGuide! Dachang pen classics are delivered by lightning express!
CPP version
#include<bits/stdc++.h> using namespace std; #define ll long long const int mod = 1e9 + 7; const int inf = 0x3f3f3f3f; const int ninf = 0xc0c0c0c0; const int maxn = 100 + 5; int f[maxn][maxn]; int dp[maxn][maxn]; int main() { string s; cin >> s; int n, m; sscanf(s.c_str(), "%d,%d", &n, &m); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { scanf("%d", &f[i][j]); } } memset(dp, inf, sizeof dp); dp[0][0] = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { int x = f[i][j]; for (int k = 0; k <= x; ++k) { if (i + k < n && f[i + k][j] != 0) { dp[i + k][j] = min(dp[i + k][j], dp[i][j] + 1); } if (j + k < m && f[i][j + k] != 0) { dp[i][j + k] = min(dp[i][j + k], dp[i][j] + 1); } } } } if (dp[n - 1][m - 1] != inf) { cout << dp[n - 1][m - 1]; } else { cout << -1; } return 0; } /* testcases: 3,3 3 2 2 0 1 0 1 1 1 */ // Follow TechGuide! Dachang pen classics are delivered by lightning express!Third track: minimum compilation time (100%)
Title Description
Company A needs to introduce an open source project into the project and evaluate the compilation time of A module in the open source project.
At present, the compilation time of each module in the project and the list of modules it depends on are known. When there are an unlimited number of parallel tasks, find the shortest compilation time of a specified module.
If there is a circular dependency between modules or the dependent module does not exist, the compilation cannot be completed and - 1 is returned.
Enter Description:
The first line is the target module name,
In the future, each line of input defines a module, including the module name, compilation time and dependent module list, separated by commas. If the dependent module list does not exist, it means that it can be compiled independently, such as module 2,10, module 1,10
The module name contains only letters and numbers and at least one character. The number of modules shall not exceed 50000
module3 module1,10 module2,5 module3,10,modulel,moduln2
Output Description:
Output the minimum compilation time. If the compilation cannot be completed, output - 1
20
Explanation:
Module1 compilation takes 10 ms, module2 compilation takes 5 ms, module3 compilation depends on module1 and module2, and self compilation also takes 10 ms, so the total compilation time is 10+max(10, 5) = 20 ms
Reference code
Topological sorting. I don't see that there can be letters after the module, which is written in numbers
public class TechGUide{ public static void main(String[] args) { Scanner in = new Scanner(System.in); String name = in.nextLine(); List<String> strings = new ArrayList<>(); while(in.hasNextLine()){ String string = in.nextLine(); strings.add(string); } Set<String> set = new HashSet<>(); //Start flag set.add("start"); //Count the names of all modules, which can be mapped for(String s: strings){ String[] temp = s.split(","); for(int i = 0; i < temp.length; i++){ if(i != 1){ set.add(temp[i]); } } } //Map letters to numbers HashMap<String, Integer> mapping = new HashMap<>(); int index = 0; for(String s : set){ mapping.put(s, index); index++; } //Save the next node location of the node HashMap<String, List<String>> relation = new HashMap<>(); relation.put("start", new ArrayList<String>()); int[][] weights = new int[set.size()][set.size()]; for(String s: strings){ String[] temp = s.split(","); //String end = temp[0]; if(temp.length == 2){ List<String> start = relation.get("start"); start.add(temp[0]); weights[mapping.get("start")][mapping.get(temp[0])] = Integer.parseInt(temp[1]); } else{ for(int i = 2; i < temp.length; i++){ weights[mapping.get(temp[i])][mapping.get(temp[0])] = Integer.parseInt(temp[1]); if(relation.containsKey(temp[i])){ List<String> next = relation.get(temp[i]); next.add(temp[0]); } else{ List<String> next = new ArrayList<>(); next.add(temp[0]); relation.put(temp[i], next); } } } } //Check for circular dependencies for(int i = 0; i < set.size() - 1; i++){ for(int j = i + 1; j < set.size(); j++){ if(weights[i][j] != 0 && weights[j][i] != 0){ System.out.println(-1); System.exit(0); } } } Queue<String> que = new ArrayDeque<>(); que.add("start"); //Save the maximum time it takes to execute the task int[] end = new int[set.size()]; int max = 0; //Traverse from scratch while(!que.isEmpty()){ String node = que.poll(); //Module from execution to end List<String> nextNodes = relation.get(node); //Module missing, early termination if(!node.equals(name) && (nextNodes.size()==0 || nextNodes == null)){ System.out.println(-1); break; } //It's time for the task. The maximum time for the array to reach the task if(node.equals(name)){ System.out.println(end[mapping.get(node)]); break; } for(String next : nextNodes){ que.add(next); end[mapping.get(next)] = Math.max(end[mapping.get(next)], end[mapping.get(node)] + weights[mapping.get(node)][mapping.get(next)]); } } } } /* m2 m2,10,m1 m1,10,m2 */ /* m2 m2,10,m1 */ /* m3 m1,10 m2,5 m3,10,m1,m2 // Follow TechGuide! Dachang pen classics are delivered by lightning express! */
Congratulations on finding the treasure! WeChat search official account [TechGuide] pays attention to more fresh good articles and the Internet classics.