Create A123.java
This commit is contained in:
1 parent
5d14218049
commit
dd82752869
1 file changed
+51
@@ -0,0 +1,51 @@
|
||||
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<Integer> temp1=new ArrayList<>();
|
||||
* ArrayList<Integer> temp2=new ArrayList<>();
|
||||
* int up=0;
|
||||
* for(int x=1;x<prices.length;x++){
|
||||
* if(prices[x]>temp&&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<Integer,Integer> 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));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user