-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-if-n-and-its-double-exist_1346.py
44 lines (31 loc) · 1.18 KB
/
check-if-n-and-its-double-exist_1346.py
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
# Given an array arr of integers, check if there exist two indices i and j such that :
# i != j
# 0 <= i, j < arr.length
# arr[i] == 2 * arr[j]
# Example 1:
# Input: arr = [10,2,5,3]
# Output: true
# Explanation: For i = 0 and j = 2, arr[i] == 10 == 2 * 5 == 2 * arr[j]
# Example 2:
# Input: arr = [3,1,7,11]
# Output: false
# Explanation: There is no i and j that satisfy the conditions.
# ---------------------------------------Runtime 93 msBeats 10.34% Memory 16.3 MB Beats 49.69%---------------------------------------
from typing import List
# My solution
# Complexity O(n^2)
class Solution:
def checkIfExist(self, arr: List[int]) -> bool:
for i in range(len(arr)):
for j in range(len(arr)):
if i != j and arr[i] * 2 == arr[j]:
return True
return False
# ---------------------------------------Runtime 70 ms Beats 43.95% Memory 16.4 MB Beats 14.46%---------------------------------------
# Solution @leihao1313
# Complexity O(n)
from collections import Counter
class Solution:
def checkIfExist(self, arr: list[int]) -> bool:
cnt = Counter(arr)
return any(2 * x in cnt and x != 0 for x in arr) or cnt[0] > 1