C# solution, but is not passing TLE


  • 0
    T
    public class Solution {
       public int MaxRotateFunction(int[] A)
            {
                int maxValue = int.MinValue;
                
                if (A.Length == 0)
                {
                    return 0;
                }
    
                for (int i = 0; i < A.Length; i++)
                {
                    int value = CalculateValue(A, i);
                    maxValue = value > maxValue ? value : maxValue;
                }
    
                return maxValue;
            }
    
            private int CalculateValue(int[] A, int k)
            {
                int value = 0;
    
                for (int i = 0; i < A.Length; i++)
                {
                    int newIndex = i + k;
                    if (newIndex >= A.Length)
                    {
                        newIndex -= A.Length;
                    }
    
                    value += i * A[newIndex];
                }
    
                return value;
            }
    
    }
    

    I feel this is a fairly efficient solution for the problem. But still my solution is not passing TLE. Please review this solution.


Log in to reply
 

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