forked from oleg-cherednik/DailyCodingProblem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
36 lines (31 loc) · 1.02 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* @author Oleg Cherednik
* @since 02.06.2019
*/
public class Solution {
public static void main(String... args) {
int[][] matrix = {
{ 1, 3, 7, 10, 15, 20 },
{ 2, 6, 9, 14, 22, 25 },
{ 3, 8, 10, 15, 25, 30 },
{ 10, 11, 12, 23, 30, 35 },
{ 20, 25, 30, 35, 40, 45 } };
System.out.println(count(matrix, 1, 1, 3, 3));
}
public static int count(int[][] matrix, int i1, int j1, int i2, int j2) {
int res = 0;
int low = matrix[i1][j1];
int high = matrix[i2][j2];
for (int[] row : matrix) {
if (row[0] > high || row[row.length - 1] < low)
res += row.length;
else if (row[0] >= low && row[row.length - 1] <= high)
continue;
for (int i = 0; i < row.length && row[i] < low; i++)
res++;
for (int i = row.length - 1; i >= 0 && row[i] > high; i--)
res++;
}
return res;
}
}