-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
solutions/1072. Flip Columns For Maximum Number of Equal Rows/1072.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
public class Solution | ||
{ | ||
public int MaxEqualRowsAfterFlips(int[][] matrix) | ||
{ | ||
int m = matrix.Length; | ||
int n = matrix[0].Length; | ||
int ans = 0; | ||
int[] flip = new int[n]; | ||
HashSet<int> seen = new HashSet<int>(); | ||
|
||
for (int i = 0; i < m; ++i) | ||
{ | ||
if (seen.Contains(i)) | ||
continue; | ||
|
||
int count = 0; | ||
|
||
for (int j = 0; j < n; ++j) | ||
flip[j] = 1 ^ matrix[i][j]; | ||
|
||
for (int k = 0; k < m; ++k) | ||
{ | ||
if (AreArraysEqual(matrix[k], matrix[i]) || AreArraysEqual(matrix[k], flip)) | ||
{ | ||
seen.Add(k); | ||
++count; | ||
} | ||
} | ||
|
||
ans = Math.Max(ans, count); | ||
} | ||
|
||
return ans; | ||
} | ||
|
||
private bool AreArraysEqual(int[] a, int[] b) | ||
{ | ||
if (a.Length != b.Length) | ||
return false; | ||
|
||
for (int i = 0; i < a.Length; i++) | ||
{ | ||
if (a[i] != b[i]) | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
public class Solution | ||
{ | ||
public int FindNthDigit(int n) | ||
{ | ||
int digitSize = 1; | ||
int startNum = 1; | ||
long count = 9; | ||
|
||
while (digitSize * count < n) | ||
{ | ||
n -= digitSize * (int)count; | ||
++digitSize; | ||
startNum *= 10; | ||
count *= 10; | ||
} | ||
|
||
int targetNum = startNum + (n - 1) / digitSize; | ||
int index = (n - 1) % digitSize; | ||
return (int)(targetNum.ToString()[index]) - '0'; | ||
} | ||
} |