Skip to content

Commit ae42dd5

Browse files
committed
Add HackerRank: 2D Array - DS
1 parent e8d854e commit ae42dd5

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@ hs_err_pid*
2424

2525
# IntelliJ
2626
**/.idea/*
27-
*.iml
27+
*.iml
28+
29+
# Compiled
30+
target/*

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ HackerRank assigns their problems a difficulty rating (Easy, Medium, Hard, Exper
2121

2222
### Arrays
2323

24+
- 2D Array - DS (Easy) [Problem](https://www.hackerrank.com/challenges/2d-array/problem) | [Solution](https://github.com/aykrieger/coding-exercises/blob/master/java/src/hackerrank/arrays/TwoDArrayDS.java)
25+
2426
- Minimum Swaps 2 (Medium) [Problem](https://www.hackerrank.com/challenges/minimum-swaps-2/problem) | [Solution](https://github.com/aykrieger/coding-exercises/blob/master/java/src/hackerrank/arrays/MinimumSwaps2.java)
2527

2628
### Greedy Algorithms
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package hackerrank.arrays;
2+
3+
import static java.util.stream.Collectors.toList;
4+
5+
import java.io.BufferedReader;
6+
import java.io.BufferedWriter;
7+
import java.io.FileWriter;
8+
import java.io.IOException;
9+
import java.io.InputStreamReader;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
import java.util.stream.IntStream;
13+
import java.util.stream.Stream;
14+
15+
/**
16+
* @see <a href="https://www.hackerrank.com/challenges/2d-array/problem">2D Array - DS</a>
17+
*/
18+
public class TwoDArrayDS {
19+
20+
/*
21+
* Complete the 'hourglassSum' function below.
22+
*
23+
* The function is expected to return an INTEGER.
24+
* The function accepts 2D_INTEGER_ARRAY arr as parameter.
25+
*/
26+
27+
public static int hourglassSum(List<List<Integer>> arr) {
28+
Integer largestSum = null;
29+
30+
for (int x = 0; x < 4; x++) {
31+
for (int y = 0; y < 4; y++) {
32+
// Here we begin calculating the sum of a single hourglass
33+
int currentSum = 0;
34+
for (int i = 0; i < 3; i++) {
35+
for (int j = 0; j < 3; j++) {
36+
if (i == 1 && (j == 0 || j == 2)) {
37+
continue;
38+
}
39+
40+
currentSum = currentSum + arr.get(x + i).get(y + j);
41+
}
42+
}
43+
if (largestSum == null) {
44+
largestSum = currentSum;
45+
}
46+
if (currentSum > largestSum) {
47+
largestSum = currentSum;
48+
}
49+
}
50+
}
51+
return largestSum;
52+
}
53+
54+
public static void main(String[] args) throws IOException {
55+
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
56+
BufferedWriter bufferedWriter = new BufferedWriter(
57+
new FileWriter(System.getenv("OUTPUT_PATH")));
58+
59+
List<List<Integer>> arr = new ArrayList<>();
60+
61+
IntStream.range(0, 6).forEach(i -> {
62+
try {
63+
arr.add(
64+
Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
65+
.map(Integer::parseInt)
66+
.collect(toList())
67+
);
68+
} catch (IOException ex) {
69+
throw new RuntimeException(ex);
70+
}
71+
});
72+
73+
int result = hourglassSum(arr);
74+
75+
bufferedWriter.write(String.valueOf(result));
76+
bufferedWriter.newLine();
77+
78+
bufferedReader.close();
79+
bufferedWriter.close();
80+
}
81+
}

0 commit comments

Comments
 (0)