Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e842b21

Browse files
nextcommit
1 parent c27b2cc commit e842b21

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

‎src/arrays/maxProfit.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package arrays;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
6+
7+
public class maxProfit {
8+
9+
public static void profit(int arr[]) {
10+
11+
Arrays.sort(arr);
12+
int k = 0;
13+
HashMap<Integer, Integer> mp = new HashMap<>();
14+
int temp[] = new int[arr.length];
15+
for (int i = 0; i <= arr.length - 3; i++) {
16+
17+
int curr = arr[i + 1];
18+
temp[k++] = curr;
19+
mp.put(curr, i + 1);
20+
21+
22+
}
23+
24+
// for (int i = 0; i < k; i++) {
25+
// System.out.println(temp[i]);
26+
// }
27+
int mx = Integer.MIN_VALUE;
28+
for (int i = 0; i < k - 1; i++) {
29+
30+
int curr =temp[i];
31+
int index = mp.get(curr);
32+
33+
for (int j = i + 1; j < k; j++) {
34+
35+
int next = temp[j];
36+
if(mp.get(next)!=index-1 && mp.get(next)!=index+1){
37+
if((curr+next)>mx){
38+
mx = (curr + next);
39+
}
40+
}
41+
}
42+
43+
44+
}
45+
46+
47+
System.out.println("max"+mx);
48+
49+
50+
51+
}
52+
53+
54+
public static void main(String[] args) {
55+
56+
57+
int arr[] = {1, 2, 3, 4, 5, 6,7,8,9};
58+
int arr2[] = {3, 4, 2};
59+
profit(arr);
60+
}
61+
62+
63+
}

‎src/revision/LongestBracket.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package revision;
2+
3+
import java.util.HashMap;
4+
import java.util.Stack;
5+
6+
public class LongestBracket {
7+
8+
9+
public static void solution(String str) {
10+
long mx = Long.MIN_VALUE;
11+
HashMap<Long, Integer> mp = new HashMap<>();
12+
Stack<Integer> stk = new Stack<>();
13+
14+
stk.push(-1);
15+
for (int i = 0; i < str.length(); i++) {
16+
17+
if (str.charAt(i) == '(') {
18+
stk.push(i);
19+
}
20+
21+
else {
22+
23+
24+
stk.pop();
25+
26+
if (!stk.isEmpty()) {
27+
28+
mx = Math.max(mx, i - stk.peek());
29+
if (mp.containsKey(mx)) {
30+
mp.put(mx, mp.get(mx) + 1);
31+
} else {
32+
mp.put(mx, 1);
33+
}
34+
} else {
35+
36+
stk.push(i);
37+
}
38+
39+
40+
41+
}
42+
43+
44+
}
45+
46+
if (mx > 0) {
47+
48+
System.out.print(mx + " " + mp.get(mx) / 2);
49+
} else {
50+
System.out.print("0 1");
51+
}
52+
}
53+
54+
55+
56+
public static void main(String[] args) {
57+
58+
}
59+
60+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /