う〜ん。計算量省こうとしてミスった感があるなぁ・・・。ただのforにした方が見やすくてミスも少なそう。
import java.util.*;
public class InterestingParty_2 {
int bestInvitation(String[] first, String[] second) {
HashMap<String, String> hm = new HashMap<String, String>();
for(int r = 0; r < first.length; r++) {
hm.put(first[r], second[r]);
}
int[] count = new int[first.length * 2];
for(int r = 0; r < first.length; r++) {
for(int c = 0; c < second.length; c++) {
if(r == c) continue;
if(first[r] == first[c] || first[r] == hm.get(first[c])) count[r]++;
}
for(int c = 0; c < second.length; c++) {
if(r == c) continue;
if(hm.get(first[r]) == first[c] || hm.get(first[r]) == hm.get(first[c])) count[r + 4]++;
}
count[r]++;
count[r + 4]++;
}
int ans = Integer.MIN_VALUE;
for(int r = 0; r < count.length; r++) {
ans = Math.max(ans, count[r]);
}
return(ans);
}
void doIt() {
String[] first = {"s", "pro", "cob", "mon"};
String[] second = {"pyth", "pyth", "ana", "pyth"};
System.out.println(bestInvitation(first, second));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new InterestingParty_2().doIt();
}
}
あ、ここおかしいとか遠慮なくどんどん突っ込んでくださると嬉しいです!(≧▽≦)b
count逐次最大値入れてきゃ、配列にしなくていいじゃん
俺馬鹿だぁ・・・