Skip to content

Commit b59ec39

Browse files
committed
Solve #451, #565
1 parent ba8e4d0 commit b59ec39

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.sean.array;
2+
3+
import java.util.*;
4+
5+
/***
6+
* 565. Array Nesting
7+
*
8+
* N is an integer within the range [1, 20,000].
9+
* The elements of A are all distinct.
10+
* Each element of A is an integer within the range [0, N-1].
11+
*/
12+
public class NestingArray {
13+
public int arrayNesting(int[] nums) {
14+
if (nums == null || nums.length == 0)
15+
return 0;
16+
17+
int length = nums.length;
18+
if (length == 1) {
19+
return 1;
20+
}
21+
22+
int max = 0;
23+
Set<Integer> visitedPositions = new HashSet<>();
24+
25+
for (int i = 0; i < length; i++) {
26+
if (!visitedPositions.contains(i)) {
27+
int elem = nums[i];
28+
int cnt = 0;
29+
while (!visitedPositions.contains(elem)) {
30+
++cnt;
31+
32+
visitedPositions.add(elem);
33+
elem = nums[elem];
34+
}
35+
36+
max = Math.max(max, cnt);
37+
}
38+
}
39+
40+
return max;
41+
}
42+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.sean.sorting;
2+
3+
import java.util.Comparator;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import java.util.PriorityQueue;
7+
8+
/***
9+
* 451. Sort Characters By Frequency
10+
*/
11+
public class FrequencySort {
12+
public String frequencySort(String s) {
13+
if (s == null) {
14+
return "";
15+
}
16+
17+
int length = s.length();
18+
if (length <= 1) {
19+
return s;
20+
}
21+
22+
PriorityQueue<String> queue = new PriorityQueue<>(new Comparator<String>() {
23+
@Override
24+
public int compare(String o1, String o2) { //5@a, 7@b
25+
int length2 = o2.length();
26+
int length1 = o1.length();
27+
if(length2 > length1) {
28+
return 1;
29+
} else if(length2 < length1) {
30+
return -1;
31+
}
32+
return o2.compareTo(o1);
33+
}
34+
});
35+
36+
Map<Character, Integer> map = new HashMap<>();
37+
for (int i = 0; i < length; i++) {
38+
char ch = s.charAt(i);
39+
if (map.containsKey(ch)) {
40+
map.put(ch, map.get(ch) + 1);
41+
} else {
42+
map.put(ch, 1);
43+
}
44+
}
45+
46+
for (Character key : map.keySet()) {
47+
queue.add(map.get(key) + "," + key);
48+
}
49+
50+
StringBuilder builder = new StringBuilder();
51+
while (!queue.isEmpty()) {
52+
String str = queue.poll();
53+
String[] strs = str.split(",");
54+
int n = Integer.parseInt(strs[0]);
55+
for (int j = 0; j < n; j++) {
56+
builder.append(strs[1]);
57+
}
58+
}
59+
return builder.toString();
60+
}
61+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.sean.array;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.*;
7+
8+
public class NestingArrayTest {
9+
10+
@Test
11+
public void arrayNesting() {
12+
NestingArray array = new NestingArray();
13+
int len = array.arrayNesting(new int[]{5, 4, 0, 3, 1, 6, 2});
14+
Assert.assertEquals(4, len);
15+
}
16+
}

0 commit comments

Comments
 (0)