From dd82752869693f6b331d36b027182ea7d5284ed6 Mon Sep 17 00:00:00 2001 From: Mrhongxing <154480715+Mrhongxing@users.noreply.github.com> Date: Wed, 14 Oct 2026 00:33:22 +0800 Subject: [PATCH] Create A123.java --- src/com/hong/www/A123.java | 51 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/com/hong/www/A123.java diff --git a/src/com/hong/www/A123.java b/src/com/hong/www/A123.java new file mode 100644 index 0000000..1afac1a --- /dev/null +++ b/src/com/hong/www/A123.java @@ -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 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)); + } +} \ No newline at end of file