Skip to content

Commit

Permalink
Merge pull request #998 from 0xff-dev/1331
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 1331
  • Loading branch information
6boris authored Oct 30, 2024
2 parents 8be7d40 + 48698fe commit 441c4ec
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 22 deletions.
34 changes: 21 additions & 13 deletions leetcode/1301-1400/1331.Rank-Transform-of-an-Array/README.md
Original file line number Diff line number Diff line change
@@ -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]
```

## 结语

Expand Down
29 changes: 27 additions & 2 deletions leetcode/1301-1400/1331.Rank-Transform-of-an-Array/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit 441c4ec

Please sign in to comment.