每日AC-- 美团 -- 最大差值 | leetcode-求树最小深度 | 网易招聘笔试(升级之路)

美团  -- 最大插值



题目描述

有一个长为n的数组A,求满足0≤a≤b<n的A[b]-A[a]的最大值。

给定数组A及它的大小n,请返回最大差值。

测试样例:
[10,5],2
返回:0


没什么要说的 ,简单,以后尽量不 A水题 做些树相关的题


看代码:

package algorithm.greedy;

/**
 * 类说明
 * 
 * <pre>
 * Modify Information:
 * Author        Date          Description
 * ============ =========== ============================
 * DELL          2017年6月20日    Create this file
 * </pre>
 * 
 */

public class LongestDistance {

    public int getDis(int[] A, int n) {
        // write code here
        int min = A[0]; 
        int max = 0;
        for(int i = 0; i < A.length ;i++){
//            if(A[i] < min ){
//                min = A[i];
//            }
            min = A[i];
            for(int j = i+1; j < A.length; j++){
                if(max < A[j] - min ){
                    max =  A[j] - min;
                }
            }
            
        }
        return  max;
    }
    
    /**
     * @param args
     */
    public static void main(String[] args) {
//          int[] A = {10,5, 10};
//          int n = 3;
          int[] A = {5477,12458,4200,10290,7790,11432,10743};
          int n = 7;
          int ans = new LongestDistance().getDis(A, n);
          
          System.out.println(ans);
          
    }

}


题目描述

Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.


递归代码:

public class TreeNode {

    public int val;

    public TreeNode left;

    public TreeNode right;

    public TreeNode(int x) {
        val = x;
    }
}


package algorithm.dataStruct;

import common.TreeNode;

/**
 * 类说明
 * 
 * <pre>
 * Modify Information:
 * Author        Date          Description
 * ============ =========== ============================
 * DELL          2017年6月19日    Create this file
 * </pre>
 * 
 */

public class MinimumDepth {
    
    public int run(TreeNode root) {
        
        if(null == root){
            return 0;
        }
        
        int l = run(root.left);
        int r = run(root.right);
        
        if(l == 0 || r == 0){
            return 1+l+r;
        }
        
        return 1+Math.min(l,r);
        
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        
        node1.left = node2;
        node2.left = node3;

        int  ans = new MinimumDepth().run(node1);
        System.out.println(ans);
    }

}


import java.util.Scanner;

/**
 * 类说明
 * 
 * <pre>
 * Modify Information:
 * Author        Date          Description
 * ============ =========== ============================
 * DELL          2017年6月20日    Create this file
 * </pre>
 * 
 */

public class WangYiGuaiShou {
    
    public static int  gcd(int a, int b){
        if(b == 0) return a;
        else return gcd(b, a%b);
    }

    /**
     * 题目描述
     * 
     * 小易经常沉迷于网络游戏.有一次,他在玩一个打怪升级的游戏,他的角色的初始能力值为
     * a.在接下来的一段时间内,他将会依次遇见n个怪物,每个怪物的防御力为b1,b2,b3…bn.
     * 如果遇到的怪物防御力bi小于等于小易的当前能力值c,那么他就能轻松打败怪物,并
     * 且使得自己的能力值增加bi;如果bi大于c,那他也能打败怪物,但他的能力值只能增加bi
     * 与c的最大公约数.那么问题来了,在一系列的锻炼后,小易的最终能力值为多少?
     * 
     * 输入描述:
     * 
     * 对于每组数据,第一行是两个整数n(1≤n<100000)表示怪物的数量和a表示小易的初始能力值.
     * 第二行n个整数,b1,b2…bn(1≤bi≤n)表示每个怪物的防御力
     * 
     * 输出描述:
     * 
     * 对于每组数据,输出一行。每行仅包含一个整数,表示小易的最终能力值
     * 
     * 输入例子:
     * 
     * 3 50 
     * 50 105 200 
     * 5 20
     * 30 20 15 40 100
     * 
     * 输出例子:
     * 
     * 110
     *  205
     * 
     * 
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner cin  = new Scanner(System.in);
        while(cin.hasNext()){
            int n = cin.nextInt();
            int b = cin.nextInt();
            int[] z = new int[100000];
            for(int i = 0; i < n; i++){
                z[i] = cin.nextInt();
            }
            
            int c = b;
            for(int i = 0; i < n;i++){
                if(z[i] <= c){
                    c += z[i];
                }else{
                    c += gcd(c,z[i]);
                }
            }
            System.out.println(c);
        }
        

    }

}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangxiaoming

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值