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:
- Each given array will have at least 1 number. There will be at least two non-empty arrays.
- The total number of the integers in all the
m
arrays will be in the range of [2, 10000]. - 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:
- The input array won't violate no-adjacent-flowers rule.
- The input array size is in the range of [1, 20000].
- n is a non-negative integer which won't exceed the input array size.
623. Add One Row to Treepublic 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; } }
-
Given the root of a binary tree, then value
v
and depthd
, you need to add a row of nodes with valuev
at the given depthd
. The root node is at depth 1.The adding rule is: given a positive integer depth
d
, for each NOT null tree nodesN
in depthd-1
, create two tree nodes with valuev
asN's
left subtree root and right subtree root. AndN'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 depthd
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:
- The given d is in range [1, maximum depth of the given tree + 1].
- 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); } }