Skip to content

Commit 27880b3

Browse files
committed
feat: add solutions to lc problem: No.1331
No.1331.Rank Transform of an Array
1 parent 03deb8c commit 27880b3

File tree

3 files changed

+73
-23
lines changed

3 files changed

+73
-23
lines changed

solution/1300-1399/1331.Rank Transform of an Array/README.md

+35-12
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53-
离散化
53+
**方法一:离散化**
5454

5555
<!-- tabs:start -->
5656

@@ -61,18 +61,8 @@
6161
```python
6262
class Solution:
6363
def arrayRankTransform(self, arr: List[int]) -> List[int]:
64-
def find(x):
65-
left, right = 0, len(t) - 1
66-
while left < right:
67-
mid = (left + right) >> 1
68-
if t[mid] >= x:
69-
right = mid
70-
else:
71-
left = mid + 1
72-
return left + 1
73-
7464
t = sorted(set(arr))
75-
return [find(x) for x in arr]
65+
return [bisect_left(t, x) + 1 for x in arr]
7666
```
7767

7868
```python
@@ -108,6 +98,25 @@ class Solution {
10898
}
10999
```
110100

101+
```java
102+
class Solution {
103+
public int[] arrayRankTransform(int[] arr) {
104+
Set<Integer> s = new HashSet<>();
105+
for (int v : arr) {
106+
s.add(v);
107+
}
108+
List<Integer> alls = new ArrayList<>(s);
109+
alls.sort((a, b) -> a - b);
110+
int n = arr.length;
111+
int[] ans = new int[n];
112+
for (int i = 0; i < n; ++i) {
113+
ans[i] = Collections.binarySearch(alls, arr[i]) + 1;
114+
}
115+
return ans;
116+
}
117+
}
118+
```
119+
111120
### **C++**
112121

113122
```cpp
@@ -126,6 +135,20 @@ public:
126135
};
127136
```
128137
138+
```cpp
139+
class Solution {
140+
public:
141+
vector<int> arrayRankTransform(vector<int>& arr) {
142+
unordered_set<int> s(arr.begin(), arr.end());
143+
vector<int> alls(s.begin(), s.end());
144+
sort(alls.begin(), alls.end());
145+
vector<int> ans;
146+
for (int v: arr) ans.push_back(lower_bound(alls.begin(), alls.end(), v) - alls.begin() + 1);
147+
return ans;
148+
}
149+
};
150+
```
151+
129152
### **Go**
130153

131154
```go

solution/1300-1399/1331.Rank Transform of an Array/README_EN.md

+34-11
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,8 @@
5454
```python
5555
class Solution:
5656
def arrayRankTransform(self, arr: List[int]) -> List[int]:
57-
def find(x):
58-
left, right = 0, len(t) - 1
59-
while left < right:
60-
mid = (left + right) >> 1
61-
if t[mid] >= x:
62-
right = mid
63-
else:
64-
left = mid + 1
65-
return left + 1
66-
6757
t = sorted(set(arr))
68-
return [find(x) for x in arr]
58+
return [bisect_left(t, x) + 1 for x in arr]
6959
```
7060

7161
```python
@@ -99,6 +89,25 @@ class Solution {
9989
}
10090
```
10191

92+
```java
93+
class Solution {
94+
public int[] arrayRankTransform(int[] arr) {
95+
Set<Integer> s = new HashSet<>();
96+
for (int v : arr) {
97+
s.add(v);
98+
}
99+
List<Integer> alls = new ArrayList<>(s);
100+
alls.sort((a, b) -> a - b);
101+
int n = arr.length;
102+
int[] ans = new int[n];
103+
for (int i = 0; i < n; ++i) {
104+
ans[i] = Collections.binarySearch(alls, arr[i]) + 1;
105+
}
106+
return ans;
107+
}
108+
}
109+
```
110+
102111
### **C++**
103112

104113
```cpp
@@ -117,6 +126,20 @@ public:
117126
};
118127
```
119128
129+
```cpp
130+
class Solution {
131+
public:
132+
vector<int> arrayRankTransform(vector<int>& arr) {
133+
unordered_set<int> s(arr.begin(), arr.end());
134+
vector<int> alls(s.begin(), s.end());
135+
sort(alls.begin(), alls.end());
136+
vector<int> ans;
137+
for (int v: arr) ans.push_back(lower_bound(alls.begin(), alls.end(), v) - alls.begin() + 1);
138+
return ans;
139+
}
140+
};
141+
```
142+
120143
### **Go**
121144

122145
```go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def arrayRankTransform(self, arr: List[int]) -> List[int]:
3+
m = {v: i for i, v in enumerate(sorted(set(arr)), 1)}
4+
return [m[v] for v in arr]

0 commit comments

Comments
 (0)