[LeetCode][Python3] 765. Couples Holding Hands
2019. 8. 15. 23:48 |
프로그래밍/LeetCode
Problem :
https://leetcode.com/problems/couples-holding-hands/
My Solution :
class Solution:
def minSwapsCouples(self, row):
ans = 0
pos = {n: i for i, n in enumerate(row)}
for i in range(0, len(row), 2):
left, right = row[i], row[i+1]
if left % 2 == 0:
pair = left + 1
else:
pair = left - 1
if right != pair:
j = pos[pair]
row[i+1], row[j] = row[j], row[i+1]
ans += 1
pos[right] = j
return ans
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 784. Letter Case Permutation (0) | 2019.08.20 |
---|---|
[LeetCode][Python3] 980. Unique Paths III (0) | 2019.08.20 |
[LeetCode][Python3] 47. Permutations II (0) | 2019.08.18 |
[LeetCode][Python3] 1079. Letter Tile Possibilities (0) | 2019.08.16 |
[LeetCode][Python3] 854. K-Similar Strings (0) | 2019.08.15 |
[LeetCode][Python3] 5. Longest Palindromic Substring (0) | 2019.04.30 |
[LeetCode][Python3] 140. Word Break II (0) | 2019.04.25 |
[LeetCode][Python3] 50. Pow(x, n) (0) | 2019.04.24 |
최근에 달린 댓글 최근에 달린 댓글