惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
L
LINUX DO - 最新话题
T
Troy Hunt's Blog
博客园_首页
量子位
Jina AI
Jina AI
S
SegmentFault 最新的问题
IT之家
IT之家
Hacker News - Newest:
Hacker News - Newest: "LLM"
大猫的无限游戏
大猫的无限游戏
N
News | PayPal Newsroom
P
Proofpoint News Feed
Cyberwarzone
Cyberwarzone
S
Securelist
Google Online Security Blog
Google Online Security Blog
P
Privacy International News Feed
博客园 - Franky
美团技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
NISL@THU
NISL@THU
C
Cisco Blogs
V
Vulnerabilities – Threatpost
腾讯CDC
The Hacker News
The Hacker News
K
Kaspersky official blog
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Security Archives - TechRepublic
Security Archives - TechRepublic
A
About on SuperTechFans
Webroot Blog
Webroot Blog
The Register - Security
The Register - Security
Scott Helme
Scott Helme
B
Blog
Security Latest
Security Latest
Last Week in AI
Last Week in AI
Google DeepMind News
Google DeepMind News
W
WeLiveSecurity
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tenable Blog
Blog — PlanetScale
Blog — PlanetScale
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Schneier on Security

博客园 - 北叶青藤

2096. Step-By-Step Directions From a Binary Tree Node to Another Find path from root to a target node in Binary Tree When Dijkstra Algorithm Should be Use? 1188. Design Bounded Blocking Queue 1115. Print FooBar Alternately 1114. Print in Order 1242. Web Crawler Multithreaded Python Multi-threading bot ip Log Rate Limiter Same Word of HTML Labels Most Frequent Call Chain remove prefix in a words list 1102. Path With Maximum Minimum Value minimum number 755. Pour Water Keyword Tagging in Reviews with Overlapping Matches Retryer Function Implementation 1125. Smallest Sufficient Team Print the terrain Split stay Task scheduling problem 滑雪问题 845. Longest Mountain in Array 723. Candy Crush 1539. Kth Missing Positive Number 1650. Lowest Common Ancestor of a Binary Tree III 424. Longest Repeating Character Replacement 843. Guess the Word 551. Student Attendance Record I
Property Booking Optimizer
北叶青藤 · 2026-03-16 · via 博客园 - 北叶青藤

Given:

  • A list of properties where each property has (id, neighborhood, capacity)
  • A group size (number of people that need accommodation)
  • A target neighborhood

Goal:
Find the optimal combination of properties in the given neighborhood that can accommodate the group with these rules:

  1. Total capacity must be >= group size
  2. Choose the combination with minimum total capacity that exceeds group size
  3. If multiple combinations have same capacity, choose the one with fewer properties
  4. If no valid combination exists, return empty list

Examples:

Example 1:
Properties:

  • (1, "area1", 5)
  • (2, "area1", 3)
  • (3, "area1", 2)
  • (4, "area2", 4)
    GroupSize = 5, neighborhood = "area1"
    Output: [1] // Property 1 alone has capacity 5, which is optimal

Example 2:
Same properties, GroupSize = 6, neighborhood = "area1"
Output: [1, 3] // Properties 1+3 give capacity 7, which is minimal solution

Example 3:
Properties:

  • (1, "area1", 5)
  • (2, "area1", 3)
    GroupSize = 10, neighborhood = "area1"
    Output: [] // No combination can accommodate 10 people
 1 def optimize_booking(properties, group_size, target_neighborhood):
 2     """
 3     Finds the optimal combination of properties to accommodate a group.
 4     
 5     Args:
 6         properties: List of (id, neighborhood, capacity)
 7         group_size: Minimum capacity required
 8         target_neighborhood: The neighborhood to search in
 9         
10     Returns:
11         List of property IDs or [] if no valid combination exists.
12     """
13     # 1. Filter properties by neighborhood
14     filtered = [p for p in properties if p[1] == target_neighborhood]
15     
16     if not filtered:
17         return []
18 
19     # 2. DP table: dp[total_capacity] = (property_count, list_of_ids)
20     # We use a dictionary to store the best (minimum property count) combination for every possible capacity sum.
21     dp = {0: (0, [])}
22     
23     for p_id, neighborhood, cap in filtered:
24         new_entries = {}
25         for current_cap, (count, ids) in dp.items():
26             new_cap = current_cap + cap
27             new_count = count + 1
28             new_ids = ids + [p_id]
29             
30             # Update only if this capacity hasn't been reached yet, 
31             # or if we found a way to reach it with fewer properties.
32             if new_cap not in dp or new_count < dp[new_cap][0]:
33                 if new_cap not in new_entries or new_count < new_entries[new_cap][0]:
34                     new_entries[new_cap] = (new_count, new_ids)
35         
36         dp.update(new_entries)
37     
38     # 3. Filter combinations that satisfy the group size
39     candidates = {cap: info for cap, info in dp.items() if cap >= group_size}
40     
41     if not candidates:
42         return []
43     
44     # 4. Find the minimum total capacity
45     min_cap = min(candidates.keys())
46     
47     # 5. Return the IDs (sorted for consistency)
48     best_combination_ids = candidates[min_cap][1]
49     return sorted(best_combination_ids)
50 
51 # --- Test Examples ---
52 properties = [
53     (1, "area1", 5),
54     (2, "area1", 3),
55     (3, "area1", 2),
56     (4, "area2", 4)
57 ]
58 
59 # Example 1: GS = 5, area1 -> Expected [1]
60 print(f"Example 1: {optimize_booking(properties, 5, 'area1')}")
61 
62 # Example 2: GS = 6, area1 -> Expected [1, 3] (Cap 7 is min, 1+3 is better than 1+2 because both are same cap)
63 print(f"Example 2: {optimize_booking(properties, 6, 'area1')}")
64 
65 # Example 3: GS = 10, area1 -> Expected []
66 print(f"Example 3: {optimize_booking(properties, 10, 'area1')}")