From 45aa7325ab500c107022040ec4665a528db59cbc Mon Sep 17 00:00:00 2001 From: AnthonySchuijlenburg Date: Wed, 12 Jun 2024 11:00:16 +0200 Subject: [PATCH] feat: add a solution for the daily problem of S0075 --- README.md | 1 + .../com/anthonyschuijlenburg/S0075/Problem.md | 25 ++++++++ .../anthonyschuijlenburg/S0075/Solution.java | 20 ++++++ .../S0075/SolutionTest.java | 61 +++++++++++++++++++ 4 files changed, 107 insertions(+) create mode 100644 src/main/java/com/anthonyschuijlenburg/S0075/Problem.md create mode 100644 src/main/java/com/anthonyschuijlenburg/S0075/Solution.java create mode 100644 src/test/java/com/anthonyschuijlenburg/S0075/SolutionTest.java diff --git a/README.md b/README.md index a96f274..0735800 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/src/main/java/com/anthonyschuijlenburg/S0075/Problem.md b/src/main/java/com/anthonyschuijlenburg/S0075/Problem.md new file mode 100644 index 0000000..c96b2c7 --- /dev/null +++ b/src/main/java/com/anthonyschuijlenburg/S0075/Problem.md @@ -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. diff --git a/src/main/java/com/anthonyschuijlenburg/S0075/Solution.java b/src/main/java/com/anthonyschuijlenburg/S0075/Solution.java new file mode 100644 index 0000000..0a7c39b --- /dev/null +++ b/src/main/java/com/anthonyschuijlenburg/S0075/Solution.java @@ -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 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); + } + } +} diff --git a/src/test/java/com/anthonyschuijlenburg/S0075/SolutionTest.java b/src/test/java/com/anthonyschuijlenburg/S0075/SolutionTest.java new file mode 100644 index 0000000..76d9628 --- /dev/null +++ b/src/test/java/com/anthonyschuijlenburg/S0075/SolutionTest.java @@ -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); + } +} \ No newline at end of file