File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Brute Force | Hash Set
3
+ * Time O(n^2) | Space O(n^2)
4
+ * https://leetcode.com/problems/find-missing-and-repeated-values
5
+ * @param {number[][] } grid
6
+ * @return {number[] }
7
+ */
8
+ var findMissingAndRepeatedValues = function ( grid ) {
9
+
10
+ const n = grid . length ;
11
+ const haves = new Set ( ) ;
12
+ const visited = new Set ( ) ;
13
+ let doubleCount = - 1 ;
14
+
15
+ for ( let i = 0 ; i < n ; i ++ ) {
16
+ for ( let j = 0 ; j < n ; j ++ ) {
17
+ haves . add ( grid [ i ] [ j ] ) ;
18
+ if ( visited . has ( grid [ i ] [ j ] ) ) {
19
+ doubleCount = grid [ i ] [ j ] ;
20
+ }
21
+ visited . add ( grid [ i ] [ j ] ) ;
22
+ }
23
+ }
24
+
25
+ let missing = - 1 ;
26
+
27
+ for ( let i = 0 ; i < n ** 2 ; i ++ ) {
28
+ if ( ! haves . has ( i + 1 ) ) {
29
+ missing = i + 1 ;
30
+ }
31
+ }
32
+
33
+ return [ doubleCount , missing ] ;
34
+ } ;
You can’t perform that action at this time.
0 commit comments