algorithms

Algorithm implementations
git clone git://git.laack.co/algorithms.git
Log | Files | Refs | README

commit f6f567002b8b31de257de3eaeda8fa9a7e3c524e
parent b027200d789669f827c66acbb1531882a457b492
Author: Andrew Laack <andrew@laack.co>
Date:   Wed, 19 Aug 2026 21:32:17 -0500

Completed a few more. I should read directions better...

Diffstat:
Acinema-seat-allocation/cinema-seat-allocation-v2.py | 40++++++++++++++++++++++++++++++++++++++++
Acinema-seat-allocation/cinema-seat-allocation-v3.py | 43+++++++++++++++++++++++++++++++++++++++++++
Acinema-seat-allocation/cinema-seat-allocation-v4.py | 38++++++++++++++++++++++++++++++++++++++
Acinema-seat-allocation/cinema-seat-allocation-v5.py | 36++++++++++++++++++++++++++++++++++++
Acinema-seat-allocation/cinema-seat-allocation.py | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aremove-trailing-zeroes/remove-trailing-zeroes.cpp | 14++++++++++++++
Areverse-integer/reverse-integer-2.cpp | 29+++++++++++++++++++++++++++++
7 files changed, 258 insertions(+), 0 deletions(-)

diff --git a/cinema-seat-allocation/cinema-seat-allocation-v2.py b/cinema-seat-allocation/cinema-seat-allocation-v2.py @@ -0,0 +1,40 @@ +class Solution: + def maxNumberOfFamilies(self, n: int, reservedSeats: List[List[int]]) -> int: + + reservedSeats.sort() + count = 0 + seat_index = 0 + + for row_num in range(1, n + 1): + + first_seats = True + second_seats = True + third_seats = True + + if seat_index < len(reservedSeats): + current_reserved = reservedSeats[seat_index] + + while seat_index < len(reservedSeats) and current_reserved[0] == row_num: + if current_reserved[1] >= 2 and current_reserved[1] <= 5: + first_seats = False + if current_reserved[1] >= 4 and current_reserved[1] <= 7: + second_seats = False + if current_reserved[1] >= 6 and current_reserved[1] <= 9: + third_seats = False + + seat_index += 1 + + if seat_index < len(reservedSeats): + current_reserved = reservedSeats[seat_index] + + + if first_seats and second_seats: + second_seats = False + if second_seats and third_seats: + third_seats = False + + if first_seats: count += 1 + if second_seats: count += 1 + if third_seats: count += 1 + + return count diff --git a/cinema-seat-allocation/cinema-seat-allocation-v3.py b/cinema-seat-allocation/cinema-seat-allocation-v3.py @@ -0,0 +1,43 @@ +class Solution: + + def start_from_seat(self, num): + to_remove = [] + if num >= 2 and num <= 5: + to_remove.append(2) + if num >= 4 and num <= 7: + to_remove.append(4) + if num >= 6 and num <= 9: + to_remove.append(6) + return to_remove + + def maxNumberOfFamilies(self, n: int, reservedSeats: List[List[int]]) -> int: + + valid = set() + + occupied_rows = set() + + for seat in reservedSeats: + occupied_rows.add(seat[0]) + + + + for y in range(1,n+1): + if y not in occupied_rows: + continue + + valid.add(str([y,2])) + valid.add(str([y,4])) + valid.add(str([y,6])) + + for seat in reservedSeats: + to_remove = self.start_from_seat(seat[1]) + for start in to_remove: + valid.discard(str([seat[0],start])) + + for y in range(1,n+1): + if str([y,2]) in valid and str([y,4]) in valid: + valid.discard(str([y,4])) + if str([y,4]) in valid and str([y,6]) in valid: + valid.discard(str([y,6])) + + return len(valid) + ((n - len(occupied_rows)) * 2) diff --git a/cinema-seat-allocation/cinema-seat-allocation-v4.py b/cinema-seat-allocation/cinema-seat-allocation-v4.py @@ -0,0 +1,38 @@ +# ISSUE: Only need to track bitmask for items that have elements +class Solution: + def maxNumberOfFamilies(self, n: int, reservedSeats: List[List[int]]) -> int: + bitmask = [] + + for i in range(n): + # leftmost and rightmost don't matter + bitmask.append(0b00000000) + + for seat in reservedSeats: + if seat[1] != 10 and seat[1] != 1: + bitmask[seat[0] - 2] = bitmask[seat[0] - 2] | 2**(seat[1] - 2) + + mask_first = 0b11110000 + mask_second =0b00111100 + mask_third = 0b00001111 + + count = 0 + + for i in range(n): + current = bitmask[i] + + first = False + second = False + + + if current & mask_first == 0: + count += 1 + first = True + + if not first and current & mask_second == 0: + count += 1 + second = True + + if not second and current & mask_third == 0: + count += 1 + + return count diff --git a/cinema-seat-allocation/cinema-seat-allocation-v5.py b/cinema-seat-allocation/cinema-seat-allocation-v5.py @@ -0,0 +1,36 @@ +class Solution: + def maxNumberOfFamilies(self, n: int, reservedSeats: List[List[int]]) -> int: + + bitmask = {} + + for seat in reservedSeats: + if bitmask.get(seat[0] - 1) is None: + bitmask[seat[0] - 1] = 0b0000000000 + bitmask[seat[0] - 1] = bitmask[seat[0] - 1] | 2**(seat[1] - 1) + + mask_first = 0b0111100000 + mask_second = 0b0001111000 + mask_third = 0b0000011110 + + count = (n - len(bitmask)) * 2 + + for i in bitmask: + + current = bitmask[i] + + first = False + second = False + + + if current & mask_first == 0: + count += 1 + first = True + + if not first and current & mask_second == 0: + count += 1 + second = True + + if not second and current & mask_third == 0: + count += 1 + + return count diff --git a/cinema-seat-allocation/cinema-seat-allocation.py b/cinema-seat-allocation/cinema-seat-allocation.py @@ -0,0 +1,58 @@ +SEATS_PER_ROW = 10 +NUM_PEOPLE = 4 + +def compute(n, reservedSeatsDict): + + result = 0 + + for y in range(1, n+1): + + running = 0 + row = reservedSeatsDict.get(y) + + if row is None: + result += 2 + continue + + valid_first = {2,4,6} + + for x in range(1, SEATS_PER_ROW + 1): + if x in valid_first or running > 0: + if not x in row: + running += 1 + else: + running = 0 + continue + + if running == NUM_PEOPLE: + running = 0 + result += 1 + + return result + +def reserved_dict(reservedSeats): + + result = {} + current_row = {} + current_row_num = -1 + + for seat in reservedSeats: + if seat[0] != current_row_num: + current_row = result.get(seat[0]) + current_row_num = seat[0] + + if current_row is None: + result[seat[0]] = {seat[1]} + current_row = result[seat[0]] + else: + # current_row is a set so it is a reference. + current_row.add(seat[1]) + + return result + +class Solution: + + def maxNumberOfFamilies(self, n: int, reservedSeats: List[List[int]]) -> int: + reservedSeats.sort() + reservedSeatsDict = reserved_dict(reservedSeats) + return compute(n, reservedSeatsDict) diff --git a/remove-trailing-zeroes/remove-trailing-zeroes.cpp b/remove-trailing-zeroes/remove-trailing-zeroes.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + string removeTrailingZeros(string num) { + int properLength = num.length() - 1; + + while (num[properLength] == '0'){ + properLength -= 1; + } + + num.resize(properLength + 1); + + return num; + } +}; diff --git a/reverse-integer/reverse-integer-2.cpp b/reverse-integer/reverse-integer-2.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int reverse(int x) { + + int result = 0; + bool positive = true; + + if (x < -INT_MAX) return 0; + + if (x < 0) positive = false, x *= -1; + + while (x > 0) { + int residual = x % 10; + x /= 10; + int prior_result = result; + + if (INT_MAX / 10 < result) return 0; + + result *= 10; + result += residual; + } + + if (!positive) { + result *= -1; + } + + return result; + } +};