Java Why 'O(n)' solution take 16 ms?

  • 1

    The running time of my code is like 16ms, but it looks O(n) to me.
    Can someone point me where I got wrong?
    Here is my code:

    public class Solution {
        public int maxSubArray(int[] nums) {
            int max = nums[0];
            int lastSum = nums[0];
            for (int i = 1; i < nums.length;i++) {
                int cur = nums[i];
                lastSum = Math.max(cur, cur + lastSum);
                if (lastSum > max) max = lastSum;
            return max;

Log in to reply

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