LoginSignup
1
1

More than 5 years have passed since last update.

楽しいパーティ2

Last updated at Posted at 2014-06-21

 う〜ん。計算量省こうとしてミスった感があるなぁ・・・。ただの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逐次最大値入れてきゃ、配列にしなくていいじゃん
俺馬鹿だぁ・・・

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1