We can't use static in java? Or is this a Leetcode bug?


  • 0

    The code below is perfectly right if it runs in IntelliJ, but I got a wrong answer in leetcode.

    public class Solution {
        public static int sum;
        public static int pre;
        public int maxRotateFunction(int[] A) {
            // int sum = 0;
            // int pre = 0;
            int max = Integer.MIN_VALUE;
            int n = A.length;
            if (n <= 1) return 0;
            for (int i = 0; i < n; i++) {
                sum += A[i];
                pre += i * A[i];
            }
            if (pre > max) max = pre;
            for (int i = n - 1; i >= 1; i--) {
                pre = pre + sum - n * A[i];
                if (pre > max) max = pre;
            }
            return max;
        }
    }
    

  • 0
    T

    They use the same instance of the Solution class for every test case so you have to reset your static variables every time


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.