Skip to content

[Hacker Rank] Interview Preparation Kit: Arrays: Array Manipulation. … #215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package ae.hackerrank.interview_preparation_kit.arrays;

import java.util.Arrays;
import java.util.List;


/**
* Crush (Brute Force).
*
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/crush.md]]
* @link Solution notes [[docs/hackerrank/interview_preparation_kit/arrays/crush_optimized-solution-notes.md]]
*/
public class CrushBruteForce {

private CrushBruteForce() {
}

static java.util.logging.Logger logger = util.CustomLogger.getLogger();

private static final int INITIALIZER = 0;

/**
* arrayManipulation.
*/
public static long arrayManipulation(int n, List<List<Integer>> queries) {
// why adding 1?
// first slot to adjust 1-based index and
int[] result = new int[n + 1];
Arrays.fill(result, INITIALIZER);
int maximum = INITIALIZER;

for (List<Integer> query : queries) {
int start = query.get(0);
int end = query.get(1);
int value = query.get(2);

for (int i = start; i < end + 1; i++) {
result[i] += value;
}

for (int current : result) {
maximum = Math.max(current, maximum);
}
}

return maximum;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ae.hackerrank.interview_preparation_kit.arrays;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;


@TestInstance(Lifecycle.PER_CLASS)
class CrushBruteForceTest {

public static class CrushBruteForceTestCase {
public String title;
public Integer n;
public List<List<Integer>> queries;
public long expected;
}

List<CrushBruteForceTestCase> testCases;

@BeforeAll
public void setup() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();

String path = String.join("/", "hackerrank",
"interview_preparation_kit",
"arrays",
"crush.testcases.json");
File file = new File(
this.getClass()
.getClassLoader()
.getResource(path)
.getFile()
);

ObjectMapper mapper = new ObjectMapper();
this.testCases = mapper.readerForListOf(CrushBruteForceTestCase.class)
.readValue(objectMapper.readTree(file));
}

@Test void testArrayManipulation() {
for (CrushBruteForceTestCase testCase : testCases) {
long solutionFound = CrushBruteForce
.arrayManipulation(testCase.n, testCase.queries);

assertEquals(testCase.expected, solutionFound,
String.format("%s(%d, %s) answer must be: %s",
"CrushBruteForce.arrayManipulation",
testCase.n,
testCase.queries.toString(),
testCase.expected
)
);
}
}
}