Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options answers only not deleted user 39242

Fastest-algorithm competitions are won by the answer with the smallest asymptotic time complexity. For challenges based on actual runtime, use [fastest-code] instead.

4 votes
Accepted

Optimizing polygon

Python, \$\mathcal O(n^2)\$ def best_pentagon(grid): best = float("-inf"), -1, -1, -1 rects = [0] * len(grid[0]) pents = [(0, 0)] * len(grid[0]) for y, row in enumerate(grid): …
Anders Kaseorg's user avatar
5 votes

Compute how many are no larger than each item in an array

Python, \$O(n \log n)\$ Simple algorithm based on merge sort that runs in \$O(n \log n)\$ for inputs in any range (assuming comparisons are \$O(1)\$). def counts_by_value(array: list[int]) -> list[tup …
Anders Kaseorg's user avatar
5 votes
Accepted

Remove entries from array to sort it and maximize sum of elements

Haskell, \$O(n \log n)\$ time, \$O(n)\$ space {-# LANGUAGE MultiParamTypeClasses #-} import qualified Data.FingerTree as F data S = S { sSum :: Int , sArr :: [Int] } deriving (Show) instance …
Anders Kaseorg's user avatar
2 votes

Total Derangement (Difficulty Level: Hard)

Python 3, \$O((n - 1)!^{m - 1}nm)\$ expected time This is a reference answer using a very straightforward and slow rejection sampling algorithm, for the sake of having at least one valid answer. It pi …
Anders Kaseorg's user avatar