Skip to content

Commit 28ddb44

Browse files
authored
Merge pull request #215 from sir-gon/feature/crush
[Hacker Rank] Interview Preparation Kit: Arrays: Array Manipulation. …
2 parents e1c755d + c047bbc commit 28ddb44

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package ae.hackerrank.interview_preparation_kit.arrays;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
7+
/**
8+
* Crush (Brute Force).
9+
*
10+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/crush.md]]
11+
* @link Solution notes [[docs/hackerrank/interview_preparation_kit/arrays/crush_optimized-solution-notes.md]]
12+
*/
13+
public class CrushBruteForce {
14+
15+
private CrushBruteForce() {
16+
}
17+
18+
static java.util.logging.Logger logger = util.CustomLogger.getLogger();
19+
20+
private static final int INITIALIZER = 0;
21+
22+
/**
23+
* arrayManipulation.
24+
*/
25+
public static long arrayManipulation(int n, List<List<Integer>> queries) {
26+
// why adding 1?
27+
// first slot to adjust 1-based index and
28+
int[] result = new int[n + 1];
29+
Arrays.fill(result, INITIALIZER);
30+
int maximum = INITIALIZER;
31+
32+
for (List<Integer> query : queries) {
33+
int start = query.get(0);
34+
int end = query.get(1);
35+
int value = query.get(2);
36+
37+
for (int i = start; i < end + 1; i++) {
38+
result[i] += value;
39+
}
40+
41+
for (int current : result) {
42+
maximum = Math.max(current, maximum);
43+
}
44+
}
45+
46+
return maximum;
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package ae.hackerrank.interview_preparation_kit.arrays;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.util.List;
9+
import org.junit.jupiter.api.BeforeAll;
10+
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.api.TestInstance;
12+
import org.junit.jupiter.api.TestInstance.Lifecycle;
13+
14+
15+
@TestInstance(Lifecycle.PER_CLASS)
16+
class CrushBruteForceTest {
17+
18+
public static class CrushBruteForceTestCase {
19+
public String title;
20+
public Integer n;
21+
public List<List<Integer>> queries;
22+
public long expected;
23+
}
24+
25+
List<CrushBruteForceTestCase> testCases;
26+
27+
@BeforeAll
28+
public void setup() throws IOException {
29+
ObjectMapper objectMapper = new ObjectMapper();
30+
31+
String path = String.join("/", "hackerrank",
32+
"interview_preparation_kit",
33+
"arrays",
34+
"crush.testcases.json");
35+
File file = new File(
36+
this.getClass()
37+
.getClassLoader()
38+
.getResource(path)
39+
.getFile()
40+
);
41+
42+
ObjectMapper mapper = new ObjectMapper();
43+
this.testCases = mapper.readerForListOf(CrushBruteForceTestCase.class)
44+
.readValue(objectMapper.readTree(file));
45+
}
46+
47+
@Test void testArrayManipulation() {
48+
for (CrushBruteForceTestCase testCase : testCases) {
49+
long solutionFound = CrushBruteForce
50+
.arrayManipulation(testCase.n, testCase.queries);
51+
52+
assertEquals(testCase.expected, solutionFound,
53+
String.format("%s(%d, %s) answer must be: %s",
54+
"CrushBruteForce.arrayManipulation",
55+
testCase.n,
56+
testCase.queries.toString(),
57+
testCase.expected
58+
)
59+
);
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)