We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0d60a2a commit 274439dCopy full SHA for 274439d
Python/counting-elements.py
@@ -0,0 +1,31 @@
1
+# Time: O(n)
2
+# Space: O(n)
3
+
4
+class Solution(object):
5
+ def countElements(self, arr):
6
+ """
7
+ :type arr: List[int]
8
+ :rtype: int
9
10
+ lookup = set(arr)
11
+ return sum(1 for x in arr if x+1 in lookup)
12
13
14
+# Time: O(nlogn)
15
+# Space: O(1)
16
17
18
19
20
21
22
+ arr.sort()
23
+ result, l = 0, 1
24
+ for i in xrange(len(arr)-1):
25
+ if arr[i] == arr[i+1]:
26
+ l += 1
27
+ continue
28
+ if arr[i]+1 == arr[i+1]:
29
+ result += l
30
+ l = 1
31
+ return result
0 commit comments