[문제] You want to determine the most popular person in the social network. To do this, you will count the number of "2-friends" that each person has. Person A is called a 2-friend of another person B if they are friends with each other of if there exists some person C who is a friend of both A and B. The most popular person is the person with the highest number of 2-friends. (There might be more than one if multiple people all have the maximal number of 2-friends.)

You are given a String[] friends, where the j-th character of the i-th element is 'Y' if person i and person j are friends, and 'N' otherwise. Return the number of 2-friends of the most popular person in this social network


[예시 : 입력 데이터와 출력 데이터]


friends = {"NYY", "YNY", "YYN"}


Returns : 2


모든 사람이 2명의 전체 친구가 있습니다.


public class FriendScore {


   public static void main(String[] args){

    String[][] friends = new String[][]{{"N","Y","N","N","N"},

                                       {"Y","N","Y","N","N"},

                                       {"N","Y","N","Y","N"},

                                       {"N","N","Y","N","Y"},

                                       {"N","N","N","Y","N"}};



    int fHigher = 0, sHigher=0, flag=0;


    for(int i=0; i<friends[0].length; i++){

        if(fHigher >= sHigher)

            sHigher = fHigher;

        fHigher =0;


      for(int j=0; j<friends[0].length; j++) {

          if(friends[i][j].equals("Y"))

              fHigher++;


          for(int z=0; z<friends[0].length;  z++){

              if(i == j) {

                  continue;

              }else if(friends[j][i].equals("Y") && friends[j][z].equals("Y") && friends[i][z].equals("N"))

                  flag++;

          }


          if(flag >= 2){

           fHigher += flag-1;

           flag = 0;

          }

      }

    }


    System.out.println(sHigher);

   }

}




+ Recent posts