Skip to content

Commit a350fa2

Browse files
committed
2661
1 parent 1391259 commit a350fa2

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# @param {Integer[]} arr
2+
# @param {Integer[][]} mat
3+
# @return {Integer}
4+
def first_complete_index(arr, mat)
5+
index_hash = arr.map.with_index { |x, i| [x, i] }.to_h
6+
7+
rows = mat.size
8+
cols = mat[0].size
9+
10+
max_row_indexs = Array.new(rows, 0)
11+
max_col_indexs = Array.new(cols, 0)
12+
13+
(0...rows).each { |r|
14+
(0...cols).each { |c|
15+
index = index_hash[mat[r][c]]
16+
max_row_indexs[r] = [max_row_indexs[r], index].max
17+
max_col_indexs[c] = [max_col_indexs[c], index].max
18+
}
19+
}
20+
21+
[max_row_indexs.min, max_col_indexs.min].min
22+
end
23+
24+
# optimize
25+
def first_complete_index(arr, mat)
26+
indexes_hash = {}
27+
rows = mat.size
28+
cols = mat[0].size
29+
(0...rows).each { |r|
30+
(0...cols).each { |c|
31+
indexes_hash[mat[r][c]] = [r, c]
32+
}
33+
}
34+
35+
row_count = Array.new(rows, 0)
36+
col_count = Array.new(cols, 0)
37+
arr.each_with_index { |x, i|
38+
r, c = indexes_hash[x]
39+
return i if (row_count[r] += 1) >= cols || (col_count[c] += 1) >= rows
40+
}
41+
end

0 commit comments

Comments
 (0)