Q1: swipline calculate area
Q2: move negative to another side
Q3: shuffle 1 2,3
Q4: 蓄水池随机求1/n
Q5: 反转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 queue = new ArrayDeque<>();
//Node tmp = root;
queue.offer(root);
return add(root, queue);
}
private Node add(Node root, Queue 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 getArea(int[][] buildings) {
//TODO: check input
List 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(){
//Collections.sort(input, new Comparator(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 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> shuffle(int n) {
if(n == 0) {
return null;
}
List> results = new ArrayList<>();
int[] onePath = new int[n * 2];
dfs(0, used, results, onePath);
return results;
}
private void dfs(int level, Set used,List> 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 getSkyline(int[][] buildings) {
//TODO: check input
List 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(){
//Collections.sort(input, new Comparator(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 maxHeap = new PriorityQueue<>(Collections.reverseOrder()); //reverseOrder()
List 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 queue = new ArrayDeque<>();
//Node tmp = root;
queue.offer(root);
return add(root, queue);
}
private Node add(Node root, Queue 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 getArea(int[][] buildings) {
//TODO: check input
List 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(){
//Collections.sort(input, new Comparator(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 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> shuffle(int n) {
if(n == 0) {
return null;
}
List> results = new ArrayList<>();
int[] onePath = new int[n * 2];
dfs(0, used, results, onePath);
return results;
}
private void dfs(int level, Set used,List> 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 getSkyline(int[][] buildings) {
//TODO: check input
List 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(){
//Collections.sort(input, new Comparator(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 maxHeap = new PriorityQueue<>(Collections.reverseOrder()); //reverseOrder()
List 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 queue = new ArrayDeque<>();
//Node tmp = root;
queue.offer(root);
return add(root, queue);
}
private Node add(Node root, Queue 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 getArea(int[][] buildings) {
//TODO: check input
List 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(){
//Collections.sort(input, new Comparator(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 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> shuffle(int n) {
if(n == 0) {
return null;
}
List> results = new ArrayList<>();
int[] onePath = new int[n * 2];
dfs(0, used, results, onePath);
return results;
}
private void dfs(int level, Set used,List> 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 getSkyline(int[][] buildings) {
//TODO: check input
List 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(){
//Collections.sort(input, new Comparator(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 maxHeap = new PriorityQueue<>(Collections.reverseOrder()); //reverseOrder()
List 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;
}