Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4e645a3

Browse files
2965. Find Missing and Repeated Values
1 parent 27ce049 commit 4e645a3

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
3+
// Solution by Sergey Leschev
4+
// 2965. Find Missing and Repeated Values
5+
6+
// Time complexity: O(n * n)
7+
// Space complexity: O(n * n)
8+
9+
func findMissingAndRepeatedValues(_ grid: [[Int]]) -> [Int] {
10+
var freq = [Int: Int]()
11+
let n = grid.count
12+
13+
for i in 0..<n {
14+
for j in 0..<n {
15+
freq[grid[i][j], default: 0] += 1
16+
}
17+
}
18+
19+
var repeated = 0
20+
var missing = 0
21+
22+
for i in 1...(n * n) {
23+
if freq[i] == 2 {
24+
repeated = i
25+
}
26+
if freq[i] == nil {
27+
missing = i
28+
}
29+
}
30+
31+
return [repeated, missing]
32+
}
33+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /