Skip to content

Commit fe92a20

Browse files
committed
Add solution #1331
1 parent 8b61970 commit fe92a20

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@
230230
1318|[Minimum Flips to Make a OR b Equal to c](./1318-minimum-flips-to-make-a-or-b-equal-to-c.js)|Medium|
231231
1323|[Maximum 69 Number](./1323-maximum-69-number.js)|Easy|
232232
1324|[Print Words Vertically](./1324-print-words-vertically.js)|Medium|
233+
1331|[Rank Transform of an Array](./1331-rank-transform-of-an-array.js)|Easy|
233234
1332|[Remove Palindromic Subsequences](./1332-remove-palindromic-subsequences.js)|Easy|
234235
1333|[Filter Restaurants by Vegan-Friendly, Price and Distance](./1333-filter-restaurants-by-vegan-friendly-price-and-distance.js)|Medium|
235236
1342|[Number of Steps to Reduce a Number to Zero](./1342-number-of-steps-to-reduce-a-number-to-zero.js)|Easy|
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* 1331. Rank Transform of an Array
3+
* https://leetcode.com/problems/rank-transform-of-an-array/
4+
* Difficulty: Easy
5+
*
6+
* Given an array of integers arr, replace each element with its rank.
7+
*
8+
* The rank represents how large the element is. The rank has the following rules:
9+
* - Rank is an integer starting from 1.
10+
* - The larger the element, the larger the rank. If two elements are equal, their
11+
* rank must be the same.
12+
* - Rank should be as small as possible.
13+
*/
14+
15+
/**
16+
* @param {number[]} arr
17+
* @return {number[]}
18+
*/
19+
var arrayRankTransform = function(arr) {
20+
const ranks = [...new Set(arr)]
21+
.sort((a, b) => a - b)
22+
.reduce((map, value, index) => map.set(value, index + 1), new Map());
23+
24+
return arr.map(rank => ranks.get(rank));
25+
};

0 commit comments

Comments
 (0)