Skip to content

Commit f255827

Browse files
My solution for 1460
1 parent da3d4ec commit f255827

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

problems/1460/jeremymanning.md

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
11
# [Problem 1460: Make Two Arrays Equal by Reversing Subarrays](https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/description/?envType=daily-question)
22

33
## Initial thoughts (stream-of-consciousness)
4+
- There are two ways to solve this:
5+
- We could sort both arrays and then compare each corresponding element in turn. This would take $O(n \log n)$ time.
6+
- We could count the number of occurances of each unique element, store in a hash table, and ensure that the keys/values all match. This would take $O(n)$ time (i.e., faster). So let's go with this approach.
47

58
## Refining the problem, round 2 thoughts
9+
- No special cases to deal with...
610

711
## Attempted solution(s)
812
```python
9-
class Solution: # paste your code here!
10-
...
13+
class Solution:
14+
def canBeEqual(self, target: List[int], arr: List[int]) -> bool:
15+
target_count = {}
16+
arr_count = {}
17+
for t, a in zip(target, arr):
18+
if t in target_count:
19+
target_count[t] += 1
20+
else:
21+
target_count[t] = 1
22+
23+
if a in arr_count:
24+
arr_count[a] += 1
25+
else:
26+
arr_count[a] = 1
27+
28+
for t, t_count in target_count.items():
29+
if (t not in arr_count) or (target_count[t] != arr_count[t]):
30+
return False
31+
32+
return True
1133
```
34+
- Given test cases pass; submitting...
35+
36+
![Screenshot 2024-08-02 at 8 12 24 PM](https://github.com/user-attachments/assets/0d1fd1fe-1937-4d81-8a2e-d55f3ce42dab)
37+
38+
Solved!

0 commit comments

Comments
 (0)