Problem Statement
A diamond mine is a matrix of ones and zeroes with R rows and C columns.
A diamond is a pattern of ones forming the boundary of a 45-degree rotated square. The diamonds of size 1, 2, and 3 are shown below.
size 1: size 2: size 3:
1
1 1 1
1 1 1 1 1
1 1 1
1
The diamond mine in this problem will be pseudo-randomly generated using the following method:
You are given two
Process the cells of the matrix in row major order (i.e., first row left to right, second row left to right, etc.). Each time you process a cell, look at the next xi (starting with x1 for the upper left corner). If it is greater than or equal to threshold, the current cell will contain a 1, otherwise it will contain a 0.
Your method shall compute and return the size of the largest diamond that can be found in the mine. If there are no diamonds, return 0.
Definition
- Class:
- DiamondMining
- Method:
- largestDiamond
- Parameters:
- int, int, int, int
- Returns:
- int
- Method signature:
- int largestDiamond(int R, int C, int seed, int threshold)
- (be sure your method is public)
Notes
- The random generation of the input is only for keeping the input size small. The author's solution does not depend on any properties of the generator, and would work fast enough for any input of allowed dimensions.
Constraints
- R will be between 1 and 750, inclusive.
- C will be between 1 and 750, inclusive.
- seed will be between 0 and 65,535, inclusive.
- threshold will be between 0 and 65,536, inclusive.
Examples
5
5
47
20598
Returns: 3
The generated pseudo-random sequence is 17332, 39133, 37242, 14235, 656, 12265, 20598, 6471, 51372, 44853, 44210, 45363, 37384, 49857, 49710, 19295, 39588, 22157, 60650, 29643, 24192, 38553, 51430, 63095, and 39324. The resulting diamond mine looks as follows: 0 1 1 0 0 0 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1
5
5
47
20599
Returns: 2
The pseudo-random sequence is the same, but the threshold is larger. The only change in the resulting diamond mine is that now the cell in row 2, column 2 contains a 0, and therefore there is no diamond of size 3.
4
4
11
65500
Returns: 0
No diamonds at all.
3
6
47
15000
Returns: 2
1 1 1 0 0 0 1 0 1 1 1 1 1 1 1 1 1 1 The only diamond of size 2 is located on the left side of the mine.
200
300
42
0
Returns: 100
Regardless of the seed, if the threshold is 0, each cell contains a 1. Therefore the size of the largest diamond is only limited by the dimensions of the mine.
500
600
42
2000
Returns: 129
1
1
42
50000
Returns: 0
1
1
42
5900
Returns: 1
750
750
42
5000
Returns: 49
748
741
22741
0
Returns: 371
747
5
24324
0
Returns: 3
1
744
16756
0
Returns: 1
601
688
1207
0
Returns: 301
748
747
23219
1
Returns: 374
741
1
20926
1
Returns: 1
1
748
32044
1
Returns: 1
472
254
20638
1
Returns: 127
746
750
31769
2
Returns: 373
744
5
10857
2
Returns: 3
5
750
29825
2
Returns: 3
121
148
15596
2
Returns: 61
745
742
493
10
Returns: 371
742
5
10872
10
Returns: 3
5
749
13442
10
Returns: 3
642
108
15961
10
Returns: 54
741
749
1332
50
Returns: 371
750
2
26801
50
Returns: 1
4
743
24434
50
Returns: 2
526
299
15958
50
Returns: 150
744
750
23667
100
Returns: 371
743
5
17636
100
Returns: 3
3
742
11024
100
Returns: 2
676
423
20765
100
Returns: 212
741
743
2003
1000
Returns: 244
742
1
19801
1000
Returns: 1
1
745
19354
1000
Returns: 1
587
494
20358
1000
Returns: 207
747
749
9222
2000
Returns: 117
748
3
27149
2000
Returns: 2
1
741
12174
2000
Returns: 1
166
210
2203
2000
Returns: 72
750
748
10219
3000
Returns: 66
743
3
24987
3000
Returns: 2
3
744
12341
3000
Returns: 2
252
384
32489
3000
Returns: 63
749
750
28914
5000
Returns: 49
743
5
3067
5000
Returns: 3
5
744
6880
5000
Returns: 3
443
225
26011
5000
Returns: 42
750
743
25151
10000
Returns: 20
750
1
18157
10000
Returns: 1
3
742
14063
10000
Returns: 2
304
662
20808
10000
Returns: 21
750
746
6801
15000
Returns: 11
742
3
50
15000
Returns: 2
2
748
11414
15000
Returns: 1
650
552
20614
15000
Returns: 11
747
747
1248
20000
Returns: 10
748
2
27008
20000
Returns: 1
1
749
28766
20000
Returns: 1
246
579
24646
20000
Returns: 9
747
749
18376
25000
Returns: 6
741
2
10802
25000
Returns: 1
4
746
5996
25000
Returns: 2
657
536
28638
25000
Returns: 7
748
744
22440
30000
Returns: 5
748
4
8897
30000
Returns: 2
2
742
14467
30000
Returns: 1
337
416
26853
30000
Returns: 6
743
747
12168
40000
Returns: 3
744
1
5622
40000
Returns: 1
5
746
15818
40000
Returns: 2
679
161
28353
40000
Returns: 3
750
748
29885
50000
Returns: 3
744
4
19419
50000
Returns: 2
3
745
19277
50000
Returns: 2
576
110
18945
50000
Returns: 3
743
743
2382
65000
Returns: 1
748
2
629
65000
Returns: 1
5
743
7748
65000
Returns: 1
139
325
13162
65000
Returns: 1
749
748
20756
65535
Returns: 1
741
2
16435
65535
Returns: 0
3
748
2326
65535
Returns: 0
228
500
29395
65535
Returns: 1
747
745
11684
65536
Returns: 0
746
4
23588
65536
Returns: 0
3
746
28699
65536
Returns: 0
183
399
1456
65536
Returns: 0
735
723
25441
3421
Returns: 62
712
747
31234
1234
Returns: 155
734
750
24324
432
Returns: 353
1
1
0
50000
Returns: 0
750
750
37777
12345
Returns: 13
733
749
63987
8067
Returns: 25
750
750
0
65535
Returns: 1
750
750
10
1000
Returns: 259
750
750
47
1000
Returns: 259
750
750
5436
0
Returns: 375
750
750
1
100
Returns: 374
750
750
124
30000
Returns: 6
750
750
2354
1000
Returns: 259
750
750
47
32000
Returns: 6
741
742
16435
10000
Returns: 18
750
750
278
1000
Returns: 259
750
750
1
0
Returns: 375
749
749
1091
1823
Returns: 128
730
730
1221
2000
Returns: 128
750
750
11
103
Returns: 371
741
742
16435
5000
Returns: 39
750
750
42
40000
Returns: 4
750
749
12345
30
Returns: 375