Skip to content

Commit

Permalink
feat: add a solution for the daily problem of S0075
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonySchuijlenburg committed Jun 12, 2024
1 parent f589404 commit 45aa732
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Contributions are welcome! If you have a more efficient solution or want to add
| 67 | Add Binary | 1 ms | 42.4 MB | [Problem](src/main/java/com/anthonyschuijlenburg/S0067/Problem.md) |
| 69 | Sqrt(x) | 1 ms | 40.9 MB | [Problem](src/main/java/com/anthonyschuijlenburg/S0069/Problem.md) |
| 70 | Climbing Stairs | 1 ms | 39.8 MB | [Problem](src/main/java/com/anthonyschuijlenburg/S0070/Problem.md) |
| 75 | Sort Colors | 1 ms | 41.5 MB | [Problem](src/main/java/com/anthonyschuijlenburg/S0075/Problem.md) |
| 83 | Remove Duplicates from Sorted List | 0 ms | 43.5 MB | [Problem](src/main/java/com/anthonyschuijlenburg/S0083/Problem.md) |
| 88 | Merge Sorted Array | 0 ms | 41.8 MB | [Problem](src/main/java/com/anthonyschuijlenburg/S0088/Problem.md) |
| 94 | Binary Tree Inorder Traversal | 0 ms | 41.3 MB | [Problem](src/main/java/com/anthonyschuijlenburg/S0094/Problem.md) |
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/anthonyschuijlenburg/S0075/Problem.md
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 src/main/java/com/anthonyschuijlenburg/S0075/Solution.java
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 src/test/java/com/anthonyschuijlenburg/S0075/SolutionTest.java
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);
}
}

0 comments on commit 45aa732

Please sign in to comment.