TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

This problem (reportedly) took Don Knuth, 24 hours to solve

1 点作者 soham超过 9 年前

1 comment

informatimago超过 9 年前
I don&#x27;t know what was Knuth&#x27;s solution, but it&#x27;s trivial:<p><pre><code> (- (reduce &#x27;+ vector) (let ((n (- (length vector) 2))) (&#x2F; (* n (+ n 1)) 2))) </code></pre> reduce is O(n) time and O(1) space. length is O(1) time and space.<p>Notice also that if there are no duplicates, this formula will give the maximum element (- (length vector) 1).