File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed
Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change 1+ // https://leetcode.com/problems/count-servers-that-communicate/
2+ // algorithms
3+ // Medium (58.15%)
4+ // Total Accepted: 7,731
5+ // Total Submissions: 13,294
6+
7+
8+ class Solution {
9+
10+ private final static int EXIST_BIT = 1 ;
11+ private final static int COMPUTED_BIT = 1 << 1 ;
12+
13+ public int countServers (int [][] grid ) {
14+ int row = grid .length ;
15+ int col = grid [0 ].length ;
16+ int res = 0 ;
17+ ArrayList <Integer > l = new ArrayList <>();
18+
19+ for (int i = 0 ; i < row ; i ++) {
20+ l .clear ();
21+ for (int j = 0 ; j < col ; j ++) {
22+ if (grid [i ][j ] == 1 ) {
23+ l .add (j );
24+ }
25+ }
26+ if (l .size () > 1 ) {
27+ res += l .size ();
28+ for (int j : l ) {
29+ grid [i ][j ] |= COMPUTED_BIT ;
30+ }
31+ }
32+ }
33+
34+ for (int j = 0 ; j < col ; j ++) {
35+ l .clear ();
36+ for (int i = 0 ; i < row ; i ++) {
37+ if ((grid [i ][j ] & EXIST_BIT ) != 0 ) {
38+ l .add (i );
39+ }
40+ }
41+ if (l .size () > 1 ) {
42+ for (int i : l ) {
43+ if ((grid [i ][j ] & COMPUTED_BIT ) == 0 ) {
44+ res ++;
45+ }
46+ }
47+ }
48+ }
49+
50+ return res ;
51+ }
52+
53+ }
You can’t perform that action at this time.
0 commit comments