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 35991

When the code scales so poorly in the face of large inputs that it cannot complete in a reasonable amount of time, use this instead of the [performance] tag.

4 votes
Accepted

Practical number algorithm

Some points in addition to what Roland already said: You are correct that the readLine() returns an optional and you need to ensure that it is not nil. However, nil is only returned on an end-of-file …
Martin R's user avatar
  • 23.5k
4 votes
Accepted

Target Sum leetcode

Your solution computes all possible target sums that are obtained by distributing the signs \$+1\$ and \$-1\$ to the numbers. For an array with \$n\$ numbers that are \$2^n\$ combinations. This is a …
Martin R's user avatar
  • 23.5k
3 votes
Accepted

Add a value to each of the array element between two given indices successively

Some general remarks: System.out.println(queryLength); The function should only return the result, but not print anything. int a = 0 , b = 0 , k = 0; The initial values are not used, and var …
Martin R's user avatar
  • 23.5k
3 votes

Programming challenge “Greatest Odd Divisor”

First some remarks to your code (in addition to what @200_success already said): Use more horizontal space and make the spacing consistent. if (num % i == 0 && i % 2 != 0) is better readable than i …
Martin R's user avatar
  • 23.5k
7 votes

Obtaining a target number only using the operations ×2, ×3, and +1

For each i = 0...number, your method stores a std::list containing the complete sequence of intermediate numbers from 1 to i. Each list is created by copying the list from the optimal predecessor and …
Martin R's user avatar
  • 23.5k
11 votes

Odd divisor summation

First I would suggest to separate the code into two functions: One which computes the sum of odd divisors of a single number, and another which adds the odd divisors of all numbers in a list. Also, us …
Martin R's user avatar
  • 23.5k
4 votes
Accepted

Finding decreasing sequences in an array of numbers

Some general remarks: Don't use namespace std;, see for example Why is “using namespace std;” considered bad practice?. Define variables at the narrowest possible scope. In particular, avoid global …
Martin R's user avatar
  • 23.5k
7 votes
Accepted

Minimum window substring - LeetCode challenge

Performance The main culprit for exceeding the time limit is here: while end < s.count { ... } A Swift string is a Collection but not a RandomAccessCollection, so that the complexity of var count …
Martin R's user avatar
  • 23.5k
1 vote
Accepted

Minimum Window Substring in Swift exceeds LeetCode runtime check

The main bottleneck is how characters of a string are accessed. A Swift String is a collection of Characters, and a Character represents a single extended grapheme cluster, which can be one or more Un …
Martin R's user avatar
  • 23.5k
3 votes
Accepted

Checking whether strings are permutations of each other in Swift

First some general remarks: There is no need to terminate statements with a semicolon in Swift. The condition in an if-statement does not require parentheses, e.g. if debug { ... } You should be c …
Martin R's user avatar
  • 23.5k
3 votes

Swift solution to Leetcode “Longest Substring Without Repeating Characters”

The Swift standard library does not have a built-in method to access the n'th character of a string. It is tempting to fill that gap with a custom extension like you did, but the problem is that self …
Martin R's user avatar
  • 23.5k
8 votes
Accepted

Project Euler problem 14 (longest Collatz sequence) in Swift 3

The main performance bottleneck of your code was already pointed out in the other answers: building lots of arrays (without need). I'll like to point out how some Swift techniques can be used to achie …
Martin R's user avatar
  • 23.5k
1 vote
Accepted

Sums for list of ranges (CodeFights Basic Skills)

First let's simplify your existing code. You already use reduce() to add the elements of sumArray, the same can be done to replace var tempSum = 0 for i in q[0]...q[1] { tempSum += …
Martin R's user avatar
  • 23.5k
4 votes
Accepted

Outputting prime numbers in given ranges

Before addressing the performance problem I would like to make some general remarks: You validate the input values (t, m, n), but invalid numbers are simply ignored. For a programming challenge I wo …
Martin R's user avatar
  • 23.5k
3 votes
Accepted

LeetCode - Weekly Contest 213 - Kth Smallest Instructions

A review of your current code class Solution uses the fields String res = string.Empty; int count = 0; int K; to pass information around between the “main” KthSmallestPath() function and the FindAllP …
Martin R's user avatar
  • 23.5k

15 30 50 per page