Skip to content

Double Sequence Pairing

Table of Contents

2037. Minimum Number of Moves to Seat Everyone

  • LeetCode | LeetCode CH (Easy)

  • Tags: array, greedy, sorting, counting sort

  • Return the minimum number of moves needed to seat everyone.
2037. Minimum Number of Moves to Seat Everyone - Python Solution
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

  • LeetCode | LeetCode CH (Easy)

  • Tags: array, two pointers, greedy, sorting

  • Return the maximum number of your content children that can be satisfied.
455. Assign Cookies - Python Solution
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

2323. Find Minimum Time to Finish All Jobs II

Comments