Tôi cảm thấy hơi nóng tính ở đó. Tôi không làm điều này cho điểm. Tôi làm điều đó vì tôi thích nó. Tôi đã cho bạn rất nhiều và nghĩ rằng bạn sẽ có thể thực hiện thông qua của riêng bạn. Dù sao, đây là một nơi mà những người lạ hoàn toàn giúp đỡ những người lạ hoàn toàn.
Đây là một mã, với kết quả của các xét nghiệm sau đây:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ResolvingAlgo {
public static Character[] resolver(Character[] objects, int R) {
//calculate frequency of each element
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (Character c : objects) {
Integer freq = map.get(c);
map.put(c, (freq == null) ? 1 : freq + 1);
}
//count elements with frequency R
List<Character> pillars = new ArrayList<Character>();
for (Character c : map.keySet()) {
int freq = map.get(c);
if (R == freq) {
pillars.add(c);
} else if (objects.length/R < freq) {
return null;
}
}
//output array
Character output[] = new Character[objects.length];
//load the pillars R+1 apart
int skip = (pillars.size()<R)?R:R+1;
for (Character c : pillars) {
int index = 0;
for (int out=index; out<output.length; out++) {
if (output[out] == null) {
break;
}
index++;
}
for (int i = R; i > 0; i--) {
output[index] = c;
index += skip;
}
map.remove(c);
}//pillars
//add remainders
while (!map.isEmpty()) {
int index = 0;
Character keyset[] = Arrays.copyOf(map.keySet().toArray(new Character[0]), map.size());
for (Character c : keyset) {
for (int out = index; out < output.length; out++) {
if (null == output[out]) {
break;
}
index++;
}
output[index] = c;
int freq = map.get(c);
if (freq <= 1) {
map.remove(c);
} else {
map.put(c, freq - 1);
}
}//for keyset
}//while
return output;
}//resolver
public static void main(String... args) {
Character[][] input = {
{'a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd'},
{'a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'k'},
{'a', 'a', 'a', 'b', 'c', 'd', 'd', 'd', 'k'},
{'a', 'b', 'd', 'c', 'a', 'd', 'k', 'a', 'd', 'a', 'a'},
{'a', 'a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd'},
{'a', 'b', 'c', 'd', 'e', 'f', 'a', 'b', 'c', 'd', 'e', 'f'},
{'a','b','c','d','a','b','c','d'}
};
for(Character in[]: input)
System.out.println(Arrays.toString(resolver(in, 3)));
}
}
Kết quả kiểm tra:
[d, b, c, a, d, b, c, a, d, b, c, a]
[b, c, a, d, b, c, a, k, b, c, a, d]
[d, a, b, d, a, c, d, a, k]
null
[b, c, d, b, c, a, b, c, d, a, a, a]
[f, d, e, b, c, a, f, d, e, b, c, a]
[d, b, c, a, d, b, c, a]
@ EvgenyKluev- khi tôi đồng ý rằng tình trạng của bạn là đủ để có được không trật tự, nó là cần thiết? Ngoài ra, bạn có thể sử dụng cách tiếp cận của bạn để tạo ra một trật tự khi một tồn tại? – templatetypedef
@ EvgenyKluev- Tôi không chắc tôi hiểu ý bạn là gì. Tôi không thấy cách sắp xếp mảng có thể tạo ra giải pháp làm việc khi có một mảng, cũng không thấy lý do tại sao sắp xếp mảng và lưu ý rằng không có quá nhiều bản sao của một phần tử đảm bảo rằng có cách sắp xếp các yếu tố. Bạn có thể xây dựng? – templatetypedef
@templatetypedef, tôi hiểu nhầm câu hỏi. Lấy làm tiếc. –