In the bustling tech hub of Silicon Valley, an ambitious coordinator is trying to assemble the ultimate dream team for an upcoming mega-hackathon. There are developers available, uniquely ranked by their precise skill levels from to . On paper, a massive team sounds perfect—but there's a catch: these developers have massive egos and hypersensitive insecurities.
Every single developer has strict social boundaries about who they will share a table with. Specifically:
lowerSkill[i] team members have a lower skill level than them.higherSkill[i] team members have a higher skill level than them.If even one chosen developer feels uncomfortable with the final team line-up, they won't sign up. Your mission as the coordinator is to find the absolute largest group of developers you can bring into the room such that everyone sits down, looks around, and happily agrees to stay.
Imagine you have 5 developers. Their tolerance thresholds are:
lowerSkill = [1, 3, 2, 2, 2]higherSkill = [2, 2, 1, 1, 3]If you greedily pick all 5, the expert at rank 5 looks down, sees 4 people below him, screams "Too many amateurs!" (since his limit was 2), and leaves. After a series of dramatic exits, you realize the magic number is 3. By inviting only developers 1, 3, and 4, a perfect harmony is achieved. Developer 1 tolerates the 2 superiors; Developer 3 is perfectly balanced with 1 junior and 1 senior; and Developer 4 happily accepts having 2 juniors below him.
Binary Search, Greedy, Sequence MatchingWelcome to the lawless roads of Hackerland, a country made up of isolated cities interconnected by a web of bidirectional highways. You are a legendary long-haul trucker tasked with delivering a high-value cargo from City to City . Your rig is a mechanical marvel: it has an unlimited fuel tank capacity, but it drinks fuel like water. Traveling down any highway requires a specific number of fuel units, given by the road's weight.
The problem? The corporate fuel syndicates control the prices, and every city charges wildly different rates per unit of fuel, tracked in the global arr index. Some cities are dirt cheap hubs; others are highway robbery.
Fortunately, since your fuel tank is infinite, you can play the market. If you know an expensive mountain pass is coming up later in your journey, you don't have to buy gas at the base of the mountain. If you stumbled across a cheap fuel haven three towns ago, you could have filled your infinite tank to the brim right then and there to cover the rest of your trip!
Your goal is to map out a route and a refueling strategy that gets you to your destination with the absolute minimum dent in your wallet. If the highways are broken and there's no path forward, you'll have to radio in a failure.
You need to get from City 3 to City 2. The local prices are:
arr = [9, 11, 3, 2, 10] (City 4 is incredibly cheap at , while City 3 is ).You start at City 3. You could drive straight through the expensive tollways, but your GPS plots a clever detour: .
Graph, Shortest Path, Dijkstra's Algorithm, Floyd-Warshallcurrent_city, but a pair: (current_city, cheapest_fuel_found_so_far).