Skip to content

Commit ec8b5a8

Browse files
committed
Added code for contest 106
1 parent 8ba3b09 commit ec8b5a8

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public int threeSumMulti(int[] A, int target) {
3+
int div = 1000000007;
4+
int count = 0;
5+
for (int i=0; i<A.length; i++) {
6+
int temp = getPairsCount(A.length, target-A[i], A, i+1);
7+
count = (count+temp)%div;
8+
}
9+
return count;
10+
}
11+
12+
public int getPairsCount(int n, int sum, int[] arr, int start) {
13+
Map<Integer, Integer> map = new HashMap<>();
14+
for (int i=start; i<n; i++) {
15+
map.put(arr[i], map.getOrDefault(arr[i], 0)+1);
16+
}
17+
18+
int count = 0;
19+
for (int i=start; i<n; i++) {
20+
if(map.get(sum-arr[i]) != null) {
21+
count += map.get(sum - arr[i]);
22+
}
23+
24+
if (sum-arr[i] == arr[i]) {
25+
count--;
26+
}
27+
}
28+
29+
return count/2;
30+
}
31+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int minAddToMakeValid(String S) {
3+
Stack<Character> open = new Stack<>();
4+
int count = 0;
5+
6+
for (char c : S.toCharArray()) {
7+
if (c == '(') {
8+
open.push(c);
9+
}
10+
else {
11+
if (open.empty()) {
12+
count++;
13+
}
14+
else {
15+
open.pop();
16+
}
17+
}
18+
}
19+
20+
count += open.size();
21+
22+
return count;
23+
}
24+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public int[] sortArrayByParityII(int[] A) {
3+
int[] ans = new int[A.length];
4+
int[] odd = new int[A.length/2];
5+
int[] even = new int[A.length/2];
6+
7+
int i = 0;
8+
int j = 0;
9+
int k = 0;
10+
11+
while (k < A.length) {
12+
if (A[k]%2 == 0) {
13+
even[j++] = A[k++];
14+
}
15+
else {
16+
odd[i++] = A[k++];
17+
}
18+
}
19+
20+
i = 0;
21+
j = 0;
22+
k = 0;
23+
24+
while (k < ans.length) {
25+
if (k%2 == 0) {
26+
ans[k++] = even[j++];
27+
}
28+
else {
29+
ans[k++] = odd[i++];
30+
}
31+
}
32+
33+
return ans;
34+
}
35+
}

0 commit comments

Comments
 (0)