File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change
1
+ ## 3Sum With Multiplicity
2
+
3
+ #### Description
4
+
5
+ [ link] ( https://leetcode.com/problems/927 )
6
+
7
+ ---
8
+
9
+ #### Solution
10
+
11
+ - See Code
12
+
13
+ ---
14
+
15
+ #### Code
16
+
17
+ > Complexity T : O(n) M : O(n)
18
+
19
+ ``` python
20
+ class Solution :
21
+ def threeEqualParts (self , A : List[int ]) -> List[int ]:
22
+
23
+ num_ones = sum (A)
24
+
25
+ if num_ones == 0 :
26
+ return [0 , 2 ]
27
+
28
+ if num_ones % 3 != 0 :
29
+ return [- 1 , - 1 ]
30
+
31
+ c = 0
32
+ s1 = s2 = s3 = - 1
33
+ for idx,x in enumerate (A):
34
+ # Find the first 1 in each part
35
+ if x == 1 :
36
+ c += 1
37
+
38
+ if c == 1 and s1 < 0 :
39
+ s1 = idx
40
+
41
+ if c == num_ones// 3 + 1 and s2 < 0 :
42
+ s2 = idx
43
+
44
+ if c == num_ones* 2 // 3 + 1 and s3 < 0 :
45
+ s3 = idx
46
+ break
47
+
48
+ n = len (A[s3:]) # The length of each part when all the leading 0's are removed
49
+
50
+ if A[s1:s1+ n] == A[s2:s2+ n] and A[s2:s2+ n] == A[s3:]:
51
+ return [s1+ n- 1 , s2+ n]
52
+ else :
53
+ return [- 1 , - 1 ]
54
+ ```
You can’t perform that action at this time.
0 commit comments