Skip to content

Latest commit

 

History

History
30 lines (25 loc) · 788 Bytes

File metadata and controls

30 lines (25 loc) · 788 Bytes

LeetCode Records - Question 2640 Find the Score of All Prefixes of an Array

Attempt 1: Calculate the maximum number of array first

class Solution {
    public long[] findPrefixScore(int[] nums) {
        int[] maxNums = new int[nums.length];

        int currMax = nums[0];
        for (int i = 0; i < nums.length; i++) {
            currMax = Math.max(currMax, nums[i]);
            maxNums[i] = currMax;
        }

        long[] ans = new long[nums.length];
        long currSum = 0;
        for (int i = 0; i < nums.length; i++) {
            long val = nums[i] + maxNums[i];
            currSum += val;
            ans[i] = currSum;
        }

        return ans;
    }
}
  • Runtime: 3 ms (Beats: 83.75%)
  • Memory: 66.65 MB (Beats: 13.59%)