8000 refactor 506 · jayawadhwani/Leetcode@ba4b6b1 · GitHub
[go: up one dir, main page]

Skip to content

Commit ba4b6b1

Browse files
refactor 506
1 parent 4d1c06f commit ba4b6b1

File tree

2 files changed

+32
-27
lines changed

2 files changed

+32
-27
lines changed

src/main/java/com/fishercoder/solutions/_506.java

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import java.util.Map;
66

77
/**
8-
* Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: "Gold Medal", "Silver Medal" and "Bronze Medal".
8+
* 506. Relative Ranks
9+
*
10+
* Given scores of N athletes, find their relative ranks and the people with the top three highest scores,
11+
* who will be awarded medals: "Gold Medal", "Silver Medal" and "Bronze Medal".
912
1013
Example 1:
1114
Input: [5, 4, 3, 2, 1]
@@ -19,30 +22,32 @@
1922
*/
2023
public class _506 {
2124

22-
public String[] findRelativeRanks(int[] nums) {
23-
int[] tmp = new int[nums.length];
24-
for (int i = 0; i < nums.length; i++) {
25-
tmp[i] = nums[i];
26-
}
27-
Arrays.sort(tmp);
28-
Map<Integer, String> rankMap = new HashMap();
29-
int len = nums.length;
30-
for (int i = len - 1; i >= 0; i--) {
31-
if (i == len - 1) {
32-
rankMap.put(tmp[i], "Gold Medal");
33-
} else if (i == len - 2) {
34-
rankMap.put(tmp[i], "Silver Medal");
35-
} else if (i == len - 3) {
36-
rankMap.put(tmp[i], "Bronze Medal");
37-
} else {
38-
rankMap.put(tmp[i], String.valueOf(len - i));
25+
public static class Solution1 {
26+
public String[] findRelativeRanks(int[] nums) {
27+
int[] tmp = new int[nums.length];
28+
for (int i = 0; i < nums.length; i++) {
29+
tmp[i] = nums[i];
3930
}
31+
Arrays.sort(tmp);
32+
Map<Integer, String> rankMap = new HashMap();
33+
int len = nums.length;
34+
for (int i = len - 1; i >= 0; i--) {
35+
if (i == len - 1) {
36+
rankMap.put(tmp[i], "Gold Medal");
37+
} else if (i == len - 2) {
38+
rankMap.put(tmp[i], "Silver Medal");
39+
} else if (i == len - 3) {
40+
rankMap.put(tmp[i], "Bronze Medal");
41+
} else {
42+
rankMap.put(tmp[i], String.valueOf(len - i));
43+
}
44+
}
45+
String[] result = new String[len];
46+
for (int i = 0; i < len; i++) {
47+
result[i] = rankMap.get(nums[i]);
48+
}
49+
return result;
4050
}
41-
String[] result = new String[len];
42-
for (int i = 0; i < len; i++) {
43-
result[i] = rankMap.get(nums[i]);
44-
}
45-
return result;
4651
}
4752

4853
}

src/test/java/com/fishercoder/_506Test.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
* Created by fishercoder on 1/15/17.
1212
*/
1313
public class _506Test {
14-
private static _506 test;
14+
private static _506.Solution1 solution1;
1515
private static String[] expected;
1616
private static String[] actual;
1717
private static int[] nums;
1818

1919
@BeforeClass
2020
public static void setup() {
21-
test = new _506();
21+
solution1 = new _506.Solution1();
2222
}
2323

2424
@Before
@@ -31,7 +31,7 @@ public void setupForEachTest() {
3131
public void test1() {
3232
nums = new int[]{2, 4, 1};
3333
expected = new String[]{"Silver Medal", "Gold Medal", "Bronze Medal"};
34-
actual = test.findRelativeRanks(nums);
34+
actual = solution1.findRelativeRanks(nums);
3535
assertArrayEquals(expected, actual);
3636

3737
}
@@ -40,7 +40,7 @@ public void test1() {
4040
public void test2() {
4141
nums = new int[]{5, 4, 3, 2, 1};
4242
expected = new String[]{"Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"};
43-
actual = test.findRelativeRanks(nums);
43+
actual = solution1.findRelativeRanks(nums);
4444
assertArrayEquals(expected, actual);
4545

4646
}

0 commit comments

Comments
 (0)
0