Demo entry 6663004

subseqsum

   

Submitted by anonymous on Nov 28, 2017 at 23:29
Language: Java. Code size: 990 Bytes.

public class DayTraderStrength {
    public static int algorithmAforStrength(int[ ] profits) {
        int n = profits.length;
        int maximum = profits[0];

        for(int j = 1; j < n; j++) {
            for(int i = 0; i <= j; i++) {
                int subseq_sum = 0;
                for(int idx = i; idx <= j; idx++) {
                    subseq_sum += profits[idx];
                }
                if (subseq_sum > maximum) {
                    maximum = subseq_sum;
                }
            }
        }

        return maximum;
    }

    public static int algorithmBforStrength(int[ ] profits) {
        int n = profits.length;

        int[] M = new int[n+1];
        M[0] = 0;
        int maximum = M[0];

        for(int j = 1; j < n+1; j++) {
            M[j] = Math.max(M[j-1]+profits[j-1],profits[j-1]);
            if(M[j] > maximum) {
                maximum = M[j];
            }
        }

        return maximum;
    }
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).