Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a57f972

Browse filesBrowse files
committed
Weekly contest 223
1 parent 0f14d29 commit a57f972
Copy full SHA for a57f972

File tree

Expand file treeCollapse file tree

3 files changed

+89
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+89
-0
lines changed

‎Tag/LeetcodeTips.md

Copy file name to clipboard
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[TOC]
2+
3+
# Basic knowledge
4+
## Hanming distance
5+
For two arraies `nums1` and `nums2`, Hanming distance indicates how many elements in these two arraies are different.
6+
Formally, it is the number of indices `i` for `0 <= i <= n-1` where `nums1[i] != nums2[i]`
7+
8+
**Examples:**
9+
```Python
10+
>>> a = [1,2,2]
11+
>>> b = [1,1,2]
12+
>>> HanmingDistance(a,b)
13+
1
14+
15+
>>> a = [1,2,2]
16+
>>> b = [2,1,4]
17+
>>> HanmingDistance(a,b)
18+
3
19+
```

‎WeeklyContest/README.md

Copy file name to clipboardExpand all lines: WeeklyContest/README.md
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
|Date|Contest|Problem|Complexity|Solution|
55
|---|---|---|---|---|
6+
|2021/01/10|WC223_03|[Minimize Hamming Distance After Swap Operations](https://leetcode-cn.com/problems/minimize-hamming-distance-after-swap-operations/)| M | [Python](https://github.com/codename1995/leetcodehub/blob/master/python/WC223_03_Minimize_Hamming_Distance_After_Swap_Operations.py) 并查集典型应用|
7+
||WC223_01|[Decode xored array](https://leetcode-cn.com/problems/decode-xored-array/) |E| a xor b = c </br> 异或的逆运算: </br> Python: a = - ( ~(c xor b) + 1)
68
|2020/04/19|WC185_04|[Build Array Where You Can Find The Maximum Exactly K Comparisons](https://leetcode-cn.com/problems/build-array-where-you-can-find-the-maximum-exactly-k-comparisons/) |H| [C++](https://github.com/codename1995/leetcodehub/blob/master/WeeklyContest/WCCPP/WC185_04_Build_Array_Where_You_Can_Find_The_Maximum_Exactly_K_Comparisons/WC185_04_Build_Array_Where_You_Can_Find_The_Maximum_Exactly_K_Comparisons.cpp) 三维动态规划|
79
|2020/04/12|WC184|第二次全A,但是是因为题目比较简单|||
810
||WC184_03|[HTML entity parser](https://leetcode-cn.com//problems/html-entity-parser/)|M|转义字符不熟悉|
@@ -151,6 +153,7 @@
151153
#### Rank
152154
|No. | Rank| Percent|
153155
|---|---|---|
156+
|WC223 |1394/3870 |36.0%|
154157
|WC222 |547/3117 |17.5%|
155158
|WC221 |1003/3397 |29.5%|
156159
|WC185 |956/5001 |19.1%|
+67Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import math
2+
import collections
3+
4+
5+
class Solution:
6+
def minimumHammingDistance(self, source, target, allowedSwaps):
7+
# 参考大佬解法:https://leetcode-cn.com/problems/minimize-hamming-distance-after-swap-operations/solution/python3-bing-cha-ji-ha-xi-biao-by-yim-6-4a03/
8+
n = len(source)
9+
parent = {i:i for i in range(n)}
10+
11+
# 定义并查集寻根算法
12+
def find(x):
13+
if x!=parent[x]:
14+
parent[x] = find(parent[x])
15+
return parent[x]
16+
17+
# 进行连通
18+
for x,y in allowedSwaps:
19+
rootX, rootY = find(x), find(y)
20+
if rootX!=rootY:
21+
parent[rootY] = rootX
22+
23+
# 统计连通结果
24+
dic = collections.defaultdict(list)
25+
for i in range(n):
26+
root = find(i)
27+
dic[root].append(i)
28+
29+
res = 0
30+
for k,v in dic.items():
31+
a = [source[i] for i in v]
32+
b = [target[i] for i in v]
33+
Counter = collections.Counter(b)
34+
for c in a:
35+
if Counter[c]:
36+
Counter[c] -= 1
37+
else:
38+
res += 1
39+
40+
return res
41+
42+
43+
44+
45+
46+
import time
47+
start = time.clock()
48+
# Time restriction
49+
# Simple case
50+
source = [1,2,3,4]
51+
target = [2,1,4,5]
52+
allowedSwaps = [[0,1],[2,3]]
53+
ExpectOutput = 1
54+
# nums = [8975,9106,5349,7891,3795,983,3532,3121,9341,6891,5416,6221,1247,5343,7006]
55+
# ExpectOutput = 10
56+
57+
solu = Solution() # 先对类初始化,才能进行调用
58+
# temp = solu.removeSubfolders(s)
59+
temp = solu.minimumHammingDistance(source, target, allowedSwaps)
60+
if (temp == ExpectOutput):
61+
print('right')
62+
else:
63+
print('wrong')
64+
print(temp)
65+
66+
elapsed = (time.clock() - start)
67+
print("Time used:", elapsed)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.