-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindDuplicates.py
More file actions
67 lines (50 loc) · 2.22 KB
/
findDuplicates.py
File metadata and controls
67 lines (50 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
Given an integer array nums of length n where all the integers of nums are in the range [1, n] and each integer appears
once or twice, return an array of all the integers that appears twice.
You must write an algorithm that runs in O(n) time and uses only constant extra space.
Example 1:
Input: nums = [4,3,2,7,8,2,3,1]
Output: [2,3]
Example 2:
Input: nums = [1,1,2]
Output: [1]
Example 3:
Input: nums = [1]
Output: []
"""
"""
The below is the solution I could think of first. But, memory-wise, it is not the best solution to go with, as we are
using two DS for the operation.
*The stats for the submission*
Runtime: 558 ms, faster than 36.21% of Python3 online submissions for Find All Duplicates in an Array.
Memory Usage: 23.5 MB, less than 8.65% of Python3 online submissions for Find All Duplicates in an Array.
"""
class Solution:
def findDuplicates(self, nums: list[int]) -> list[int]:
memo = set()
output = []
for i in range(len(nums)):
if nums[i] in memo:
output.append(nums[i])
else:
memo.add(nums[i])
return output
"""
I came across this other solution which is probably more memory efficient
* Since the element can appear once or twice, loop through all the elements of the list.
Every time you go to an element, turn the value at the index of that element to negative to mark that you have seen this
element once.
First scenarios: In every element (i), check the value of the element at index (i), if it is negative -> Then it has
appeared before -> Then it is what we are looking for.
Second scenarios: If the value of the element at index (i) is still positive -> Then it is the first time we meet it ->
Turn it to negative and continue.
"""
class Solution:
def findDuplicates2(self, nums: list[int]) -> list[int]:
answers = []
for num in nums:
if nums[abs(num) - 1] < 0:
answers.append(abs(num))
else:
nums[abs(num) - 1] = -nums[abs(num) - 1]
return answers