6

One immediate improvement is realizing that for e.g the input 14 7, all combinations like x x x x x x 9 and above can never sum to 14, because even for the minimal case x = 1, 9 + 6*1 = 15. So you can vastly reduce the number of combinations you need to check by using k = {i for i in combinations_with_replacement(range(1, x - y + 2), y) if sum(i) == x} ...


5

The comments contain two proofs that a better solution exists. Start with an array of all ones, then repeatedly add one to the smallest number (because that gives the largest percentual increase) until you reach the maximum allowed sum. This can be fomulated as a proof by induction. – Rainer P. This can be proven rather simply by using Python, but it can ...


4

TLDR: it can be done much more quickly and much more concisely, in 28 minutes end-to-end, and in 12 minutes when all XML are already on disk. The key trick: using the long rather than wide format for each data frame. There are two parts to your code: style and performance. Let me post my approach and comment on why I coded it like this. For imports, I ...


4

Welcome to Code Review, here some suggestions about your code: public class review { ... } Java classnames always begin with uppercase letter so rename it to Review. private ArrayList<String> restaurants = new ArrayList<String>(); In java language it is preferable using if possible an interface like List on the left part of the assignment so ...


3

Performance The main source of slowness is the lack of something: when an empty square is found and all possibilities for it have been tried, getSolutionCountRecursively does not return, it tries to fill in some other empty square. Filling in the board in a different order just results in the same boards being created but with a different "move history", ...


3

Overall You don't use encapsulation. Which makes your list vulnerable to incorrect initialization and accidental incorrect modification from outside the list. You use several C based style choice rather than C++ style which make your codde harder to read. Code Review Only a list of int? class Node { public: int value; // int only ...


3

Adding to what @dariosicily mentioned already: Performance If you just need to find out a part-of-speech of each word and do not need to build a phrase-structure tree of sentences, you need only to specify 3 annotations (without parse): props.setProperty("annotators", "tokenize,ssplit,pos"); I assume this can give you a significant boost in performance. ...


3

Doc-string You can add a docstring to the method using the convention of PEP-257 so IDE's etc can parse it and show it when looking at the method. typing By adding type annotations you can make it more clear to the user of the function what input the methods require and what to expect as answers, serving as extra documentation. It also allows static ...


3

I see a number of things that may help you improve your program. Decompose the code into smaller functions This code is very dense, very long and not well organized making it difficult to follow and understand. As a first step, I'd recommend extracting out smaller functions, such as to calculate error values. Each function should be small, well ...


2

You've got inefficiency here: // Sum of a subarray, based on B(x, i, L) -- i is one-indexing public static double sum(double[] x, int i, int L) { return IntStream.range(i, i + L) .parallel() .mapToDouble(idx -> x[idx - 1]) .sum(); } I don't know how big L is, but ...


2

Your function modifies the get/put position of the buffer. I think this is a bug, and it makes benchmarks misleading. Try calling extract twice in a row -- you'll get different results. You set the put position after calling sgetn but you should have set the get position. This should be intuitive. After a read, you "undo" your reading. Fix the bug with ...


2

So, I am assuming that you are aware that a more efficient algorithm for finding prime numbers can be written, so we'll ignore this aspect. Comments to the prime_finder method If we pass num=1 to the method, will it return what you expect? Do you consider 1 a prime number? But even more important, the name of the method is misleading. The method does not ...


2

I don't know why you are surprised plain loops are faster, without an explanation of why you were surprised, it is hard to comment on that. Linq is bound to have some overhead. By vectorisation I guess you mean the use of special machine instructions, I doubt these would have any significant effect here. Most of the comparisons will fail at the first ...


1

Some suggestions: split code more, line by line, so it is easier to profile and see bottlenecks use profvis use %>% less often if that part of code is called frequently, it has some overhead separate function definitions This should run a little bit faster: (15s vs 27s on your example) P_d <- function(x, tau, d){ alpha <- 1/tau l <- sapply(...


1

The nested loop compares each poly to each point, so the complexity is (Npoly * Npts). If the grid is regular, that is each square in a row has the same top and bottom coordinate, and each square in a column has the same left and right coordinate, then binary searches (bisect in std library) could be used to determine which grid row and column a point ...


1

Your current approach of using a nested loop to check every Person's distance from every other Person is O(n ^ 2), which is pretty expensive as the number of people increase. One alternative is to iterate over all infected people and add all coordinates within the infection radius to a Set, O(n). Then iterate over non-infected people and see if their ...


1

If the objective is to avoid duplicating information in memory, the code you have given won't archive it. The new keyword hides the original value. It doesn't replace it. You should try another approximations, like the Decorator pattern: https://en.wikipedia.org/wiki/Decorator_pattern The MasterData inherits from the GlobalData class and stores internally ...


1

1. You never check if head was assigned a valid pointer before dereferencing it here: int find(struct Node *head, int n) { int count = 1; //if count equal too n return node->data if(count == n) return head->value; // <--- here //recursively decrease n and increase // head to next pointer return find(head->...


1

Your major issue here is the algorithm, in which the number of operations grows as the factorial as n. To avoid this you will, I believe, need to go inside the loops that generate permutations. First, I'd like to restate the problem as: if n individuals are seated around a table and then rearranged randomly, is it possible to move them in such a way that ...


1

The next big improvement is also an algorithmic one. Rather than rotating a permutation n times, to check for fixed points, we can instead look the distribution of (index-value) % n. If two different elements of the permutation have the same value x for this, it means that rotating by x will produce 2 fixed points. This means that for each permutation, ...


Only top voted, non community-wiki answers of a minimum length are eligible