Problem 1: Profitable Project Pairs
Description
Given two integer lists Profit
and Cost
, count the number of index pairs (i, j)
such that i < j
and (Profit[i] - Cost[i]) + (Profit[j] - Cost[j]) > 0
.
A brute-force O(n²) approach would time out, so we use the following optimized approach:
- Compute
net_profit[i] = Profit[i] - Cost[i]
- Sort the
net_profit
list - For each index
i
, use binary search to find how manyj > i
satisfynet_profit[i] + net_profit[j] > 0
Function Signature
def count_profitable_pairs(Profit: List[int], Cost: List[int]) -> int:
Example
Profit = [5, 3, 2]
Cost = [2, 1, 4]
# net_profit = [3, 2, -2]
# Sorted = [-2, 2, 3]
# Valid pairs: (1, 2), (0, 1), (0, 2)
# Output: 3
Problem 2: Maximum Swaps
Description
Given two lists a
and b
, and an integer k
(maximum number of swaps allowed), return the maximum number of unique elements that can be in list a
after at most k
swaps from list b
.
Steps:
- Compute elements in
b
not ina
:set(b) - set(a)
- Compute the number of duplicate elements in
a
:len(a) - len(set(a))
- The maximum possible unique elements we can add is
min(k, len(b_not_in_a), duplicates_in_a)
- Result is
len(set(a)) + swaps_used
Function Signature
def max_unique_after_swaps(a: List[int], b: List[int], k: int) -> int:
Example
a = [1, 2, 2, 3]
b = [3, 4, 5]
k = 2
# set(a) = {1, 2, 3}, set(b) - set(a) = {4, 5}
# duplicates = 1, max usable swaps = min(2, 2, 1) = 1
# result = 3 + 1 = 4
Problem 3: Binary Circuit
Description
Given a binary string (composed of '0' and '1'), calculate the total cost to move all 1
s to the rightmost part of the string.
Rules:
- Only the leftmost block of
1
s can be moved - Move the rightmost
1
from the leftmost group to immediately before the next1
- Cost =
1 + distance_moved
Use dynamic programming or greedy counting:
- Traverse left to right, for each
0
, add current number of1
s before it to cost - If a
0
is the first one after a1
, add an extra1
to cost
Function Signature
def binary_circuit_cost(s: str) -> int:
Example
s = "1100101"
# Steps:
# 1 -> count_ones = 2
# cost += count_ones at each '0'
# total cost = 2 (first 0) + 2 (second 0) + 3 (third 0 after more 1s) = 7
# Output: 7
我们长期稳定承接各大科技公司如TikTok、Google、Amazon等的OA笔试代写服务,确保满分通过。如有需求,请随时联系我们。
We consistently provide professional online assessment services for major tech companies like TikTok, Google, and Amazon, guaranteeing perfect scores. Feel free to contact us if you're interested.
