Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 1331 #998

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
}
Loading