pseudocode

Kth largest element in a max-heap

不羁的心 提交于 2019-11-30 12:06:08
问题 I'm trying to come up with something to solve the following: Given a max-heap represented as an array, return the kth largest element without modifying the heap. I was asked to do it in linear time, but was told it can be done in log time. I thought of a solution: Use a second max-heap and fill it with k or k+1 values into it (breadth first traversal into the original one) then pop k elements and get the desired one. I suppose this should be O(N+logN) = O(N) Is there a better solution,

Computing All The Possible Substrings of a Given String [duplicate]

Deadly 提交于 2019-11-30 10:15:55
Possible Duplicate: How to find all substrings of a string in PHP Find all subsets of a list How can I compute all the possible substrings of a string? For example given a string ABCDE. All its possible substrings will be A, B, C, D, E, AB, BC, CD, DE, ABC, BCD, CDE, ABCD, BCDE, ABCDE Thanks! A pseudocode will be highly appreciated. :D Just use two for-loops: generate substrings(string): for start in [0,1,...,string.length-1]: for end in [start,...,string.length-1]: yield string[start...end] You can also do it this way with two for-loops: generate substrings(string): for substringLength in [1

Algorithm to reach a number in a fixed amount of steps using addition, division and multiplication only

孤街醉人 提交于 2019-11-30 08:52:53
Working on a game at work and at one point in the game the player is tossed into a bonus game. The amount they need to win is predetermined, however we'd like to come up with an algorithm which uses addition, multiplication and division to get to that amount in x amount of steps. The amount of steps would be known ahead of time as well, so the algorithm would just need to figure out how to use that amount of steps to reach the number. The only computations you can use are +1 through +15, x2, x4, /2, /4. You can exceed the target number during the steps, but must end up at the target number on

How to distribute points evenly on the surface of hyperspheres in higher dimensions?

醉酒当歌 提交于 2019-11-30 07:53:48
问题 I am interested in evenly distributing N points on the surface of spheres in dimensions 3 and higher. To be more specific: Given a number of points N and number of dimensions D (where D > 1, N > 1) The distance of every point to the origin must be 1 The minimum distance between any two points should be as large as possible The distance of each point to it's closest neighbour doesn't necessarily have to be the same for every point (indeed it's not possible for it to be the same unless the

uml classdiagram constructor with parameters

♀尐吖头ヾ 提交于 2019-11-30 06:34:00
问题 I am a complete ROOKIE at this so I need some help on it. How would you create uml class diagram and constructors with parameters. for default (no parameters) you do policyholder() for diagram and pseudo-code for parameters would you do the same thing policyholder (policynumber, service class, and customer age) for class diagrams and pseudo-code. It also asked to initialize each attribute to value where an object of this type can be instantiated, If a policy number is not between 1000 and 999

Finding shortest repeating cycle in word?

社会主义新天地 提交于 2019-11-30 06:32:41
问题 I'm about to write a function which, would return me a shortest period of group of letters which would eventually create the given word. For example word abkebabkebabkeb is created by repeated abkeb word. I would like to know, how efficiently analyze input word, to get the shortest period of characters creating input word. 回答1: O(n) solution. Assumes that the entire string must be covered. The key observation is that we generate the pattern and test it, but if we find something along the way

What is the meaning of “from distinct vertex chains” in this nearest neighbor algorithm?

女生的网名这么多〃 提交于 2019-11-30 06:14:06
The following pseudo-code is from the first chapter of an online preview version of The Algorithm Design Manual (page 7 from this PDF ). The example is of a flawed algorithm, but I still really want to understand it: [...] A different idea might be to repeatedly connect the closest pair of endpoints whose connection will not create a problem, such as premature termination of the cycle. Each vertex begins as its own single vertex chain. After merging everything together, we will end up with a single chain containing all the points in it. Connecting the final two endpoints gives us a cycle. At any

Kth largest element in a max-heap

元气小坏坏 提交于 2019-11-30 02:31:31
I'm trying to come up with something to solve the following: Given a max-heap represented as an array, return the kth largest element without modifying the heap. I was asked to do it in linear time, but was told it can be done in log time. I thought of a solution: Use a second max-heap and fill it with k or k+1 values into it (breadth first traversal into the original one) then pop k elements and get the desired one. I suppose this should be O(N+logN) = O(N) Is there a better solution, perhaps in O(logN) time? The max-heap can have many ways, a better case is a complete sorted array, and in

How to generate n different colors for any natural number n? [duplicate]

≡放荡痞女 提交于 2019-11-29 19:37:40
This question already has an answer here: How to automatically generate N “distinct” colors? 13 answers Say n = 100; How do I generate 100 visually distinct colors? Is this mathematically possible? 100 is a lot of colours, but you might be able to do it by distributing them as sparsely as possible in the HSB or HSL space; doing it in RGB is probably difficult. For example, you might decide to use 10 different hues, 4 different saturation levels, and 3 different brightness settings, that would give you up to 120 colours. You'll need to pick the saturation and brightness values carefully; human

Determine compass direction from one lat/lon to the other

佐手、 提交于 2019-11-29 18:06:08
Does anyone have an algorithm to determine the direction from one lat/lon to another (pseudo-code): CalculateHeading( lat1, lon1, lat2, long2 ) returns string heading Where heading is e.g. NW, SW, E, etc. Basically, I have two points on a map and I want to get a general idea of the direction taking into account that 50 miles East and one mile North is simply East and not Northeast. Dean Harding This site has the basic algorithm: // in javascript, not hard to translate... var y = Math.sin(dLon) * Math.cos(lat2); var x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon