Created
January 18, 2017 22:10
-
-
Save schmohlio/77a51a792b38e2304af883933a4085dd to your computer and use it in GitHub Desktop.
"friend circles" brain teaser
This file contains hidden or 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 io.schmohl; | |
import java.io.*; | |
import java.util.*; | |
import java.text.*; | |
import java.math.*; | |
import java.util.regex.*; | |
public class FriendCircles { | |
/* | |
* Complete the function below. | |
*/ | |
static final char YES = 'Y'; | |
// we also don't check for edge cases bc HackerRank is guaranteeing "Constraints". | |
static int friendCircles(String[] friends) { | |
char[][] matrix = stringArrayToMatrix(friends); | |
int n = matrix.length; | |
// optimization so that we don't allocated an array. | |
if (n == 1) return 1; | |
// store the friends that were assigned a friend group (since they cannot be in >1 friend group) in memory. | |
// incurs o(n) space and time. | |
boolean[] assigned = new boolean[n]; | |
for (int i = 0; i < n; i++) assigned[i] = false; | |
// count the number of circles; | |
int circles = 0; | |
// matrix rows traversal. | |
for (int i = 0; i < n; i++) { | |
// skip friends on the axis that have already been assigned a friend group. | |
if (assigned[i]) continue; | |
// each iteration must mark the friend assigned; | |
// NOTE: this needs to occur before invoking updateInPlace. | |
assigned[i] = true; | |
// execute recursive search for friends. | |
updateInPlace(matrix, assigned, i); | |
// each iteratino increases the number of circle friends. because the | |
// UpdateInPlace method terminated. | |
circles++; | |
} | |
return circles; | |
} | |
// helper function for columns. we can think of this as a traversal of columns @ bottom of the call stack, | |
// but it alternates witch each new stack. | |
// updates the assigned friends, should not modify matrix. recurses on each new found friend. | |
private static void updateInPlace(char[][] matrix, boolean[] assigned, int i) { | |
for (int j = 0; j < matrix.length; j++) { | |
// skip j already assigned to friends circle. | |
if (assigned[j]) continue; | |
// skip where i == j. GAH this was the problem! | |
if (i == j) continue; | |
// check friendship. if they are friends, mark the new friend as assigned, and traverse their friends. | |
if (matrix[i][j] == YES) { | |
assigned[j] = true; | |
updateInPlace(matrix, assigned, j); | |
} | |
} | |
} | |
// we should probably get rid of this as it seems inefficient? | |
// no checks in place bc of HackerRank guarantees. | |
private static char[][] stringArrayToMatrix(String[] a) { | |
int n = a.length; | |
char[][] r = new char[n][n]; | |
for (int i = 0; i < n; i++) { | |
r[i] = a[i].toCharArray(); | |
} | |
return r; | |
} | |
public static void main( String[] args ) | |
{ | |
String sample = "" + | |
"4\n" + | |
"YYNN\n" + | |
"YYYN\n" + | |
"NYYN\n" + | |
"NNNY\n"; | |
Scanner in = new Scanner(sample); | |
int res; | |
int _friends_size = 0; | |
_friends_size = Integer.parseInt(in.nextLine().trim()); | |
String[] _friends = new String[_friends_size]; | |
String _friends_item; | |
for(int _friends_i = 0; _friends_i < _friends_size; _friends_i++) { | |
try { | |
_friends_item = in.nextLine(); | |
} catch (Exception e) { | |
_friends_item = null; | |
} | |
_friends[_friends_i] = _friends_item; | |
} | |
System.out.println("result: " + friendCircles(_friends)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment