Skip to content

Double Sequence Pairing

Table of Contents

2037. Minimum Number of Moves to Seat Everyone

"""
-   Return the minimum number of moves needed to seat everyone.
"""

from typing import List


# Greedy
def minMovesToSeat(seats: List[int], students: List[int]) -> int:
    seats.sort()
    students.sort()
    moves = 0

    for i, j in zip(seats, students):
        moves += abs(i - j)

    return moves


print(minMovesToSeat([3, 1, 5], [2, 7, 4]))  # 4

455. Assign Cookies

"""
-   Return the maximum number of your content children that can be satisfied.
"""

from typing import List


# Greedy
def findContentChildren(g: List[int], s: List[int]) -> int:
    g.sort()
    s.sort()
    i, j = 0, 0

    while i < len(g) and j < len(s):
        if g[i] <= s[j]:
            i += 1
        j += 1

    return i


# |-------------|-------------|--------------|
# |   Approach  |    Time     |    Space     |
# |-------------|-------------|--------------|
# |   Greedy    | O(N * logN) |    O(1)      |
# |-------------|-------------|--------------|


g = [1, 2, 3]
s = [1, 1]
print(findContentChildren(g, s))  # 1

2410. Maximum Matching of Players With Trainers

1433. Check If a String Can Break Another String

870. Advantage Shuffle

826. Most Profit Assigning Work

2449. Minimum Number of Operations to Make Arrays Similar

1889. Minimum Space Wasted From Packaging

2561. Rearranging Fruits

from collections import defaultdict
from typing import List


def minCost(basket1: List[int], basket2: List[int]) -> int:
    cnt = defaultdict(int)
    for x, y in zip(basket1, basket2):
        cnt[x] += 1
        cnt[y] -= 1

    a, b = [], []
    for x, c in cnt.items():
        if c % 2:
            return -1

        if c > 0:
            a.extend([x] * (c // 2))
        else:
            b.extend([x] * (-c // 2))

    a.sort()
    b.sort(reverse=True)
    mn = min(cnt)

    return sum(min(x, y, mn * 2) for x, y in zip(a, b))


if __name__ == "__main__":
    basket1 = [4, 2, 2, 2]
    basket2 = [1, 4, 1, 2]
    assert minCost(basket1, basket2) == 1

2323. Find Minimum Time to Finish All Jobs II

Comments