-
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.
feat: add a solution for the daily problem of S0075
- Loading branch information
1 parent
f589404
commit 45aa732
Showing
4 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
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
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,25 @@ | ||
# 75. Sort Colors | ||
|
||
Topics: Array, Two Pointers, Sorting | ||
|
||
Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue. | ||
|
||
We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively. | ||
|
||
You must solve this problem without using the library's sort function. | ||
|
||
### Example 1: | ||
|
||
Input: nums = [2,0,2,1,1,0] | ||
Output: [0,0,1,1,2,2] | ||
|
||
### Example 2: | ||
|
||
Input: nums = [2,0,1] | ||
Output: [0,1,2] | ||
|
||
### Constraints: | ||
|
||
n == nums.length | ||
1 <= n <= 300 | ||
nums[i] is either 0, 1, or 2. |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/anthonyschuijlenburg/S0075/Solution.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,20 @@ | ||
package com.anthonyschuijlenburg.S0075; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
|
||
public class Solution { | ||
public void sortColors(int[] nums) { | ||
HashMap<Integer, Integer> map = new HashMap<>(); | ||
|
||
for (int num : nums) { | ||
map.put(num, map.getOrDefault(num, 0) + 1); | ||
} | ||
|
||
int counter = 0; | ||
for (int key : map.keySet()) { | ||
Arrays.fill(nums, counter, counter + map.get(key), key); | ||
counter += map.get(key); | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/test/java/com/anthonyschuijlenburg/S0075/SolutionTest.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.anthonyschuijlenburg.S0075; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class SolutionTest { | ||
@Test | ||
void climbStairs_testCase0() { | ||
Solution solutionGenerator = new Solution(); | ||
int[] arr = new int[]{0}; | ||
solutionGenerator.sortColors(arr); | ||
|
||
assertArrayEquals(new int[]{0}, arr); | ||
} | ||
|
||
@Test | ||
void climbStairs_testCase1() { | ||
Solution solutionGenerator = new Solution(); | ||
int[] arr = new int[]{1}; | ||
solutionGenerator.sortColors(arr); | ||
|
||
assertArrayEquals(new int[]{1}, arr); | ||
} | ||
|
||
@Test | ||
void climbStairs_testCase2() { | ||
Solution solutionGenerator = new Solution(); | ||
int[] arr = new int[]{0, 1, 2}; | ||
solutionGenerator.sortColors(arr); | ||
|
||
assertArrayEquals(new int[]{0, 1, 2}, arr); | ||
} | ||
|
||
@Test | ||
void climbStairs_testCase3() { | ||
Solution solutionGenerator = new Solution(); | ||
int[] arr = new int[]{2, 1, 0}; | ||
solutionGenerator.sortColors(arr); | ||
|
||
assertArrayEquals(new int[]{0, 1, 2}, arr); | ||
} | ||
|
||
@Test | ||
void climbStairs_testCase4() { | ||
Solution solutionGenerator = new Solution(); | ||
int[] arr = new int[]{2, 0, 2, 1, 1, 0}; | ||
solutionGenerator.sortColors(arr); | ||
|
||
assertArrayEquals(new int[]{0, 0, 1, 1, 2, 2}, arr); | ||
} | ||
|
||
@Test | ||
void climbStairs_testCase5() { | ||
Solution solutionGenerator = new Solution(); | ||
int[] arr = new int[]{2, 0, 1}; | ||
solutionGenerator.sortColors(arr); | ||
|
||
assertArrayEquals(new int[]{0, 1, 2}, arr); | ||
} | ||
} |