Skip to content

Commit fe13be7

Browse files
committed
update algorithm
1 parent f76458e commit fe13be7

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed
Binary file not shown.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package easy.array;
2+
3+
/**
4+
*描述 : 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
5+
* 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。
6+
* 注意你不能在买入股票前卖出股票。
7+
*
8+
*示例1: 输入: [7,1,5,3,6,4]
9+
* 输出: 5
10+
* 解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
11+
* 注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格。
12+
*
13+
*示例2 输入: [7,6,4,3,1]
14+
* 输出: 0
15+
* 解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
16+
*/
17+
18+
public class _121_BestTimeToBuyAndSellStock {
19+
20+
/**
21+
*
22+
* 直接暴力求解,遍历两次数组,求出最优解
23+
*/
24+
25+
public static int violence(int[] prices){
26+
if(prices == null || prices.length ==0){
27+
return 0;
28+
}
29+
int maxProfit = 0;
30+
for (int i = 0; i < prices.length; i++) {
31+
for (int j = i+1; j < prices.length; j++) {
32+
if(prices[j]-prices[i] >maxProfit){
33+
maxProfit = prices[j]-prices[i];
34+
}
35+
36+
}
37+
}
38+
return maxProfit;
39+
}
40+
41+
42+
/**
43+
* 动态规划:
44+
* 1.定义一个从最开始到当前位置的最小值
45+
* 2.计算今天减去最小值的值,也就是当天的利润
46+
* 3.比较当天的利润和之前的最大利润
47+
*/
48+
public static int dp(int[] prices){
49+
if(prices == null || prices.length == 0)
50+
return 0;
51+
52+
int mixProfit = Integer.MAX_VALUE;
53+
int maxProfit = 0;
54+
55+
for (int i = 0; i < prices.length; i++) {
56+
//找到最小值
57+
mixProfit = Math.min(mixProfit,prices[i]);
58+
//比较当天的利润和最大利润
59+
maxProfit = Math.max(maxProfit,(prices[i]-mixProfit));
60+
61+
}
62+
return maxProfit;
63+
64+
}
65+
66+
public static void main(String[] args) {
67+
int a[] ={7,6,4,3,1};
68+
System.out.println(violence(a) == dp(a));
69+
}
70+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package easy.array;
2+
3+
/**
4+
* 描述: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
5+
* 设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。
6+
* 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
7+
*
8+
* 示例1: 输入: [7,1,5,3,6,4]
9+
* 输出: 7
10+
* 解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
11+
* 随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。
12+
* 示例2: 输入: [1,2,3,4,5]
13+
* 输出: 4
14+
* 解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
15+
* 注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。
16+
* 因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
17+
*
18+
* 思路:使用两个变量 一个valley表示当天天数中最低价,
19+
* 一个peek表示当前所有的天数中的最高价
20+
* 因为我们可以买卖股票且是不用付手续费的,也就是我们可以卖了这一只股票,然后再把它买回来
21+
* 所以上面示例2中,5-1和 (2-1)+(3-2)+(4-3)+(5-4)的效果是一样的,所以顶峰就找递增的,底就找递减的
22+
*
23+
*/
24+
public class _122_BestTimeToBuyAndSellStock2 {
25+
public int maxProfit(int[] prices) {
26+
if(prices.length == 0 || prices == null)
27+
return 0;
28+
int valley = prices[0],peek = prices[0];
29+
int i =0;
30+
int maxProfit = 0;
31+
while(i < prices.length-1){
32+
while(i<prices.length-1 && prices[i]>=prices[i+1])
33+
i++;
34+
valley = prices[i];
35+
while(i<prices.length-1 && prices[i]<=prices[i+1])
36+
i++;
37+
peek = prices[i];
38+
maxProfit += peek-valley;
39+
}
40+
41+
return maxProfit;
42+
}
43+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package easy.array;
2+
3+
/**描述:
4+
* Given a non-empty array of integers, every element appears twice except for one. Find that single one.
5+
* 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
6+
* 说明:
7+
* 你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?
8+
* 输入: [4,1,2,1,2]
9+
* 输出: 4
10+
*
11+
*
12+
* 思路:使用异或运算:
13+
* 异或运算有三个特征:
14+
* 1、0 ^ 任何数都为 那个数 0^a = a;
15+
* 2、两个相同的数异或结果为 0 a^a = 0;
16+
* 3、异或具有交换律 a^b^c = a^c^b
17+
*
18+
*/
19+
public class _136_singleNum {
20+
public static void main(String[] args) {
21+
int[] nums = {4,2,1,2,1};
22+
System.out.println("singleNum(nums) = " + singleNum(nums));
23+
24+
}
25+
26+
public static int singleNum(int[] nums){
27+
int result = 0;
28+
for (int num :
29+
nums) {
30+
result ^=num;
31+
}
32+
return result;
33+
}
34+
}

0 commit comments

Comments
 (0)