From 7206d9c7f9fc889f4fd3ba2bed5dba44cd57c36b Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Wed, 1 Jan 2025 21:10:25 +0800 Subject: [PATCH] Add solution and test-cases for problem 2161 --- .../README.md | 37 ++++++++++++------- .../Solution.go | 21 ++++++++++- .../Solution_test.go | 20 +++++----- 3 files changed, 52 insertions(+), 26 deletions(-) diff --git a/leetcode/2101-2200/2161.Partition-Array-According-to-Given-Pivot/README.md b/leetcode/2101-2200/2161.Partition-Array-According-to-Given-Pivot/README.md index 2cbad6e7a..5287ecf9f 100755 --- a/leetcode/2101-2200/2161.Partition-Array-According-to-Given-Pivot/README.md +++ b/leetcode/2101-2200/2161.Partition-Array-According-to-Given-Pivot/README.md @@ -1,28 +1,37 @@ # [2161.Partition Array According to Given Pivot][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given a **0-indexed** integer array `nums` and an integer `pivot`. Rearrange `nums` such that the following conditions are satisfied: + +- Every element less than `pivot` appears **before** every element greater than `pivot`. +- Every element equal to `pivot` appears **in between** the elements less than and greater than `pivot`. +- The **relative order** of the elements less than `pivot` and the elements greater than `pivot` is maintained. + + - 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. For elements less than `pivot`, if `i < j` and `nums[i] < pivot` and `nums[j] < pivot`, then `pi < pj`. Similarly for elements greater than `pivot`, if `i < j` and `nums[i] > pivot` and `nums[j] > pivot`, then `pi < pj`. + +Return `nums` after the rearrangement. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [9,12,5,10,14,3,10], pivot = 10 +Output: [9,5,3,10,10,12,14] +Explanation: +The elements 9, 5, and 3 are less than the pivot so they are on the left side of the array. +The elements 12 and 14 are greater than the pivot so they are on the right side of the array. +The relative ordering of the elements less than and greater than pivot is also maintained. [9, 5, 3] and [12, 14] are the respective orderings. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Partition Array According to Given Pivot -```go ``` - +Input: nums = [-3,4,3,2], pivot = 2 +Output: [-3,2,4,3] +Explanation: +The element -3 is less than the pivot so it is on the left side of the array. +The elements 4 and 3 are greater than the pivot so they are on the right side of the array. +The relative ordering of the elements less than and greater than pivot is also maintained. [-3] and [4, 3] are the respective orderings. +``` ## 结语 diff --git a/leetcode/2101-2200/2161.Partition-Array-According-to-Given-Pivot/Solution.go b/leetcode/2101-2200/2161.Partition-Array-According-to-Given-Pivot/Solution.go index d115ccf5e..a49ebb713 100644 --- a/leetcode/2101-2200/2161.Partition-Array-According-to-Given-Pivot/Solution.go +++ b/leetcode/2101-2200/2161.Partition-Array-According-to-Given-Pivot/Solution.go @@ -1,5 +1,22 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int, pivot int) []int { + i, j := 0, len(nums)-1 + res := make([]int, len(nums)) + for k := 0; k < len(nums); k++ { + if nums[k] < pivot { + res[i] = nums[k] + i++ + } + } + for k := len(nums) - 1; k >= 0; k-- { + if nums[k] > pivot { + res[j] = nums[k] + j-- + } + } + for ; i <= j; i++ { + res[i] = pivot + } + return res } diff --git a/leetcode/2101-2200/2161.Partition-Array-According-to-Given-Pivot/Solution_test.go b/leetcode/2101-2200/2161.Partition-Array-According-to-Given-Pivot/Solution_test.go index 14ff50eb4..d2636a598 100644 --- a/leetcode/2101-2200/2161.Partition-Array-According-to-Given-Pivot/Solution_test.go +++ b/leetcode/2101-2200/2161.Partition-Array-According-to-Given-Pivot/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + nums []int + pivot int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{9, 12, 5, 10, 14, 3, 10}, 10, []int{9, 5, 3, 10, 10, 12, 14}}, + {"TestCase2", []int{-3, 4, 3, 2}, 2, []int{-3, 2, 4, 3}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.nums, c.pivot) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.nums, c.pivot) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }