-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArraysCC_2.java
More file actions
112 lines (97 loc) · 3.32 KB
/
Copy pathArraysCC_2.java
File metadata and controls
112 lines (97 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
public class ArraysCC_2 {
public static void maxSubArraySum(int numbers[]) {
int maxSum = Integer.MIN_VALUE;
for (int i = 0; i < numbers.length; i++) {
int start = i;
for (int j = i; j < numbers.length; j++) {
int end = j;
int currSum = 0;
for (int k = start; k <= end; k++) {
currSum += numbers[k];
}
System.out.println(currSum);
if (maxSum < currSum) {
maxSum = currSum;
}
}
}
System.out.println("Max Sum = " + maxSum);
}
public static void PrefixSubArraySum(int numbers[]) {
int maxSum = Integer.MIN_VALUE;
int prefix[] = new int[numbers.length];
prefix[0] = numbers[0];
// Calculate Prefix Array
for (int i = 1; i < prefix.length; i++) {
prefix[i] = prefix[i - 1] + numbers[i];
}
for (int i = 0; i < numbers.length; i++) {
int start = i;
for (int j = i; j < numbers.length; j++) {
int end = j;
int currSum = start == 0 ? prefix[end] : prefix[end] - (prefix[start - 1]);
if (maxSum < currSum) {
maxSum = currSum;
}
}
}
System.out.println("Max Sum = " + maxSum);
}
public static void Kadans(int numbers[]) {
int ms = Integer.MIN_VALUE;
int cs = 0;
for (int i = 0; i < numbers.length; i++) {
cs += numbers[i];
if (cs < 0) {
cs = 0;
}
ms = Math.max(cs, ms);
}
System.out.println("Max Subarray Sum is " + ms);
}
public static int RainWater(int height[]) {
int n = height.length;
// calculate leftmax boundary - array
int leftmax[] = new int[n];
leftmax[0] = height[0];
for (int i = 1; i < n; i++) {
leftmax[i] = Math.max(height[i], leftmax[i - 1]);
}
// calculate rightmax boundary - array
int rightmax[] = new int[n];
rightmax[n - 1] = height[n - 1];
for (int i = n - 2; i >= 0; i--) {
rightmax[i] = Math.max(height[i], rightmax[i + 1]);
}
int trappedWater = 0;
// loop
for (int i = 0; i < n; i++) {
int waterLevel = Math.min(leftmax[i], rightmax[i]);
trappedWater += waterLevel - height[i];
}
return trappedWater;
}
public static int buyandSellStocks(int price[]) {
int buyPrice = Integer.MAX_VALUE;
int maxProfit = 0;
for (int i = 0; i < price.length; i++) {
if (buyPrice < price[i]) { // profit
int profit = price[i] - buyPrice; // todays profit
maxProfit = Math.max(maxProfit, profit); // overall max profit
} else {
buyPrice = price[i];
}
}
return maxProfit;
}
public static void main(String[] args) {
/*
* int numbers[] = { 1, -2, 6, -1, 3 };
* Kadans(numbers);
* int Height[] = { 4, 2, 0, 6, 3, 2, 5 };
* System.out.println(RainWater(Height));
*/
int price[] = { 7, 1, 5, 3, 6, 4 };
System.out.println(buyandSellStocks(price));
}
}