import depth_first_search.matrix.Q329LongestIncreasingPathInAMatrix;
import public_class.Interval;
import public_class.TreeNode;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        System.out.println(main.clumsy(10));

    }

    public int largestSumAfterKNegations(int[] A, int K) {
        if (A == null || A.length == 0) return 0;
        Arrays.sort(A);
        int index = 0, sum = 0, min = Math.abs(A[0]);
        while (index < A.length && A[index] < 0 && K > 0) {
            sum += Math.abs(A[index]);
            min = Math.min(min, Math.abs(A[index]));
            index++;
            K--;
        }
        while (index < A.length) {
            sum += A[index];
            min = Math.min(min, Math.abs(A[index]));
            index++;
        }
        if (K % 2 != 0) {
            sum -= 2 * min;
        }
        return sum;
    }

    public int clumsy(int N) {
        if (N <= 0) return 0;
        Stack<Integer> stack = new Stack<>();
        stack.push(N);
        char[] oprations = {'*', '/', '+', '-'};
        int index = 0;
        for (int i = N - 1; i >= 1; i--) {
            switch (oprations[index++]) {
                case '*':
                    stack.push(stack.pop() * i);
                    break;
                case '/':
                    stack.push(stack.pop() / i);
                    break;
                case '+':
                    stack.push(i);
                    break;
                case '-':
                    stack.push(-i);
                    break;
                    default:
                        break;
            }
            if (index > 3) index = 0;
        }
        int sum = 0;
        while (!stack.isEmpty()) sum += stack.pop();
        return sum;
    }

    public int minDominoRotations(int[] A, int[] B) {
        int possible1 = A[0], possible2 = B[0], count1 = 0, count2 = 0;
        for (int i = 0; i < A.length; i++) {
            int num1 = A[i], num2 = B[i];
            if (possible1 != num1 && possible1 != num2) possible1 = -1;
            if (possible2 != num1 && possible2 != num2) possible2 = -1;
        }
        if (possible1 == -1 && possible2 == -1) return -1;
        else if (possible1 == -1) {
            for (int i = 0; i < A.length; i++) {
                if (A[i] == B[i]) continue;
                if (A[i] == possible2) count1++;
                if (B[i] == possible2) count2++;
            }
            System.out.println("count1:" + count1);
            System.out.println("count2:" + count1);
            return Math.min(count1, count2);
        } else if (possible2 == -1) {
            for (int i = 0; i < A.length; i++) {
                if (A[i] == B[i]) continue;
                if (A[i] == possible1) count1++;
                if (B[i] == possible1) count2++;
            }
            System.out.println("count1:" + count1);
            System.out.println("count2:" + count1);
            return Math.min(count1, count2);
        } else {
            for (int i = 0; i < A.length; i++) {
                if (A[i] == B[i]) continue;
                if (A[i] == possible1) count1++;
                if (A[i] == possible2) count2++;
            }
            System.out.println("count1:" + count1);
            System.out.println("count2:" + count1);
            return Math.min(count1, count2);
        }
    }

    private int index = 0;
    public TreeNode bstFromPreorder(int[] preorder) {
        return bstFromPreorderHelper(preorder, Integer.MIN_VALUE, Integer.MAX_VALUE);
    }

    private TreeNode bstFromPreorderHelper(int[] pre, int min, int max) {
        if (index == pre.length || pre[index] <= min || pre[index] >= max) return null;
        int val = pre[index];
        TreeNode node = new TreeNode(val);
        index++;
        node.left = bstFromPreorderHelper(pre, min, val);
        node.right = bstFromPreorderHelper(pre, val, max);
        return node;
    }

}




@@ -1,102 +1,4 @@
-import depth_first_search.matrix.Q329LongestIncreasingPathInAMatrix;%0Aimport public_class.Interval;
 %0Aimp
@@ -24,17 +24,16 @@
 eeNode;%0A
-%0A
 import j
@@ -146,3744 +146,14 @@
 ();%0A
-        System.out.println(main.clumsy(10));%0A%0A    %7D%0A%0A    public int largestSumAfterKNegations(int%5B%5D A, int K) %7B%0A        if (A == null %7C%7C A.length == 0) return 0;%0A        Arrays.sort(A);%0A        int index = 0, sum = 0, min = Math.abs(A%5B0%5D);%0A        while (index %3C A.length && A%5Bindex%5D %3C 0 && K %3E 0) %7B%0A            sum += Math.abs(A%5Bindex%5D);%0A            min = Math.min(min, Math.abs(A%5Bindex%5D));%0A            index++;%0A            K--;%0A        %7D%0A        while (index %3C A.length) %7B%0A            sum += A%5Bindex%5D;%0A            min = Math.min(min, Math.abs(A%5Bindex%5D));%0A            index++;%0A        %7D%0A        if (K %25 2 != 0) %7B%0A            sum -= 2 * min;%0A        %7D%0A        return sum;%0A    %7D%0A%0A    public int clumsy(int N) %7B%0A        if (N %3C= 0) return 0;%0A        Stack%3CInteger%3E stack = new Stack%3C%3E();%0A        stack.push(N);%0A        char%5B%5D oprations = %7B'*', '/', '+', '-'%7D;%0A        int index = 0;%0A        for (int i = N - 1; i %3E= 1; i--) %7B%0A            switch (oprations%5Bindex++%5D) %7B%0A                case '*':%0A                    stack.push(stack.pop() * i);%0A                    break;%0A                case '/':%0A                    stack.push(stack.pop() / i);%0A                    break;%0A                case '+':%0A                    stack.push(i);%0A                    break;%0A                case '-':%0A                    stack.push(-i);%0A                    break;%0A                    default:%0A                        break;%0A            %7D%0A            if (index %3E 3) index = 0;%0A        %7D%0A        int sum = 0;%0A        while (!stack.isEmpty()) sum += stack.pop();%0A        return sum;%0A    %7D%0A%0A    public int minDominoRotations(int%5B%5D A, int%5B%5D B) %7B%0A        int possible1 = A%5B0%5D, possible2 = B%5B0%5D, count1 = 0, count2 = 0;%0A        for (int i = 0; i %3C A.length; i++) %7B%0A            int num1 = A%5Bi%5D, num2 = B%5Bi%5D;%0A            if (possible1 != num1 && possible1 != num2) possible1 = -1;%0A            if (possible2 != num1 && possible2 != num2) possible2 = -1;%0A        %7D%0A        if (possible1 == -1 && possible2 == -1) return -1;%0A        else if (possible1 == -1) %7B%0A            for (int i = 0; i %3C A.length; i++) %7B%0A                if (A%5Bi%5D == B%5Bi%5D) continue;%0A                if (A%5Bi%5D == possible2) count1++;%0A                if (B%5Bi%5D == possible2) count2++;%0A            %7D%0A            System.out.println(%22count1:%22 + count1);%0A            System.out.println(%22count2:%22 + count1);%0A            return Math.min(count1, count2);%0A        %7D else if (possible2 == -1) %7B%0A            for (int i = 0; i %3C A.length; i++) %7B%0A                if (A%5Bi%5D == B%5Bi%5D) continue;%0A                if (A%5Bi%5D == possible1) count1++;%0A                if (B%5Bi%5D == possible1) count2++;%0A            %7D%0A            System.out.println(%22count1:%22 + count1);%0A            System.out.println(%22count2:%22 + count1);%0A            return Math.min(count1, count2);%0A        %7D else %7B%0A            for (int i = 0; i %3C A.length; i++) %7B%0A                if (A%5Bi%5D == B%5Bi%5D) continue;%0A                if (A%5Bi%5D == possible1) count1++;%0A                if (A%5Bi%5D == possible2) count2++;%0A            %7D%0A            System.out.println(%22count1:%22 + count1);%0A            System.out.println(%22count2:%22 + count1);%0A            return Math.min(count1, count2);%0A        %7D%0A    %7D%0A%0A    private int index = 0;%0A    public TreeNode bstFromPreorder(int%5B%5D preorder) %7B%0A        return bstFromPreorderHelper(preorder, Integer.MIN_VALUE, Integer.MAX_VALUE);%0A    %7D%0A%0A    private TreeNode bstFromPreorderHelper(int%5B%5D pre, int min, int max) %7B%0A        if (index == pre.length %7C%7C pre%5Bindex%5D %3C= min %7C%7C pre%5Bindex%5D %3E= max) return null;%0A        int val = pre%5Bindex%5D;%0A        TreeNode node = new TreeNode(val);%0A        index++;%0A        node.left = bstFromPreorderHelper(pre, min, val);%0A        node.right = bstFromPreorderHelper(pre, val, max);%0A        return node;%0A    %7D%0A
+%0A    %7D
 %0A%7D%0A%0A

clean up