2018-06-25

Q1: swipline calculate area
Q2: move negative to another side
Q3: shuffle 1 2,3
Q4: 蓄水池隨機求1/n
Q5: 反轉(zhuǎn)k的鏈表
Q6: 4 integer sum to target
Q7: flat linkedlist with children
Q8: merge stone
Q9: inorder BST
Q10: max arr[j] + arr[i] + j - i

Q:
Q:
public Node flat(Node root) {
  //TODO:
  Queue<Node> queue = new ArrayDeque<>();
  //Node tmp = root;
  queue.offer(root);
return add(root, queue);
    
}

private Node add(Node root, Queue<Node> queue) {
 if (queue.isEmpty()) {
    return null;
  }
        Node root = queue.pollFirst();
        Node tmpRoot = root;
        while (root.next != null) {
            if (root.child != null) {
              queue.offer(root.child);
              root = root.next;
            }
          }
          if (root.child != null) {
            queue.offer(root.child);
          }
         }
         root.next = add(queue);
         return tmpRoot;
    
  }
}


Q:

Q:
public int find(int n) {
  IntWraper result = new IntWraper();
  dfs(n, 0, 1, result);
  return result.val;
}

private void dfs(int remaining, int level, int start, intWraper result) {
  if (level == 4 && remaining == 0) {
    result.val++;
    return;
  }
  for (int i = start; i < remaining; i++) {
    dfs(remaining - i, level + 1, i, result);
  }
}
Q3:


Q5:
pu


Q4:
class Solution {
    public List<int[]> getArea(int[][] buildings) {
        //TODO: check input
        List<Wall> input = new ArrayList<>();
        for (int[] one : buildings) {
            Wall leftWall  = new Wall(one[0], one[2], true);
            Wall rightWall = new Wall(one[1], one[2], false);
            input.add(leftWall);
            input.add(rightWall);
        }
        Collections.sort(input, new Comparator<Wall>(){
        //Collections.sort(input, new Comparator<Wall>(Wall a, Wall b){
            @Override
            public int compare(Wall a, Wall b){
            if (a.x != b.x) {
                return Integer.compare(a.x, b.x);
            } else {
                if (a.isLeft && b.isLeft) {
                    return Integer.compare(b.height, a.height);
                } else  if (!a.isLeft && !b.isLeft) {
                    return Integer.compare(a.height, b.height);
                } else {
                    if (a.isLeft) {
                        return -1;
                    } else {
                        return 1;
                    }
                }
            }
            }
        });
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder()); //reverseOrder()
        int result = 0;
        int heightPre = 0;
        int preX = input.get(0).x;
        for (Wall aWall : input) {
            if (aWall.isLeft) {
                maxHeap.offer(aWall.height);
            } else {
                maxHeap.remove(aWall.height);
            }
            int heightCur = maxHeap.isEmpty() ? 0 : maxHeap.peek(); //why isEmpty()?
            if (heightCur != heightPre) {
                result += heightPre * (aWall.x - preX);
                heightPre = heightCur;
                preX = aWall.x;
            }
        }
        System.out.print(result);
            return null;
        
    }
}

class Wall {
    int x;
    int height;
    boolean isLeft;
    
    public Wall(int x, int h, boolean isLeft){
        this.x = x;
        this.height = h;
        this.isLeft = isLeft;
    }
}


Q:
public class RerangeNegPos {
    public int[] rerange(int[] input) {
        int n = input.length;
        int i = 0, j = 0;

        while(j < n) {
            if (input[j] > 0 ) {
                if (i != j) {
                    swap(input, i++, j++);
                } else {
                    i++;
                    j++;
                }

            } else {
                j++;
            }
        }
        return input;
    }

    private void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j]= temp;
        return;
    }

    public static void main(String[] args) {
        RerangeNegPos test = new RerangeNegPos();
        int[] a = test.rerange(new int[]{-12, 11, -13, -5, 6, -7, 5, -3, -6});
        System.out.print(a);
    }

}



Q4:
//Q4
public List<List<Integer>> shuffle(int n) {
  if(n == 0) {
    return null;
  }
  List<List<Integer>> results = new ArrayList<>();
  int[] onePath = new int[n * 2];
  dfs(0, used, results, onePath);
  return results;
}

private void dfs(int level, Set<Integer> used,List<List<rInteger>> results, int[] onePath){
  if (level == 2 * n) {
    results.add(Arrays.asList(onePath));
    return;
  }
  if (onePath[level] != 0 ) {
    for (int i = 1; i <= n; i++) {
      if (!used.contains(i) && level + i  + 1 < 2 * n) {
        used.add(i);
        onePath[level] = i;
        onePath[level + i + 1] = i;
        dfs(level + 1, used, results, onePath);
        onePath[level] = 0;
        onePath[level + i + 1] = 0;
        used.remove(i);
      }
    }
  } else {
    dfs(leevel + 1, used, results, onePath);
  } 
  
}



Q5//
public class ReverseK {

    public ListNode reverseK(ListNode root, int k) {
        ListNode dummy = new ListNode(0);
        dummy.next = root;
        ListNode pre = dummy;
        ListNode nextN = dummy;
        int counter = 1;

        while (root != null) {
            if (counter != k) {
                root = root.next;
                counter++;
            } else {
                nextN = root.next;
                root.next = null;
                ListNode tmpHead = pre.next;
                pre.next = reverse(tmpHead);
                pre = tmpHead;
                System.out.println(pre.val);
                pre.next = nextN;
                counter = 1;
                root = nextN;
            }
        }

        return dummy.next;

    }

    private ListNode reverse(ListNode root) {
        if (root == null) {
            return  null;
        }
        ListNode pre = null;
        ListNode nextN;
        while (root.next != null) {
            nextN = root.next;
            root.next = pre;
            pre = root;
            root = nextN;
        }
        //
        root.next = pre;
        return root;
    }
    public  static void main(String[] args) {
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(3);
        ListNode n4 = new ListNode(4);
        ListNode n5 = new ListNode(5);
        ListNode n6 = new ListNode(6);
        ListNode n7 = new ListNode(7);
        ListNode n8 = new ListNode(8);
        ListNode n9 = new ListNode(9);

        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;
        n5.next = n6;
        n6.next = n7;
        n7.next = n8;
        n8.next = n9;

        ReverseK test = new ReverseK();
        ListNode cur = test.reverseK(n1, 3);

        while (cur != null) {
            System.out.print(cur.val);
            cur = cur.next;
        }


    }
}

class ListNode{
    int val;
    ListNode next;
    public ListNode(int val) {
        this.val = val;
    }
}


//

class Solution {
    public List<int[]> getSkyline(int[][] buildings) {
        //TODO: check input
        List<Wall> input = new ArrayList<>();
        for (int[] one : buildings) {
            Wall leftWall  = new Wall(one[0], one[2], true);
            Wall rightWall = new Wall(one[1], one[2], false);
            input.add(leftWall);
            input.add(rightWall);
        }
        Collections.sort(input, new Comparator<Wall>(){
        //Collections.sort(input, new Comparator<Wall>(Wall a, Wall b){
            @Override
            public int compare(Wall a, Wall b){
            if (a.x != b.x) {
                return Integer.compare(a.x, b.x);
            } else {
                if (a.isLeft && b.isLeft) {
                    return Integer.compare(b.height, a.height);
                } else  if (!a.isLeft && !b.isLeft) {
                    return Integer.compare(a.height, b.height); //
                } else {
                    if (a.isLeft) {
                        return -1;
                    } else {
                        return 1;
                    }
                }
            }
            }
        });
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder()); //reverseOrder()
        List<int[]> result = new ArrayList<>();
        int heightPre = 0;
        for (Wall aWall : input) {
            if (aWall.isLeft) {
                maxHeap.offer(aWall.height);
            } else {
                maxHeap.remove(aWall.height);
            }
            int heightCur = maxHeap.isEmpty() ? 0 : maxHeap.peek(); //why isEmpty()?
            if (heightCur != heightPre) {
                result.add(new int[]{aWall.x, heightCur});
                heightPre = heightCur;
            }
        }
            return result;
        
    }
}

class Wall {
    int x;
    int height;
    boolean isLeft;
    
    public Wall(int x, int h, boolean isLeft){
        this.x = x;
        this.height = h;
        this.isLeft = isLeft;
    }
}


//Q:
public class BSTSplit {
    public TreeNode[]split(TreeNode root, int target) {

        if (root == null) {
            return null;
        }
       return helper(root, new TreeNode[]{null, null}, target);

    }

    private TreeNode[] helper(TreeNode cur, TreeNode[] result, int target){
        if (cur == null) {
            return new TreeNode[]{null, null};
        }

        if (cur.val > target) {

            cur.left = helper(cur.left, result, target)[1];

            cur.right = helper(cur.right, result, target)[1];
            result[1] = cur;
        } else {
            cur.left = helper(cur.left, result, target)[0];
            cur.right = helper(cur.right, result,target)[0];
            result[0] = cur;
        }
        return result;
    }


Q:
public Node flat(Node root) {
  //TODO:
  Queue<Node> queue = new ArrayDeque<>();
  //Node tmp = root;
  queue.offer(root);
return add(root, queue);
    
}

private Node add(Node root, Queue<Node> queue) {
 if (queue.isEmpty()) {
    return null;
  }
        Node root = queue.pollFirst();
        Node tmpRoot = root;
        while (root.next != null) {
            if (root.child != null) {
              queue.offer(root.child);
              root = root.next;
            }
          }
          if (root.child != null) {
            queue.offer(root.child);
          }
         }
         root.next = add(queue);
         return tmpRoot;
    
  }
}


Q:

Q:
public int find(int n) {
  IntWraper result = new IntWraper();
  dfs(n, 0, 1, result);
  return result.val;
}

private void dfs(int remaining, int level, int start, intWraper result) {
  if (level == 4 && remaining == 0) {
    result.val++;
    return;
  }
  for (int i = start; i < remaining; i++) {
    dfs(remaining - i, level + 1, i, result);
  }
}
Q3:


Q5:
pu


Q4:
class Solution {
    public List<int[]> getArea(int[][] buildings) {
        //TODO: check input
        List<Wall> input = new ArrayList<>();
        for (int[] one : buildings) {
            Wall leftWall  = new Wall(one[0], one[2], true);
            Wall rightWall = new Wall(one[1], one[2], false);
            input.add(leftWall);
            input.add(rightWall);
        }
        Collections.sort(input, new Comparator<Wall>(){
        //Collections.sort(input, new Comparator<Wall>(Wall a, Wall b){
            @Override
            public int compare(Wall a, Wall b){
            if (a.x != b.x) {
                return Integer.compare(a.x, b.x);
            } else {
                if (a.isLeft && b.isLeft) {
                    return Integer.compare(b.height, a.height);
                } else  if (!a.isLeft && !b.isLeft) {
                    return Integer.compare(a.height, b.height);
                } else {
                    if (a.isLeft) {
                        return -1;
                    } else {
                        return 1;
                    }
                }
            }
            }
        });
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder()); //reverseOrder()
        int result = 0;
        int heightPre = 0;
        int preX = input.get(0).x;
        for (Wall aWall : input) {
            if (aWall.isLeft) {
                maxHeap.offer(aWall.height);
            } else {
                maxHeap.remove(aWall.height);
            }
            int heightCur = maxHeap.isEmpty() ? 0 : maxHeap.peek(); //why isEmpty()?
            if (heightCur != heightPre) {
                result += heightPre * (aWall.x - preX);
                heightPre = heightCur;
                preX = aWall.x;
            }
        }
        System.out.print(result);
            return null;
        
    }
}

class Wall {
    int x;
    int height;
    boolean isLeft;
    
    public Wall(int x, int h, boolean isLeft){
        this.x = x;
        this.height = h;
        this.isLeft = isLeft;
    }
}


Q:
public class RerangeNegPos {
    public int[] rerange(int[] input) {
        int n = input.length;
        int i = 0, j = 0;

        while(j < n) {
            if (input[j] > 0 ) {
                if (i != j) {
                    swap(input, i++, j++);
                } else {
                    i++;
                    j++;
                }

            } else {
                j++;
            }
        }
        return input;
    }

    private void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j]= temp;
        return;
    }

    public static void main(String[] args) {
        RerangeNegPos test = new RerangeNegPos();
        int[] a = test.rerange(new int[]{-12, 11, -13, -5, 6, -7, 5, -3, -6});
        System.out.print(a);
    }

}



Q4:
//Q4
public List<List<Integer>> shuffle(int n) {
  if(n == 0) {
    return null;
  }
  List<List<Integer>> results = new ArrayList<>();
  int[] onePath = new int[n * 2];
  dfs(0, used, results, onePath);
  return results;
}

private void dfs(int level, Set<Integer> used,List<List<rInteger>> results, int[] onePath){
  if (level == 2 * n) {
    results.add(Arrays.asList(onePath));
    return;
  }
  if (onePath[level] != 0 ) {
    for (int i = 1; i <= n; i++) {
      if (!used.contains(i) && level + i  + 1 < 2 * n) {
        used.add(i);
        onePath[level] = i;
        onePath[level + i + 1] = i;
        dfs(level + 1, used, results, onePath);
        onePath[level] = 0;
        onePath[level + i + 1] = 0;
        used.remove(i);
      }
    }
  } else {
    dfs(leevel + 1, used, results, onePath);
  } 
  
}



Q5//
public class ReverseK {

    public ListNode reverseK(ListNode root, int k) {
        ListNode dummy = new ListNode(0);
        dummy.next = root;
        ListNode pre = dummy;
        ListNode nextN = dummy;
        int counter = 1;

        while (root != null) {
            if (counter != k) {
                root = root.next;
                counter++;
            } else {
                nextN = root.next;
                root.next = null;
                ListNode tmpHead = pre.next;
                pre.next = reverse(tmpHead);
                pre = tmpHead;
                System.out.println(pre.val);
                pre.next = nextN;
                counter = 1;
                root = nextN;
            }
        }

        return dummy.next;

    }

    private ListNode reverse(ListNode root) {
        if (root == null) {
            return  null;
        }
        ListNode pre = null;
        ListNode nextN;
        while (root.next != null) {
            nextN = root.next;
            root.next = pre;
            pre = root;
            root = nextN;
        }
        //
        root.next = pre;
        return root;
    }
    public  static void main(String[] args) {
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(3);
        ListNode n4 = new ListNode(4);
        ListNode n5 = new ListNode(5);
        ListNode n6 = new ListNode(6);
        ListNode n7 = new ListNode(7);
        ListNode n8 = new ListNode(8);
        ListNode n9 = new ListNode(9);

        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;
        n5.next = n6;
        n6.next = n7;
        n7.next = n8;
        n8.next = n9;

        ReverseK test = new ReverseK();
        ListNode cur = test.reverseK(n1, 3);

        while (cur != null) {
            System.out.print(cur.val);
            cur = cur.next;
        }


    }
}

class ListNode{
    int val;
    ListNode next;
    public ListNode(int val) {
        this.val = val;
    }
}


//

class Solution {
    public List<int[]> getSkyline(int[][] buildings) {
        //TODO: check input
        List<Wall> input = new ArrayList<>();
        for (int[] one : buildings) {
            Wall leftWall  = new Wall(one[0], one[2], true);
            Wall rightWall = new Wall(one[1], one[2], false);
            input.add(leftWall);
            input.add(rightWall);
        }
        Collections.sort(input, new Comparator<Wall>(){
        //Collections.sort(input, new Comparator<Wall>(Wall a, Wall b){
            @Override
            public int compare(Wall a, Wall b){
            if (a.x != b.x) {
                return Integer.compare(a.x, b.x);
            } else {
                if (a.isLeft && b.isLeft) {
                    return Integer.compare(b.height, a.height);
                } else  if (!a.isLeft && !b.isLeft) {
                    return Integer.compare(a.height, b.height); //
                } else {
                    if (a.isLeft) {
                        return -1;
                    } else {
                        return 1;
                    }
                }
            }
            }
        });
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder()); //reverseOrder()
        List<int[]> result = new ArrayList<>();
        int heightPre = 0;
        for (Wall aWall : input) {
            if (aWall.isLeft) {
                maxHeap.offer(aWall.height);
            } else {
                maxHeap.remove(aWall.height);
            }
            int heightCur = maxHeap.isEmpty() ? 0 : maxHeap.peek(); //why isEmpty()?
            if (heightCur != heightPre) {
                result.add(new int[]{aWall.x, heightCur});
                heightPre = heightCur;
            }
        }
            return result;
        
    }
}

class Wall {
    int x;
    int height;
    boolean isLeft;
    
    public Wall(int x, int h, boolean isLeft){
        this.x = x;
        this.height = h;
        this.isLeft = isLeft;
    }
}


//Q:
public class BSTSplit {
    public TreeNode[]split(TreeNode root, int target) {

        if (root == null) {
            return null;
        }
       return helper(root, new TreeNode[]{null, null}, target);

    }

    private TreeNode[] helper(TreeNode cur, TreeNode[] result, int target){
        if (cur == null) {
            return new TreeNode[]{null, null};
        }

        if (cur.val > target) {

            cur.left = helper(cur.left, result, target)[1];

            cur.right = helper(cur.right, result, target)[1];
            result[1] = cur;
        } else {
            cur.left = helper(cur.left, result, target)[0];
            cur.right = helper(cur.right, result,target)[0];
            result[0] = cur;
        }
        return result;
    }


//Q:
public Node flat(Node root) {
  //TODO:
  Queue<Node> queue = new ArrayDeque<>();
  //Node tmp = root;
  queue.offer(root);
return add(root, queue);
    
}

private Node add(Node root, Queue<Node> queue) {
 if (queue.isEmpty()) {
    return null;
  }
        Node root = queue.pollFirst();
        Node tmpRoot = root;
        while (root.next != null) {
            if (root.child != null) {
              queue.offer(root.child);
              root = root.next;
            }
          }
          if (root.child != null) {
            queue.offer(root.child);
          }
         }
         root.next = add(queue);
         return tmpRoot;
    
  }
}


Q:

Q:
public int find(int n) {
  IntWraper result = new IntWraper();
  dfs(n, 0, 1, result);
  return result.val;
}

private void dfs(int remaining, int level, int start, intWraper result) {
  if (level == 4 && remaining == 0) {
    result.val++;
    return;
  }
  for (int i = start; i < remaining; i++) {
    dfs(remaining - i, level + 1, i, result);
  }
}
Q3:


Q5:
pu


Q4:
class Solution {
    public List<int[]> getArea(int[][] buildings) {
        //TODO: check input
        List<Wall> input = new ArrayList<>();
        for (int[] one : buildings) {
            Wall leftWall  = new Wall(one[0], one[2], true);
            Wall rightWall = new Wall(one[1], one[2], false);
            input.add(leftWall);
            input.add(rightWall);
        }
        Collections.sort(input, new Comparator<Wall>(){
        //Collections.sort(input, new Comparator<Wall>(Wall a, Wall b){
            @Override
            public int compare(Wall a, Wall b){
            if (a.x != b.x) {
                return Integer.compare(a.x, b.x);
            } else {
                if (a.isLeft && b.isLeft) {
                    return Integer.compare(b.height, a.height);
                } else  if (!a.isLeft && !b.isLeft) {
                    return Integer.compare(a.height, b.height);
                } else {
                    if (a.isLeft) {
                        return -1;
                    } else {
                        return 1;
                    }
                }
            }
            }
        });
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder()); //reverseOrder()
        int result = 0;
        int heightPre = 0;
        int preX = input.get(0).x;
        for (Wall aWall : input) {
            if (aWall.isLeft) {
                maxHeap.offer(aWall.height);
            } else {
                maxHeap.remove(aWall.height);
            }
            int heightCur = maxHeap.isEmpty() ? 0 : maxHeap.peek(); //why isEmpty()?
            if (heightCur != heightPre) {
                result += heightPre * (aWall.x - preX);
                heightPre = heightCur;
                preX = aWall.x;
            }
        }
        System.out.print(result);
            return null;
        
    }
}

class Wall {
    int x;
    int height;
    boolean isLeft;
    
    public Wall(int x, int h, boolean isLeft){
        this.x = x;
        this.height = h;
        this.isLeft = isLeft;
    }
}


Q:
public class RerangeNegPos {
    public int[] rerange(int[] input) {
        int n = input.length;
        int i = 0, j = 0;

        while(j < n) {
            if (input[j] > 0 ) {
                if (i != j) {
                    swap(input, i++, j++);
                } else {
                    i++;
                    j++;
                }

            } else {
                j++;
            }
        }
        return input;
    }

    private void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j]= temp;
        return;
    }

    public static void main(String[] args) {
        RerangeNegPos test = new RerangeNegPos();
        int[] a = test.rerange(new int[]{-12, 11, -13, -5, 6, -7, 5, -3, -6});
        System.out.print(a);
    }

}



Q4:
//Q4
public List<List<Integer>> shuffle(int n) {
  if(n == 0) {
    return null;
  }
  List<List<Integer>> results = new ArrayList<>();
  int[] onePath = new int[n * 2];
  dfs(0, used, results, onePath);
  return results;
}

private void dfs(int level, Set<Integer> used,List<List<rInteger>> results, int[] onePath){
  if (level == 2 * n) {
    results.add(Arrays.asList(onePath));
    return;
  }
  if (onePath[level] != 0 ) {
    for (int i = 1; i <= n; i++) {
      if (!used.contains(i) && level + i  + 1 < 2 * n) {
        used.add(i);
        onePath[level] = i;
        onePath[level + i + 1] = i;
        dfs(level + 1, used, results, onePath);
        onePath[level] = 0;
        onePath[level + i + 1] = 0;
        used.remove(i);
      }
    }
  } else {
    dfs(leevel + 1, used, results, onePath);
  } 
  
}



Q5//
public class ReverseK {

    public ListNode reverseK(ListNode root, int k) {
        ListNode dummy = new ListNode(0);
        dummy.next = root;
        ListNode pre = dummy;
        ListNode nextN = dummy;
        int counter = 1;

        while (root != null) {
            if (counter != k) {
                root = root.next;
                counter++;
            } else {
                nextN = root.next;
                root.next = null;
                ListNode tmpHead = pre.next;
                pre.next = reverse(tmpHead);
                pre = tmpHead;
                System.out.println(pre.val);
                pre.next = nextN;
                counter = 1;
                root = nextN;
            }
        }

        return dummy.next;

    }

    private ListNode reverse(ListNode root) {
        if (root == null) {
            return  null;
        }
        ListNode pre = null;
        ListNode nextN;
        while (root.next != null) {
            nextN = root.next;
            root.next = pre;
            pre = root;
            root = nextN;
        }
        //
        root.next = pre;
        return root;
    }
    public  static void main(String[] args) {
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(3);
        ListNode n4 = new ListNode(4);
        ListNode n5 = new ListNode(5);
        ListNode n6 = new ListNode(6);
        ListNode n7 = new ListNode(7);
        ListNode n8 = new ListNode(8);
        ListNode n9 = new ListNode(9);

        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;
        n5.next = n6;
        n6.next = n7;
        n7.next = n8;
        n8.next = n9;

        ReverseK test = new ReverseK();
        ListNode cur = test.reverseK(n1, 3);

        while (cur != null) {
            System.out.print(cur.val);
            cur = cur.next;
        }


    }
}

class ListNode{
    int val;
    ListNode next;
    public ListNode(int val) {
        this.val = val;
    }
}


//

class Solution {
    public List<int[]> getSkyline(int[][] buildings) {
        //TODO: check input
        List<Wall> input = new ArrayList<>();
        for (int[] one : buildings) {
            Wall leftWall  = new Wall(one[0], one[2], true);
            Wall rightWall = new Wall(one[1], one[2], false);
            input.add(leftWall);
            input.add(rightWall);
        }
        Collections.sort(input, new Comparator<Wall>(){
        //Collections.sort(input, new Comparator<Wall>(Wall a, Wall b){
            @Override
            public int compare(Wall a, Wall b){
            if (a.x != b.x) {
                return Integer.compare(a.x, b.x);
            } else {
                if (a.isLeft && b.isLeft) {
                    return Integer.compare(b.height, a.height);
                } else  if (!a.isLeft && !b.isLeft) {
                    return Integer.compare(a.height, b.height); //
                } else {
                    if (a.isLeft) {
                        return -1;
                    } else {
                        return 1;
                    }
                }
            }
            }
        });
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder()); //reverseOrder()
        List<int[]> result = new ArrayList<>();
        int heightPre = 0;
        for (Wall aWall : input) {
            if (aWall.isLeft) {
                maxHeap.offer(aWall.height);
            } else {
                maxHeap.remove(aWall.height);
            }
            int heightCur = maxHeap.isEmpty() ? 0 : maxHeap.peek(); //why isEmpty()?
            if (heightCur != heightPre) {
                result.add(new int[]{aWall.x, heightCur});
                heightPre = heightCur;
            }
        }
            return result;
        
    }
}

class Wall {
    int x;
    int height;
    boolean isLeft;
    
    public Wall(int x, int h, boolean isLeft){
        this.x = x;
        this.height = h;
        this.isLeft = isLeft;
    }
}


//Q:
public class BSTSplit {
    public TreeNode[]split(TreeNode root, int target) {

        if (root == null) {
            return null;
        }
       return helper(root, new TreeNode[]{null, null}, target);

    }

    private TreeNode[] helper(TreeNode cur, TreeNode[] result, int target){
        if (cur == null) {
            return new TreeNode[]{null, null};
        }

        if (cur.val > target) {

            cur.left = helper(cur.left, result, target)[1];

            cur.right = helper(cur.right, result, target)[1];
            result[1] = cur;
        } else {
            cur.left = helper(cur.left, result, target)[0];
            cur.right = helper(cur.right, result,target)[0];
            result[0] = cur;
        }
        return result;
    }

//Q
public int cal(int[] arr) {
  int maxI = A[0];
int result = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
    int curJ = A[j] + j;
    maxI = Math.max(maxI, A[i] - i);
    result = Math.max(curJ + maxI);
  }
  return result;
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 九九乘法表 感謝您在白忙之中能抽出時間; 作為一個基礎(chǔ)的程序員,通常會用到一些簡單的邏輯,比如下面的九九乘法表.這...
    淺唱yjc閱讀 139評論 0 0
  • NINE ELEMENTS OF AUTHENTIC LEARNING: 1.Provide authentic ...
    靜_靜_閱讀 1,620評論 0 1
  • 題目如下: 思路如下: 題目要求在當前方格只能往右邊、下邊這任意選擇兩個方向到達下一個方格若當前方格被阻擋,那么其...
    Dodo159753閱讀 193評論 0 0
  • 夢兒是最瞞不過的呵, 清清楚楚的, 誠誠實實的, 告訴了 你自己靈魂里的密意和隱憂。
    范范er閱讀 240評論 0 0

友情鏈接更多精彩內(nèi)容