package com.hong.www; import java.util.ArrayList; import java.util.HashMap; public class A123 { public int maxProfit(int[] prices) { /* * int temp=prices[0]; * ArrayList temp1=new ArrayList<>(); * ArrayList temp2=new ArrayList<>(); * int up=0; * for(int x=1;xtemp&&up==1){ * temp2.add(prices[x]-temp) * }else if(prices[x]>temp){ * up=1; * temp2.clear(); * temp2.add(prices[x]-temp); * }else{ * up=0; * * } * temp=prices[x]; * } */ int x=0; int y=0; HashMap temp=new HashMap<>(); for (int i = 0; i < prices.length; i++) { for (int j = i; j < prices.length; j++) { if(temp.containsKey(prices[j]-prices[i])){ temp.put(prices[j]-prices[i], temp.get(prices[j]-prices[i])+1); }else{ temp.put(prices[j]-prices[i], 1); } if(prices[j]-prices[i]>x){ y=x; x=prices[j]-prices[i]; } } } temp.get(x); return x+y; } public static void main(String[] args) { A123 solution = new A123(); int[] prices = {7,1,5,3,6,4}; System.out.println(solution.maxProfit(prices)); } }