2017_06_22几题leetcode

624. Maximum Distance in Arrays


Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers from two different arrays (each array picks one) and calculate the distance. We define the distance between two integers a and b to be their absolute difference |a-b|. Your task is to find the maximum distance.

Example 1:

Input: 
[[1,2,3],
 [4,5],
 [1,2,3]]
Output: 4
Explanation: 
One way to reach the maximum distance 4 is to pick 1 in the first or third array and pick 5 in the second array.

Note:

  1. Each given array will have at least 1 number. There will be at least two non-empty arrays.
  2. The total number of the integers in all the m arrays will be in the range of [2, 10000].
  3. The integers in the m arrays will be in the range of [-10000, 10000].
public class Solution {
    public int maxDistance(List<List<Integer>> arrays) {
        TreeMap<Integer,ArrayList<Integer>> map=new TreeMap<>();
        int ans=0;
        for(int i=0;i<arrays.size();i++){
            List<Integer> list=arrays.get(i);
            if(list.size()>0){
                int min=list.get(0);
                int max=list.get(list.size()-1);
                if(!map.containsKey(min)){
                    ArrayList<Integer> inList=new ArrayList<Integer>();
                    inList.add(i);
                    map.put(min,inList);
                }else{
                    ArrayList inList=map.get(min);
                    inList.add(i);
                    map.put(min,inList);
                }
                if(max==min)continue;
                if(map.containsKey(max)){
                    ArrayList inList=map.get(max);
                    inList.add(i);
                    map.put(max,inList);
                }else{
                    ArrayList<Integer> inList=new ArrayList<Integer>();
                    inList.add(i);
                    map.put(max,inList);
                }
            }
        }
        if(map.size()>1){
            int min=map.firstKey();
            int max=map.lastKey();
            ArrayList<Integer> minList=map.get(min);
            ArrayList<Integer> maxList=map.get(max);
            if(minList.size()>1||maxList.size()>1||minList.get(0)!=maxList.get(0)){
                ans=max-min;
            }
            int count=0;
            int secMin=0;
            int secMax=0;
            for(int key:map.keySet()){
                if(count ==1){
                    secMin=key;
                }
                if(count==map.size()-2){
                    secMax=key;
                }
                count++;
            }
            ans=Math.max(ans,Math.max(max-secMin,secMax-min));
        }
        return ans;
    }
}
605. Can Place Flowers

Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die.

Given a flowerbed (represented as an array containing 0 and 1, where 0 means empty and 1 means not empty), and a number n, return if n new flowers can be planted in it without violating the no-adjacent-flowers rule.

Example 1:

Input: flowerbed = [1,0,0,0,1], n = 1
Output: True

Example 2:

Input: flowerbed = [1,0,0,0,1], n = 2
Output: False

Note:

  1. The input array won't violate no-adjacent-flowers rule.
  2. The input array size is in the range of [1, 20000].
  3. n is a non-negative integer which won't exceed the input array size.
    public class Solution {
        public boolean canPlaceFlowers(int[] flowerbed, int n) {
            int count = 0;
            if (n == 0) return true;
            int[] nums = new int[flowerbed.length + 2];
            for (int i = 0; i < flowerbed.length; i++) {
                nums[i + 1] = flowerbed[i];
            }
            int tempCount = 0;
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] == 0) {
                    tempCount++;
                } else {
                    if (tempCount > 2) {
                        count += (tempCount-1) / 2;
                    }
                    if (count >= n) {
                        return true;
                    }
                    tempCount = 0;
                }
                if (i == nums.length - 1 && tempCount > 2) {
                    count += (tempCount-1) / 2;
    
                    if (count >= n) {
                        return true;
                    }
                }
            }
            return false;
        }
    }
    623. Add One Row to Tree
  4. Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with value v at the given depth d. The root node is at depth 1.

    The adding rule is: given a positive integer depth d, for each NOT null tree nodes N in depth d-1, create two tree nodes with value v as N's left subtree root and right subtree root. And N's original left subtree should be the left subtree of the new left subtree root, its original right subtree should be the right subtree of the new right subtree root. If depth d is 1 that means there is no depth d-1 at all, then create a tree node with value v as the new root of the whole original tree, and the original tree is the new root's left subtree.

    Example 1:

    Input: 
    A binary tree as following:
           4
         /   \
        2     6
       / \   / 
      3   1 5   
    
    v = 1
    
    d = 2
    
    Output: 
           4
          / \
         1   1
        /     \
       2       6
      / \     / 
     3   1   5   
    
    

    Example 2:

    Input: 
    A binary tree as following:
          4
         /   
        2    
       / \   
      3   1    
    
    v = 1
    
    d = 3
    
    Output: 
          4
         /   
        2
       / \    
      1   1
     /     \  
    3       1
    

    Note:

    1. The given d is in range [1, maximum depth of the given tree + 1].
    2. The given binary tree has at least one tree node.
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public TreeNode addOneRow(TreeNode root, int v, int d) {
            classV=v;
            classDeep=d;
            if(d>1)preorder(root,1);
            if(d==1){
                TreeNode newRoot=new TreeNode(v);
                newRoot.left=root;
                return newRoot;
            }
            return root;
        }
        public int classV;
        public int classDeep=-1;
        public void preorder(TreeNode root,int deep){
            if(root==null){
                return;
            }
            if(deep+1==classDeep){
               if(root!=null){
                   TreeNode temp=root.left;
                   root.left=new TreeNode(classV);
                   root.left.left=temp;
               }
               if(root!=null){
                   TreeNode temp=root.right;
                   root.right=new TreeNode(classV);
                   root.right.right=temp;
               }
               return;
            }
            preorder(root.left,deep+1);
            preorder(root.right,deep+1);
        }
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值