当前位置:网站首页>Two algorithm questions for Microsoft intern interview -- 20220119
Two algorithm questions for Microsoft intern interview -- 20220119
2022-04-22 07:23:00 【shooter7】
Microsoft intern interview 2 Dao algorithm topic
Topic 1
The title is a bit similar to leetcode The series of topics of rotating array in https://leetcode-cn.com/problems/search-rotate-array-lcci/solution/xuan-zhuan-shu-zu-cong-yi-dao-nan-ge-ge-dcv7a/, Two points of investigation . The interviewer transformed the question into V Type or inverted V Array find the subscript of the minimum value , The specific topics are as follows .
- subject : In an array , Elements are ordered , At most one inflection point , Let's find the subscript of the smallest value in the array .
- Understand the topic : The title is the interviewer's dictation , Some specific details can be added by yourself , My understanding is mainly divided into 4 Medium condition , Arrays are monotonically increasing 、 Monotonic decline 、 Concave V Array , Convex inverted V Array . During the communication between roommate and interview supervisor , The time complexity of making it find O(nlogn)——>O(n)——>O(logn), That is to say, we should finally use the dichotomy method to make it . My understanding is to judge the above first 4 Which of the two situations , And then deal with it , Monotone increasing 、 Monotonic decline 、 Convex inverted V Array this 3 In one case, you can get the answer directly , The key is concave V Array search . My approach is to use two points to approach the lowest point , If mid Return directly at the lowest point , If mid Downhill on the left , Then order left=mid+1 Continue to find . If mid Downhill on the right , Then order right=mid-1 Continue to find , until right be equal to left Return results .
- Code (Java)
public class Main {
public static void main(String[] args) {
int[] arr={
4,2,3};
int len=arr.length-1;
if(len<3) System.out.println(arr[0]<arr[1]?0:1);
// Judge the relationship between concavity and linearity
if(arr[1]>arr[0]&&arr[len]>arr[len-1]){
// Monotone increasing
System.out.println(0);
}else if(arr[1]<arr[0]&&arr[len]<arr[len-1]){
// Monotonic decline
System.out.println(len);
}else if(arr[1]>arr[0]&&arr[len]<arr[len-1]){
// Convex
System.out.println(arr[0]<arr[len]?0:len);
}else if(arr[1]<arr[0]&&arr[len]>arr[len-1]){
// Concave
int left=0,right=len;
while (left<right){
int mid=left+(right-left)/2;
if(arr[mid]<arr[mid-1]&&arr[mid]<arr[mid+1]){
System.out.println(mid);
return;
}else if(arr[mid]<arr[mid-1]&&arr[mid]>arr[mid+1]){
left=mid+1;
}else if(arr[mid]>arr[mid-1]&&arr[mid]<arr[mid+1]){
right=mid-1;
}
}
System.out.println(left);
}
}
}
Topic two
- The maximum path in a binary tree and (leetcode The original question in https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/, difficulty hard)
route It is defined as a path starting from any node in the tree , Along the parent node - Child node connection , A sequence that reaches any node . The same node in a path sequence At most once . This path At least one node , And it doesn't have to go through the root node .
Path and Is the sum of the values of the nodes in the path .
Give you the root node of a binary tree root , Back to its The maximum path and .

- Train of thought solution :
It mainly uses the bottom-up recursive method , Calculate the maximum contribution value of each node in the binary tree from bottom to top , And update the global maximum . To be specific , It is to find a path starting from the node in the subtree with the node as the root node , Make the sum of node values on the path maximum .
For specific ideas, please refer to the problem solution :https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/solution/er-cha-shu-zhong-de-zui-da-lu-jing-he-by-leetcode-/ - Code (Java):
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
int maxSum=Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
// The recursive method , This recursive function returns
maxValue(root);
return maxSum;
}
public int maxValue(TreeNode node){
if(node==null){
return 0;
}
// Recursively calculate the maximum contribution value of left and right child nodes , Only when the maximum contribution value is greater than 0 when , The corresponding child node will be selected
int leftValue=Math.max(maxValue(node.left),0);
int rightValue=Math.max(maxValue(node.right),0);
// The maximum path sum of a node depends on the value of the node 、 The maximum contribution value of the left and right child nodes of the node
int lmr=node.val+leftValue+rightValue;
int ret=node.val+Math.max(leftValue,rightValue);
maxSum=Math.max(maxSum,Math.max(lmr,ret));
return ret;
}
}
版权声明
本文为[shooter7]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220612282818.html
边栏推荐
- 在类加载的过程中,类变量的分配区域和实例变量的分配区域不一样
- 任选一小说网站,爬取任意一部小说,以记事本的形式保存。
- . net learning notes (I) -- introduction, advantages, design ideas, principles and applications of generics
- 设计一个圆类(circle),内有私有成员radius代表半径, 函数get_radius( )用于获得半径、area( )用于计算圆的面积; (2)定义一个桌子类table,内有私有数据h
- Practice of using generics and reflection (including a generic cache) -- handwritten ORM ORM framework
- Write a method Sanjiao (a, B, c) to judge whether the three parameters can form a triangle. If not, throw an exception illegalargumentexception and display the exception information a, B, C "cannot fo
- Written examination for summer internship of meituan spring recruitment -- 20220312
- 我们常说的栈帧的内部结构是什么样的呢?
- Error: [HSI 55-1545], unable to generate fsbl normally, unable to read in MSS file, failed to close system mss
- ASP. Net daily development notes ---- record logs with text documents
猜你喜欢

Build ES6 development environment and compile in real time

modelsim仿真加速注意点

机械设计知识点规划

vivado生成和调用edf网表文件

定义一个学生Student类1 获取学生的姓名:get_name() 返回类型:str 2 获取学生的年龄:get_age() 返回类型:int 3 返回3门科目中最高的分数。get_course()

Flutter环境搭建踩坑过程

(2) Basic configuration of SQL server and connection to SQL server using Navicat

在类加载的过程中,类变量的分配区域和实例变量的分配区域不一样

14行代码完成任意选择图片爬取

我们常说的栈帧的内部结构是什么样的呢?
随机推荐
Pyhon3 批量合并哔哩哔哩缓存的m4s视频文件
[number theory] congruence (III): univariate linear congruence equation
Beyond Compare“授权密钥已被吊销”的解决办法
Install and modify the installation path of utools and vscode plug-ins
CSDN text style
ASP. Net daily development notes ----- WebService server development
[number theory] [indefinite equation] n-ary primary indefinite equation, Pell equation, Pythagoras theorem, Fermat theorem
Mongodb install self start service
[number theory] prime number (III): prime number judgment method (simple method, modular 6 method, Rabin Miller and improvement)
【数论】同余(七):快速幂、矩阵快速幂
Zhejiang University Edition "C language programming (3rd Edition)" topic set exercise 7-4 find out the elements that are not common to two arrays
Pixhawk4+Up Board / NUC Implement VIO By Deploying T265
Written examination for summer internship of meituan spring recruitment -- 20220312
[number theory] Euler function (basic properties, recursive method, formula method, linear sieve method)
[number theory] congruence (V): multivariate linear congruence equation
单例池、单例Bean、单例模式
Raspberry Pi 4b
搭建ES6开发环境,实时编译
a5 transceiver 信号vod和预加重调整关系
Prompt the user to enter his name, and the user will write his name to the file guest Txt program determines that when it is not equal to N, it executes to create the file data Txt, a total of 100000