1

Here is the problem:

"Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit."

Here is my solution.

public class Solution {
    public int maxProfit(int[] prices) {
        int[] sell = new int[prices.length];
        for(int i = 0; i<prices.length; i++){
            for(int j = 0; j<i; j++){
                sell[i] = Math.max(prices[i] - prices[j], sell[i]);
            }
        }
        int sellPrice = prices[0];
        int day = 0;
        for(int i = 0; i<prices.length; i++){
            if(sellPrice < prices[i]){
                sellPrice = prices[i];
                day = i;
            }
        }

        int buyPrice = prices[0];
        for(int i = 0; i<day; i++){
            buyPrice = Math.min(buyPrice, prices[i]);
        }

        return sellPrice - buyPrice;
    }
}

I'm not sure if it work, which is not a problem now. The problem is, it always shows Runtime Error at
Line 9: (int sellPrice = prices[0];) and
Line 18: (int buyPrice = prices[0];) as "java.lang.ArrayIndexOutOfBoundsException: 0".

So what's wrong with it? How to fix it? Thank you so much.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
Michael Li
  • 111
  • 2
  • 5
  • 1
    Can you show us full code ? Where you are calling `maxProfit` method ? It seems the array `prices` is empty. – Suresh Atta Jul 22 '14 at 06:15
  • 1
    mybe your array has no elements. Can you check if prices.lengh is greater or equals 1? – Jens Jul 22 '14 at 06:19
  • The problem is that you pass `null` into the method - make sure to initialize `prices` before you pass it to the method. – Nir Alfasi Jul 22 '14 at 06:19
  • 1
    Most probably you are passing an empty array to the `int[] prices` argument – Keshan De Silva Jul 22 '14 at 06:19
  • i simply call with new Solution().maxProfit(new int[10]); and did not get any error ... can you post your exception stack trace ? and also how u called this method. – Naveen Ramawat Jul 22 '14 at 06:34

6 Answers6

1

The only reason prices[0] would throw an AIOOBE is because the prices doesn't have any prices in it.

For example:

public static void main(String[] args)
{
    int[] ints = new int[] {};

    System.out.println(ints[0]);
}

Throws this:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Main.main(Main.java:7)

Whereas this, works.

public static void main(String[] args)
{
    int[] ints = new int[] {93727};

    System.out.println(ints[0]);
}
Alexandre Santos
  • 8,170
  • 10
  • 42
  • 64
1

Check if you are not passing a zero length array to your maxProfit method and be sure to initialize the array before using it in your method.

androider
  • 982
  • 6
  • 16
  • 32
0

Here is the possibilty:

ArrayIndexOutOfBoundsException

Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

The array you passing to this method is empty. That is no elements in that array. The part

equal to the size of the array. (size is 0 and index is zero)

That is the cause in your case.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

You're getting an exception probably because the prices array was not instantiated correctly.

Also, your algorithm is not guaranteed to be correct: consider the following set of prices,

$4, $5, $1, $3

The maximum profit made would be 2 dollars, but your algorithm would result in $1.

Here's some correct answers: Maximum single-sell profit

Community
  • 1
  • 1
Caleb An
  • 366
  • 1
  • 10
0

How does your main look like??

Make sure the array prices[] is created and also initialized. If it is created like int[] prices = new int[] {}; you will see an error saying index out of bound. Make sure you initialize (from user input or using hard coded values). If it is from user input make sure that the values are assigned.

Hard Coded one will look like this:

int price[] = {100,200,500,2000,700,500,400};

Hope it helps

Waman
  • 1,179
  • 7
  • 16
-2

leetcode

When prices.lenght = 0, prices[] is null at line 9 and 18. That's why you got errors.

mortred
  • 1
  • 2