-
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 "Merge Two 2D Arrays by Summing Values" problem
- Loading branch information
Showing
2 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/main/java/com/github/dkoval/leetcode/challenge/PartitionArrayAccordingToGivenPivot.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,63 @@ | ||
package com.github.dkoval.leetcode.challenge; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* <a href="https://leetcode.com/problems/partition-array-according-to-given-pivot/">Partition Array According to Given Pivot</a> | ||
* <p> | ||
* You are given a 0-indexed integer array nums and an integer pivot. Rearrange nums such that the following conditions are satisfied: | ||
* <ul> | ||
* <li>Every element less than pivot appears before every element greater than pivot.</li> | ||
* <li>Every element equal to pivot appears in between the elements less than and greater than pivot.</li> | ||
* <li>The relative order of the elements less than pivot and the elements greater than pivot is maintained.</li> | ||
* </ul> | ||
* More formally, consider every pi, pj where pi is the new position of the ith element and pj is the new position of the jth element. | ||
* If i < j and both elements are smaller (or larger) than pivot, then pi < pj. | ||
* <p> | ||
* Return nums after the rearrangement. | ||
* <p> | ||
* Constraints: | ||
* <ul> | ||
* <li>1 <= nums.length <= 10^5</li> | ||
* <li>-106 <= nums[i] <= 10^6</li> | ||
* <li>pivot equals to an element of nums</li> | ||
* </ul> | ||
*/ | ||
public interface PartitionArrayAccordingToGivenPivot { | ||
|
||
int[] pivotArray(int[] nums, int pivot); | ||
|
||
// O(N) time | O(N) space | ||
class PartitionArrayAccordingToGivenPivotRev1 implements PartitionArrayAccordingToGivenPivot { | ||
|
||
@Override | ||
public int[] pivotArray(int[] nums, int pivot) { | ||
final var n = nums.length; | ||
|
||
final var ans = new int[n]; | ||
var i = 0; | ||
|
||
final var greater = new ArrayList<Integer>(); | ||
var same = 0; | ||
for (var x : nums) { | ||
if (x < pivot) { | ||
ans[i++] = x; | ||
} else if (x > pivot) { | ||
greater.add(x); | ||
} else { | ||
same++; | ||
} | ||
} | ||
|
||
while (same-- > 0) { | ||
ans[i++] = pivot; | ||
} | ||
|
||
for (var x : greater) { | ||
ans[i++] = x; | ||
} | ||
|
||
return ans; | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...st/kotlin/com/github/dkoval/leetcode/challenge/PartitionArrayAccordingToGivenPivotTest.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,49 @@ | ||
package com.github.dkoval.leetcode.challenge | ||
|
||
import com.github.dkoval.leetcode.challenge.PartitionArrayAccordingToGivenPivot.PartitionArrayAccordingToGivenPivotRev1 | ||
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 PartitionArrayAccordingToGivenPivotTest { | ||
|
||
class InputArgumentsProvider : ArgumentsProvider { | ||
|
||
override fun provideArguments(context: ExtensionContext): Stream<out Arguments> = Stream.of( | ||
Arguments.of( | ||
intArrayOf(9, 12, 5, 10, 14, 3, 10), | ||
10, | ||
intArrayOf(9, 5, 3, 10, 10, 12, 14) | ||
), | ||
Arguments.of( | ||
intArrayOf(-3, 4, 3, 2), | ||
2, | ||
intArrayOf(-3, 2, 4, 3) | ||
) | ||
) | ||
} | ||
|
||
@Nested | ||
inner class PartitionArrayAccordingToGivenPivotRev1Test { | ||
|
||
@ParameterizedTest | ||
@ArgumentsSource(InputArgumentsProvider::class) | ||
fun `should partition the array according to the given pivot value`( | ||
nums: IntArray, | ||
pivot: Int, | ||
expected: IntArray | ||
) { | ||
PartitionArrayAccordingToGivenPivotRev1().test(nums, pivot, expected) | ||
} | ||
} | ||
} | ||
|
||
private fun PartitionArrayAccordingToGivenPivot.test(nums: IntArray, pivot: Int, expected: IntArray) { | ||
val actual = pivotArray(nums, pivot) | ||
assertArrayEquals(expected, actual) | ||
} |