You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
4
+
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
5
+
Return the number of good subarrays of A.
6
+
7
+
Example 1:
8
+
Input: A = [1,2,1,2,3], K = 2
9
+
Output: 7
10
+
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
11
+
12
+
Example 2:
13
+
Input: A = [1,2,1,3,4], K = 3
14
+
Output: 3
15
+
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator.
4
+
Return the quotient after dividing dividend by divisor.
5
+
The integer division should truncate toward zero, which means losing its fractional part. For example, truncate(8.345) = 8 and truncate(-2.7335) = -2.
6
+
7
+
Note:
8
+
Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For this problem, assume that your function returns 231 − 1 when the division result overflows.
9
+
10
+
Example 1:
11
+
Input: dividend = 10, divisor = 3
12
+
Output: 3
13
+
Explanation: 10/3 = truncate(3.33333..) = 3.
14
+
15
+
Example 2:
16
+
Input: dividend = 7, divisor = -3
17
+
Output: -2
18
+
Explanation: 7/-3 = truncate(-2.33333..) = -2.
19
+
20
+
Example 3:
21
+
Input: dividend = 0, divisor = 1
22
+
Output: 0
23
+
'''
24
+
25
+
# take a = 10, b = 3
26
+
# step 1: 10 - (3) = 7
27
+
# step 2: 7 - (3*2) = 1
28
+
# step 3: 1 - (3*4) = ...
29
+
# each time subtract double the number which we subtracted last time and add number of time in ans
30
+
classSolution:
31
+
defdivide(self, A: int, B: int):
32
+
33
+
ifB==1:
34
+
returnA
35
+
36
+
a=abs(A)
37
+
b=abs(B)
38
+
res=0
39
+
whilea-b>=0:
40
+
x=0
41
+
while (a- (b<<x)) >=0: # 3*1, 3*2, 3*4.... where b = 3 and 2*(..) = x
Given a reference of a node in a connected undirected graph. Return a deep copy (clone) of the graph.
4
+
Each node in the graph contains a val (int) and a list (List[Node]) of its neighbors.
5
+
6
+
Test case format:
7
+
For simplicity sake, each node's value is the same as the node's index (1-indexed). For example, the first node with val = 1, the second node with val = 2, and so on. The graph is represented in the test case using an adjacency list.
8
+
Adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a node in the graph.
9
+
10
+
Example 1:
11
+
Input: adjList = [[2,4],[1,3],[2,4],[1,3]]
12
+
Output: [[2,4],[1,3],[2,4],[1,3]]
13
+
Explanation: There are 4 nodes in the graph.
14
+
1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
15
+
2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
16
+
3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
17
+
4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
18
+
19
+
Example 2:
20
+
Input: adjList = [[]]
21
+
Output: [[]]
22
+
Explanation: Note that the input contains one empty list. The graph consists of only one node with val = 1 and it does not have any neighbors.
23
+
24
+
Example 4:
25
+
Input: adjList = [[2],[1]]
26
+
Output: [[2],[1]]
27
+
'''
28
+
29
+
'''
30
+
Definition for a Node.
31
+
class Node:
32
+
def __init__(self, val = 0, neighbors = None):
33
+
self.val = val
34
+
self.neighbors = neighbors if neighbors is not None else []
35
+
'''
36
+
37
+
# method - 1
38
+
fromcollectionsimportdefaultdict, deque
39
+
classSolution:
40
+
defcloneGraph(self, node: 'Node'):
41
+
42
+
ifnode==None:
43
+
returnNone
44
+
45
+
visited=defaultdict(bool)
46
+
copyvisited=defaultdict()
47
+
queue=deque()
48
+
49
+
src=Node(node.val)
50
+
queue.append([node, src])
51
+
copyvisited[node.val] =src
52
+
53
+
whilelen(queue) >0:
54
+
55
+
original, copy=queue.popleft()
56
+
visited[original] =True
57
+
58
+
fornbrinoriginal.neighbors:
59
+
# if node is not visited
60
+
ifnotvisited[nbr]:
61
+
# if node is already created by some other node then refer that node else create
62
+
ifnbr.valnotincopyvisited:
63
+
child=Node(nbr.val)
64
+
copyvisited[nbr.val] =child
65
+
else:
66
+
child=copyvisited[nbr.val]
67
+
68
+
copy.neighbors.append(child)
69
+
child.neighbors.append(copy)
70
+
71
+
queue.append([nbr, child])
72
+
73
+
returnsrc
74
+
75
+
# method - 2
76
+
fromcollectionsimportdeque
77
+
classSolution:
78
+
defcloneGraph(self, node: 'Node'):
79
+
80
+
ifnode==None:
81
+
returnNone
82
+
83
+
# keep mapping of node: clone
84
+
clone= {}
85
+
queue=deque()
86
+
cloneroot=Node(node.val)
87
+
88
+
clone= {node: cloneroot}
89
+
queue.append(node)
90
+
91
+
whilelen(queue) >0:
92
+
93
+
n=queue.popleft()
94
+
95
+
# process all neighbors of node
96
+
fornbrinn.neighbors:
97
+
# if node is not created then create, add in map and queue
98
+
ifnbrnotinclone:
99
+
child=Node(nbr.val)
100
+
clone[nbr] =child
101
+
queue.append(nbr)
102
+
# if already created then only refer and no need to append in queue as it is seen already and processed
0 commit comments