Skip to content

Commit

Permalink
Solution to "Find the Prefix Common Array of Two Arrays" problem
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoval committed Jan 14, 2025
1 parent 7fa0063 commit da581c2
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.github.dkoval.leetcode.challenge;

import java.util.HashSet;

/**
* <a href="https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/">Find the Prefix Common Array of Two Arrays</a>
* <p>
* You are given two 0-indexed integer permutations A and B of length n.
* <p>
* A prefix common array of A and B is an array C such that C[i] is equal to the count of numbers that are present at
* or before the index i in both A and B.
* <p>
* Return the prefix common array of A and B.
* <p>
* A sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once.
* <p>
* Constraints:
* <ul>
* <li>1 <= A.length == B.length == n <= 50</li>
* <li>1 <= A[i], B[i] <= n</li>
* <li>It is guaranteed that A and B are both a permutation of n integers.</li>
* </ul>
*/
public interface FindPrefixCommonArrayOfTwoArrays {

int[] findThePrefixCommonArray(int[] A, int[] B);

// O(N) time | O(N) space
class FindPrefixCommonArrayOfTwoArraysRev1 implements FindPrefixCommonArrayOfTwoArrays {

@Override
public int[] findThePrefixCommonArray(int[] A, int[] B) {
final var n = A.length;

final var ans = new int[n];
final var as = new HashSet<Integer>();
final var bs = new HashSet<Integer>();
var common = 0;
for (var i = 0; i < n; i++) {
as.add(A[i]);
bs.add(B[i]);

if (as.contains(B[i])) {
common++;
}

if (bs.contains(A[i])) {
common++;
}

// fix double counting
if (A[i] == B[i]) {
common--;
}

ans[i] = common;
}
return ans;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.github.dkoval.leetcode.challenge

import com.github.dkoval.leetcode.challenge.FindPrefixCommonArrayOfTwoArrays.FindPrefixCommonArrayOfTwoArraysRev1
import org.junit.jupiter.api.Assertions.assertArrayEquals
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.extension.ExtensionContext
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.ArgumentsProvider
import org.junit.jupiter.params.provider.ArgumentsSource
import java.util.stream.Stream

internal class FindPrefixCommonArrayOfTwoArraysTest {

class InputArgumentsProvider : ArgumentsProvider {

override fun provideArguments(context: ExtensionContext): Stream<out Arguments> = Stream.of(
Arguments.of(
intArrayOf(1, 3, 2, 4),
intArrayOf(3, 1, 2, 4),
intArrayOf(0, 2, 3, 4)
),
Arguments.of(
intArrayOf(2, 3, 1),
intArrayOf(3, 1, 2),
intArrayOf(0, 1, 3)
)
)
}

@Nested
inner class FindPrefixCommonArrayOfTwoArraysRev1Test {

@ParameterizedTest
@ArgumentsSource(InputArgumentsProvider::class)
fun `should return the prefix common array of A and B`(A: IntArray, B: IntArray, expected: IntArray) {
FindPrefixCommonArrayOfTwoArraysRev1().test(A, B, expected)
}
}
}

private fun FindPrefixCommonArrayOfTwoArrays.test(A: IntArray, B: IntArray, expected: IntArray) {
val actual = findThePrefixCommonArray(A, B)
assertArrayEquals(expected, actual)
}

0 comments on commit da581c2

Please sign in to comment.