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

推荐订阅源

V
Visual Studio Blog
阮一峰的网络日志
阮一峰的网络日志
博客园_首页
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
罗磊的独立博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 【当耐特】
T
Tailwind CSS Blog
美团技术团队
Y
Y Combinator Blog
I
InfoQ
C
Check Point Blog
Microsoft Security Blog
Microsoft Security Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
博客园 - Franky
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
爱范儿
爱范儿
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
A
Arctic Wolf
Hugging Face - Blog
Hugging Face - Blog
S
Security Affairs
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
小众软件
小众软件
云风的 BLOG
云风的 BLOG
酷 壳 – CoolShell
酷 壳 – CoolShell
Recent Announcements
Recent Announcements
H
Heimdal Security Blog
博客园 - 司徒正美
Latest news
Latest news
H
Hacker News: Front Page
H
Help Net Security
Know Your Adversary
Know Your Adversary
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Secure Thoughts
AWS News Blog
AWS News Blog
V
Vulnerabilities – Threatpost
NISL@THU
NISL@THU
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog
The GitHub Blog
The GitHub Blog
有赞技术团队
有赞技术团队
The Cloudflare Blog
I
Intezer
N
News and Events Feed by Topic

博客园 - 北叶青藤

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 Property Booking Optimizer 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
minimum number
北叶青藤 · 2026-03-02 · via 博客园 - 北叶青藤

part 1

给一串0到9的数字,返回最小可以组成的整数,以string返回

比如 [1, 3, 3, 4, 2] -> "12334"

所有数字用一遍,0除外,比如[0, 1, 2]就返回12

 Instead of sorting the entire array, we count the occurrences of each digit. Since we only care about 1–9, we iterate through those keys in order to build our string.

 1 def smallest_int_optimized(digits):
 2     # Step 1: Count frequencies - O(N)
 3     counts = [0] * 10
 4     for d in digits:
 5         counts[d] += 1
 6     
 7     # Step 2: Build string from 1 to 9 - O(1) (fixed number of digits)
 8     res = []
 9     for d in range(1, 10):
10         res.append(str(d) * counts[d])
11         
12     return "".join(res)

 part 2

part1的基础上返回值要大于或等于一个lower bound

比如 [7, 1, 8], lower bound = 719,返回781

 1 from collections import Counter
 2 
 3 def find_min_greater_equal_counter(nums, lower_bound):
 4     s_bound = str(lower_bound)
 5     n = len(s_bound)
 6     # 统计每个数字出现的频率
 7     counts = Counter(nums)
 8     # 获取去重后的有序数字列表
 9     unique_digits = sorted(counts.keys())
10     
11     def solve(index, is_greater):
12         if index == n:
13             return ""
14 
15         target = int(s_bound[index])
16         
17         for d in unique_digits:
18             if counts[d] > 0:
19                 # 剪枝:如果还没超越 bound 且当前数字太小,跳过
20                 if not is_greater and d < target:
21                     continue
22                 
23                 # 尝试放置数字 d
24                 counts[d] -= 1
25                 res = solve(index + 1, is_greater or (d > target))
26                 
27                 if res is not None:
28                     return str(d) + res
29                 
30                 # 回溯
31                 counts[d] += 1
32         
33         return None
34 
35     return solve(0, False)