
Problem Reference - Hackerrank
This updated information has been further expanded upon on my new website. You can find the updated details here: https://k5kc.com/cs/problems/number-of-provinces/.
Problem
There areYou are given a
Y
or N
. If Input Format
First line of the input contains an integer
Output Format
Print the maximum number of friend circles.
Constraints
Each element of matrix friends will be
Y
or N
. Number of rows and columns will be equal in the matrix.
Here is the code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vaani.hackerrank; | |
import java.util.ArrayList; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Set; | |
/** | |
* Created by mdev on 4/8/15. | |
*/ | |
public class FriendsCircle { | |
public static void main(String[] args) { | |
String[] friends = new String[]{"YYNN", "YYYN", "NYYN", "NNNY"}; | |
String[] friends1 = new String[]{"YNNNN", "NYNNN", "NNYNN", "NNNYN","NNNNY"}; | |
int result = friendCircles(friends1); | |
System.out.println("result = " + result); | |
} | |
static int friendCircles(String[] friends) { | |
List<Set> list = new ArrayList<>(); | |
for (int friend = 0; friend < friends.length; friend++) { | |
String contacts = friends[friend]; | |
for (int contact = 0; contact < contacts.length(); contact++) { | |
Set<Integer> s1 = null, s2 = null; | |
if (contacts.charAt(contact) == 'Y') { | |
for (Set s : list) { | |
if (s.contains(friend)) | |
s1 = s; | |
if (s.contains(contact)) | |
s2 = s; | |
} | |
if (s1 == null && s2 == null) { | |
Set<Integer> newSet = new HashSet<>(); | |
newSet.add(contact); | |
newSet.add(friend); | |
list.add(newSet); | |
} else if (s1 == null && s2 != null) { | |
s2.add(friend); | |
} else if (s2 == null && s1 != null) { | |
s1.add(contact); | |
} else {//both not null | |
if(s1 != s2) { | |
for (Integer s : s2) { | |
s1.add(s); | |
} | |
list.remove(s2); | |
} | |
} | |
} | |
} | |
} | |
System.out.println(list); | |
return list.size(); | |
} | |
} |
Thanks
0 comments:
Post a Comment