Reputation: 5384
Lets say that I am trying to emulate a group of stock prices. Each of these stock prices is in an array. The delta is the difference between each of these prices. I am seeking a find the maximum delta and return it to an array. When I tried to implement this in Eclipse, I got the following errors for these two lines of code:
if (delta > largestDelta) {
largestDelta = stocks[i];
}
"The operator > is undefined for the argument type(s) int, int[]"
"Type mismatch: cannot convert from int to int[]"
Here's my entire code:
public class Stocks {
static int[] stocks = { 1, 7, 6, 7, 10, 5, 6, 10 };
// delta = sell point - buy point
// find largest delta
public static int[] findMaximumProfit(int[] stocks) {
int delta = 0;
int[] largestDelta = {0};
for (int i=0; i < stocks.length; i++) {
delta = stocks[i+1] - stocks[i];
if (delta > largestDelta) {
largestDelta = stocks[i];
}
}
return largestDelta;
}
}
What am I possibly doing wrong?
Upvotes: 3
Views: 1220
Reputation: 327
change
int[] largestDelta = {0};
to
int largestDelta = stocks[0];
or
int largestDelta = 0;
Also remember about changing the return type.
Upvotes: 4
Reputation: 53616
Consider keeping track of your delta, so you know how much they were and can optionally do some extra manipulations later on. Plus, make use of what Java already offers in term of ordering and wrapping.
For example :
import java.util.*;
class ProfitDelta implements Comparable<ProfitDelta> {
private int delta;
private int profit;
public ProfitDelta(int delta, int profit) { this.delta = delta; this.profit = profit; }
public int getDelta() { return delta; }
public int getProfit() { return profit; }
public int compareTo(ProfitDelta o) {
return o.delta - this.delta;
}
}
class Main {
static Integer[] stocks = { 1, 7, 6, 7, 10, 5, 6, 10 };
public static void main(String[] args) {
System.out.println("Best amount of profit is ");
for (ProfitDelta profit : getBestProfitAmount(stocks)) {
System.out.println("Profit : " + profit.getProfit() + ", delta=" + profit.getDelta());
}
}
public static ProfitDelta[] getBestProfitAmount(Integer[] stocks) {
ProfitDelta[] profits = new ProfitDelta[stocks.length];
List<Integer> sortedStocks = Arrays.asList(stocks);
List<ProfitDelta> sortedProfits = Arrays.asList(profits);
Collections.sort(sortedStocks);
int delta = 0;
int stockValue;
for (int i = 0; i < stocks.length; i++) {
stockValue = sortedStocks.get(i);
delta = stockValue - delta;
sortedProfits.set(i, new ProfitDelta(delta, stockValue));
delta = stockValue;
}
Collections.sort(sortedProfits);
return profits;
}
}
Will give you the output :
Best amount of profit is
Profit : 5, delta=4
Profit : 10, delta=3
Profit : 1, delta=1
Profit : 6, delta=1
Profit : 7, delta=1
Profit : 6, delta=0
Profit : 7, delta=0
Profit : 10, delta=0
If ordering is important in your original array, you might want to wrap sortedStocks
into another class that will keep the original index value, and add a property originalIndex
to ProfitDelta
. Or event simpler, like this :
import java.util.*;
class ProfitDelta implements Comparable<ProfitDelta> {
private int originalIndex = 0;
private int delta = 0;
private int profit;
public ProfitDelta(int index, int profit) { this.originalIndex = index; this.profit = profit; }
public int getOriginalIndex() { return originalIndex; }
public int getDelta() { return delta; }
public void setDelta(int delta) { this.delta = delta; }
public int getProfit() { return profit; }
public int compareTo(ProfitDelta o) { return o.delta - this.delta; }
}
class ProfitComparator implements Comparator<ProfitDelta> {
@Override public int compare(ProfitDelta o1, ProfitDelta o2) {
return o1.getProfit() - o2.getProfit();
}
}
class Main {
static int[] stocks = { 1, 7, 6, 7, 10, 5, 6, 10 };
public static void main(String[] args) {
System.out.println("Best amount of profit is ");
for (ProfitDelta profit : getBestProfitAmount(stocks)) {
System.out.println("Profit : " + profit.getProfit() +
", delta=" + profit.getDelta() +
", originalIndex=" + profit.getOriginalIndex());
}
}
public static ProfitDelta[] getBestProfitAmount(int[] stocks) {
ProfitDelta[] profits = new ProfitDelta[stocks.length];
int delta = 0;
List<ProfitDelta> sortedProfits = Arrays.asList(profits);
for (int i = 0; i < stocks.length; i++) {
sortedProfits.set(i, new ProfitDelta(i, stocks[i]));
}
Collections.sort(sortedProfits, new ProfitComparator());
for (ProfitDelta profit : profits) {
profit.setDelta(profit.getProfit() - delta);
delta = profit.getProfit();
}
Collections.sort(sortedProfits);
return profits;
}
}
Which outputs :
Best amount of profit is
Profit : 5, delta=4, originalIndex=5
Profit : 10, delta=3, originalIndex=4
Profit : 1, delta=1, originalIndex=0
Profit : 6, delta=1, originalIndex=2
Profit : 7, delta=1, originalIndex=1
Profit : 6, delta=0, originalIndex=6
Profit : 7, delta=0, originalIndex=3
Profit : 10, delta=0, originalIndex=7
Upvotes: 0
Reputation: 771
You are comparing an int, delta
, with an int[], largestDelta
. Perhaps, you should simply say int largestDelta = 0;
and define public static int findMaximumProfit(int[] stocks)
?
if you are looking for the max of all deltas between all possible stocks, you need to do a double loop:
for (int i=0; i < stocks.length; i++) {
for (int j=i+1; j < stocks.length; j++) {
delta = stocks[j] - stocks[i];
if (delta > largestDelta) {
largestDelta[0] = i;
largestDelta[1] = j;
}
}
}
Upvotes: 0
Reputation: 6432
The problem is that you're trying to compare an int and an array. This is of course an error: delta > largestDelta
.
The other error is that that you're assigning an int to an array. This line is also an error: largestDelta = stocks[i];
You have to change largest delta to an int, like so:
int largestDelta = 0;
After doing this, you'll be able to compare delta
and largestDelta
, and also assign stocks[i]
to largestDelta
Hope this helps!
Upvotes: 0