Maximizing the Profit

Sort by

recency

|

13 Discussions

|

  • + 0 comments

    I am using Python 3 and used combinations, but for some reason I am getting a timed out on some test cases:

    def maximumProfit(p):
        #
        # Write your code here.
        #
        max = -99999
        temp = 0
        if len(p) < 3:
            return -1
        c = list(combinations(p, 3))
        for i in c:
            if i[2] > i[1] and i[1] > i[0]:
                temp = i[0]*i[1]*i[2]
                if temp > max:
                    max = temp
        if max == -99999:
            return -1
        else:
            return(max)
    
  • + 0 comments

    Is there a upper limit on the length of the input that can be provided from the console (I'm using Ubuntu terminal)? It reads till 4095 characters from the input and then discards the rest. People who were able to finish this problem using Python3, I wonder how you managed to circumvent this.. Any leads on how may I proceed?

    p = list(map(int, input().rstrip().split()))

  • + 0 comments

    still missing some test cases can anyone catch why ??

    static long calc(long a, long b, long c) {
        if(a == Integer.MIN_VALUE || b == Integer.MIN_VALUE || c == Integer.MIN_VALUE)
            return Integer.MIN_VALUE;
        return a * b * c;
    }
    
    static long maximumProfit(int[] a) {
        TreeSet<Long> lset = new TreeSet<Long>();
        TreeSet<Long> rset = new TreeSet<Long>();
        for(int i : a)
            rset.add((long)i);
    
        long max = Long.MIN_VALUE;
    
        for(int i = 0;i < a.length;i++) {
            rset.remove((long)a[i]);
            long res = Long.MIN_VALUE;
    
            if(lset.size() > 0 && rset.size() > 0) {
                long a1 = (lset.first() < (long)a[i]) ? lset.first() : Integer.MIN_VALUE;
                long a2 = (lset.lower((long)a[i]) == null) ? Integer.MIN_VALUE : lset.lower((long)a[i]);
                long c1 = (rset.higher((long)a[i]) == null) ? Integer.MIN_VALUE : rset.higher((long)a[i]);
                long c2 = (rset.last() > (long)a[i]) ? rset.last() : Integer.MIN_VALUE;
    
                res = Math.max(res, calc(a1, (long)a[i], c1));
                res = Math.max(res, calc(a2, (long)a[i], c1));
                res = Math.max(res, calc(a1, (long)a[i], c2));
                res = Math.max(res, calc(a2, (long)a[i], c2));
                //System.out.println("a[i] = " + a[i] + " res = " + res);
                if(res > max)
                    max = res;
            }
    
            lset.add((long)a[i]);
        }
    
        return max == Long.MIN_VALUE ? -1 : max;
    }
    
  • + 0 comments

    What if input is [-5, -4, 2]. Should the output be 40? In real world wouldn't the profit be negative?

  • + 0 comments

    me