LeetCode 1267 | Count Servers that Communicate | Java | Medium | Frequency Array Scan | CodeByTushu
Description: π Welcome Back to CodeByTushu!
In todayβs video, we solve LeetCode 1267 β Count Servers that Communicate using Array, Frequency Counting and Matrix Traversal in Java.
In this problem, we are given an m-by-n integer grid representing a computer network center where 1 indicates an active server and 0 indicates an empty slot. Two servers can communicate with each other if and only if they share the exact same row or the exact same column. Our objective is to determine and return the total count of servers that are able to communicate with at least one other server in the cluster.
We implement an optimal two-pass matrix projection algorithm. In the first pass, we project the 2D server distribution onto two 1D frequency arrays, tracking the total server count for each row and column in linear time. In the second pass, we inspect every active server: if its row server count is greater than 1 or its column server count is greater than 1, it communicates with another server and increments our answer. This achieves an optimal O(M * N) time complexity and O(M + N) auxiliary space without modifying the input grid.
π₯ Problem Highlights
β 1D projection principle: compress 2D grid coordinates into row and column server frequency counts
β Two-pass linear matrix traversal ensuring clean separation of counting and qualification
β Communication qualification condition: active server connects if row count is greater than 1 or col count is greater than 1
β Eliminates redundant pairwise search across rows and columns
β Non-destructive design keeping original grid completely intact
β Optimal O(M * N) runtime with minimal O(M + N) auxiliary memory
β±οΈ TIMESTAMPS
00:00 Intro & Day 106 Streak Celebration
00:30 Problem Understanding & Matrix Rules
01:13 Key Insight: 1D Row & Column Projections
02:07 Two-Pass Linear Matrix Scan Strategy
03:18 Optimal Java 17 Solution Walkthrough Line by Line
05:20 Test Cases Walkthrough & Day 106 Streak Celebration
05:52 Time Complexity Analysis (O(M * N))
06:33 Space Complexity Analysis (O(M + N))
07:09 Summary & Outro!
π Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/November/08-November/1267-Count-Servers-that-Communicate
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/08-November/1267-Count-Servers-that-Communicate/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/08-November/1267-Count-Servers-that-Communicate/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/08-November/1267-Count-Servers-that-Communicate/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/08-November/1267-Count-Servers-that-Communicate/solution.js
π LeetCode Source Code
π https://codebytushu.com/leetcode/problem/count-servers-that-communicate
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Medium Problems
Java DSA Preparation
Matrix Coordinate Compression
Frequency Counting Arrays
Grid Traversal Patterns
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
When does an active server at cell (i, j) qualify as communicating with other servers?
1οΈβ£ Only when another server is directly adjacent (up, down, left, right)
2οΈβ£ When rowCount[i] is greater than 1 or colCount[j] is greater than 1
3οΈβ£ Only when rowCount[i] and colCount[j] are both greater than 1 simultaneously
4οΈβ£ When total servers in the entire grid is greater than or equal to 2
#LeetCode1267 #LeetCode #CountServersThatCommunicate #Java #JavaDSA #Array #Matrix #Counting #DSA #CodingInterview #ProblemSolving #LeetCodeMedium #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description
In todayβs video, we solve LeetCode 1267 β Count Servers that Communicate using Array, Frequency Counting and Matrix Traversal in Java.
In this problem, we are given an m-by-n integer grid representing a computer network center where 1 indicates an active server and 0 indicates an empty slot. Two servers can communicate with each other if and only if they share the exact same row or the exact same column. Our objective is to determine and return the total count of servers that are able to communicate with at least one other server in the cluster.
We implement an optimal two-pass matrix projection algorithm. In the first pass, we project the 2D server distribution onto two 1D frequency arrays, tracking the total server count for each row and column in linear time. In the second pass, we inspect every active server: if its row server count is greater than 1 or its column server count is greater than 1, it communicates with another server and increments our answer. This achieves an optimal O(M * N) time complexity and O(M + N) auxiliary space without modifying the input grid.
π₯ Problem Highlights
β 1D projection principle: compress 2D grid coordinates into row and column server frequency counts
β Two-pass linear matrix traversal ensuring clean separation of counting and qualification
β Communication qualification condition: active server connects if row count is greater than 1 or col count is greater than 1
β Eliminates redundant pairwise search across rows and columns
β Non-destructive design keeping original grid completely intact
β Optimal O(M * N) runtime with minimal O(M + N) auxiliary memory
β±οΈ TIMESTAMPS
00:00 Intro & Day 106 Streak Celebration
00:30 Problem Understanding & Matrix Rules
01:13 Key Insight: 1D Row & Column Projections
02:07 Two-Pass Linear Matrix Scan Strategy
03:18 Optimal Java 17 Solution Walkthrough Line by Line
05:20 Test Cases Walkthrough & Day 106 Streak Celebration
05:52 Time Complexity Analysis (O(M * N))
06:33 Space Complexity Analysis (O(M + N))
07:09 Summary & Outro!
π Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/November/08-November/1267-Count-Servers-that-Communicate
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/08-November/1267-Count-Servers-that-Communicate/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/08-November/1267-Count-Servers-that-Communicate/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/08-November/1267-Count-Servers-that-Communicate/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/08-November/1267-Count-Servers-that-Communicate/solution.js
π LeetCode Source Code
π https://codebytushu.com/leetcode/problem/count-servers-that-communicate
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Medium Problems
Java DSA Preparation
Matrix Coordinate Compression
Frequency Counting Arrays
Grid Traversal Patterns
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
When does an active server at cell (i, j) qualify as communicating with other servers?
1οΈβ£ Only when another server is directly adjacent (up, down, left, right)
2οΈβ£ When rowCount[i] is greater than 1 or colCount[j] is greater than 1
3οΈβ£ Only when rowCount[i] and colCount[j] are both greater than 1 simultaneously
4οΈβ£ When total servers in the entire grid is greater than or equal to 2
#LeetCode1267 #LeetCode #CountServersThatCommunicate #Java #JavaDSA #Array #Matrix #Counting #DSA #CodingInterview #ProblemSolving #LeetCodeMedium #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description




