-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solution to "Find the Prefix Common Array of Two Arrays" problem
- Loading branch information
Showing
2 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
src/main/java/com/github/dkoval/leetcode/challenge/FindPrefixCommonArrayOfTwoArrays.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/test/kotlin/com/github/dkoval/leetcode/challenge/FindPrefixCommonArrayOfTwoArraysTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |