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

推荐订阅源

L
LangChain Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
B
Blog
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
D
DataBreaches.Net
博客园 - Franky
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
腾讯CDC
雷峰网
雷峰网
爱范儿
爱范儿
Hugging Face - Blog
Hugging Face - Blog
D
Docker
The Cloudflare Blog
M
MIT News - Artificial intelligence
量子位
F
Full Disclosure
T
The Blog of Author Tim Ferriss
Jina AI
Jina AI
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
InfoQ
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
Microsoft Azure Blog
Microsoft Azure Blog
L
LINUX DO - 最新话题
V
Visual Studio Blog
T
Troy Hunt's Blog
Vercel News
Vercel News
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cisco Talos Blog
Cisco Talos Blog
Webroot Blog
Webroot Blog
W
WeLiveSecurity
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
月光博客
月光博客
GbyAI
GbyAI
D
Darknet – Hacking Tools, Hacker News & Cyber Security
N
News and Events Feed by Topic
小众软件
小众软件
Last Week in AI
Last Week in AI
Martin Fowler
Martin Fowler
G
Google Developers Blog
P
Privacy International News Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
Threat Research - Cisco Blogs
大猫的无限游戏
大猫的无限游戏
MyScale Blog
MyScale Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org

博客园 - 北叶青藤

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')}")