From 48698fe7420f85681195041380890b3afb82c153 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Wed, 2 Oct 2024 08:59:04 +0800 Subject: [PATCH] Add solution and test-cases for problem 1331 --- .../1331.Rank-Transform-of-an-Array/README.md | 34 ++++++++++++------- .../Solution.go | 29 ++++++++++++++-- .../Solution_test.go | 14 ++++---- 3 files changed, 55 insertions(+), 22 deletions(-) diff --git a/leetcode/1301-1400/1331.Rank-Transform-of-an-Array/README.md b/leetcode/1301-1400/1331.Rank-Transform-of-an-Array/README.md index 49bb1463e..2654c3ad0 100644 --- a/leetcode/1301-1400/1331.Rank-Transform-of-an-Array/README.md +++ b/leetcode/1301-1400/1331.Rank-Transform-of-an-Array/README.md @@ -1,28 +1,36 @@ # [1331.Rank Transform of an Array][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 +Given an array of integers `arr`, replace each element with its rank. + +The rank represents how large the element is. The rank has the following rules: + +- Rank is an integer starting from 1. +- The larger the element, the larger the rank. If two elements are equal, their rank must be the same. +- Rank should be as small as possible. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: arr = [40,10,20,30] +Output: [4,1,2,3] +Explanation: 40 is the largest element. 10 is the smallest. 20 is the second smallest. 30 is the third smallest. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Rank Transform of an Array -```go ``` +Input: arr = [100,100,100] +Output: [1,1,1] +Explanation: Same elements share the same rank. +``` + +**Example 3:** +``` +Input: arr = [37,12,28,9,100,56,80,5,12] +Output: [5,3,4,2,8,6,7,1,3] +``` ## 结语 diff --git a/leetcode/1301-1400/1331.Rank-Transform-of-an-Array/Solution.go b/leetcode/1301-1400/1331.Rank-Transform-of-an-Array/Solution.go index d115ccf5e..44a1797bd 100644 --- a/leetcode/1301-1400/1331.Rank-Transform-of-an-Array/Solution.go +++ b/leetcode/1301-1400/1331.Rank-Transform-of-an-Array/Solution.go @@ -1,5 +1,30 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +type index [][2]int + +func Solution(arr []int) []int { + l := len(arr) + ans := make([]int, l) + if l == 0 { + return ans + } + slices := make(index, l) + for i := range l { + slices[i] = [2]int{i, arr[i]} + } + sort.Slice(slices, func(i, j int) bool { + return slices[i][1] < slices[j][1] + }) + + rank := 1 + ans[slices[0][0]] = rank + for i := 1; i < l; i++ { + if slices[i][1] != slices[i-1][1] { + rank++ + } + ans[slices[i][0]] = rank + } + return ans } diff --git a/leetcode/1301-1400/1331.Rank-Transform-of-an-Array/Solution_test.go b/leetcode/1301-1400/1331.Rank-Transform-of-an-Array/Solution_test.go index 14ff50eb4..54130d4c3 100644 --- a/leetcode/1301-1400/1331.Rank-Transform-of-an-Array/Solution_test.go +++ b/leetcode/1301-1400/1331.Rank-Transform-of-an-Array/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{40, 10, 20, 30}, []int{4, 1, 2, 3}}, + {"TestCase2", []int{100, 100, 100}, []int{1, 1, 1}}, + {"TestCase3", []int{37, 12, 28, 9, 100, 56, 80, 5, 12}, []int{5, 3, 4, 2, 8, 6, 7, 1, 3}}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }