Tag Info

26

The algorithm This algorithm creates every combination of ships and tests them one by one, including combinations that could be seen to be useless by the first choice made. For example, if the first choice is to assign a unit of weight 60 to a ship with capacity 50, then it no longer matters what the rest of the content of arrayWithGeneratedNumbers will be, ...

14

Write useful comments carryingCapacities = new ArrayList<Integer>(); // create arraylist for the carrying capacities of ships strengths = new ArrayList<Integer>(); //create arraylists for strengths and weights of units weights = new ArrayList<Integer>(); //create arraylists for weights sc = new Scanner(new File(input)); // load my input ...

9

It appears that you've massively underestimated the complexity of the task. This is a classic Multiple Knapsack problem, and as is the case with all NP-hard problems, the time to solve it and/or the required memory grows very, very, very, very fast with the input size. The choice of algorithm is crucial. It makes no sense to polish the code if the algorithm ...

4

Using lambdas you can use multithread easily, it should improve your results: IntStream.rangeClosed(3, x).parallel().filter(n -> n%3 == 0).count() or, in case you need long values: LongStream.rangeClosed(3L, x).parallel().filter(n -> n%3L == 0L).count() I tested it with the following example: public static void main(String[] args) { ...

3

Consider an iterative path compression design for root. The recursive version uses more memory for stack frames. Typical iterative versions follow the path one at a time. The path can be followed two at a time since the end loops back to itself. The root function could be refactored to be something along the lines of: int root(int a, vector<int> &...

3

for p in permutations(all_vertices, v): $1≤n≤10^5$ Well, $(10^5)! \approx \left(\frac{10^5}{e}\right)^{10^5} \approx 10^{35657}$ so it's a waste of time trying to optimise this. The only thing to do is go back to the drawing board and spend a few hours thinking about the mathematics. At best the code you've written will serve to analyse all ...

3

If I were to compute the answer, I'd do (language shown is C#): var n = 1e8; int v = n / 3; 6 has two multiples of three less than or equal to it: 6 / 3 == 2 127 has 42: 127 / 3 == 42.3333, thus Math.floor(127 / 3) == 42. Likewise, 3 * n will have n multiples of three, including three itself. Rather than having to iterate 1e8 - 2 times in a for-loop (...

2

JJ <- (alpha - 1) %% nrows + 1 II <- ((alpha - JJ)/ncols) + 1 That looks likely to be buggy. I would guess that a is supposed to be an encoding for a pair (row, col), but in that case the same base should be used for the %% and the /. I would also suggest that if you can't use 0-indexed matrices then you do the offset to 1-based when you access ...

2

I couldn't run your code, so I can't say how much faster the following will be. My suggestion is to use NumPy's loadtxt function to get the array of the necessary coordinates. With this function, you can specify skiprows and max_rows parameters to get the necessary rows, 36-45. This should be more efficient than reading all the file in memory. Here: ...

1

$\DeclareMathOperator{\Oh}{O}$Frankly, the code in the HackerRank editor is a large mess. It is promoting crap like #include <bits/stdc++.h> and using namespace std;, and the code contains many problems: int for traversing a std::vector, copying containers around, etc. Not to mention bad practices like i++, explicitly calling fout.close(), not ...

1

From Comments Have you tested performance of copying data of a single day to a temporary table, truncating the target table, and re-inserting the copied data back to the target table? Yes, it's not taking much time to copy or insert, it is taking time to delete and select. One of the column has huge XML string. Proposed Solution Since inserting and ...

1

Use the properties of a BST In a BST, if the value being search for is less than the key of the current node, then it is in the left subtree and if the value is greater than the key of the current node, then it is in the right subtree. Your code always searches the left subtree first and then searches the right subtree if needed. This code only searches ...

1

def __init__(self, n): self.n = n + 1 self.bit = [0] * self.n bit has a well-known interpretation in programming which distracts from the intended interpretation here. IMO even something as generic as data would be better. def update(self, i, k): """Adds k to element with index i """ while i <= self.n - 1: ...

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