algorithm

largest complete subtree in a binary tree

和自甴很熟 提交于 2021-02-07 14:47:00
问题 I am defining a complete subtree as a tree with all levels full and the last level left justified i.e. all nodes are as far left as possible, and I want to find the largest subtree in a tree that is complete. One method is to do the method outlined here for every node as root, which would take O(n^2) time. Is there a better approach? 回答1: Since there isn't a C++ solution above, I have added my solution. Let me know if you feel there is anything incorrect or any improvements that can be made.

Fastest algorithm for Kth smallest Element (or median) finding on 2 Dimensional Array?

筅森魡賤 提交于 2021-02-07 14:38:54
问题 I see a lot of SO topics on related topics but none of them provides the efficient way. I want to find the k-th smallest element (or median) on 2D array [1..M][1..N] where each row is sorted in ascending order and all elements are distinct. I think there is O(M log MN) solution, but I have no idea about implementation. (Median of Medians or Using Partition with Linear Complexity is some method but no idea any more...). This is an old Google interview question and can be searched on Here. But

Fastest algorithm for Kth smallest Element (or median) finding on 2 Dimensional Array?

こ雲淡風輕ζ 提交于 2021-02-07 14:38:36
问题 I see a lot of SO topics on related topics but none of them provides the efficient way. I want to find the k-th smallest element (or median) on 2D array [1..M][1..N] where each row is sorted in ascending order and all elements are distinct. I think there is O(M log MN) solution, but I have no idea about implementation. (Median of Medians or Using Partition with Linear Complexity is some method but no idea any more...). This is an old Google interview question and can be searched on Here. But

Robot moving in a grid

南楼画角 提交于 2021-02-07 14:37:42
问题 A robot is located at the top-left corner of a 4x4 grid. The robot can move either up, down, left, or right, but can not visit the same spot twice. The robot is trying to reach the bottom-right corner of the grid.The number of ways it can reach the bottom-right corner of the grid is? Now i know that if the robot can only move down or right ,then the answer would be 8C4 because it has to go 4 squares to the right and 4 squares down, in any order. But i am having difficulty in solving the

Robot moving in a grid

自古美人都是妖i 提交于 2021-02-07 14:32:19
问题 A robot is located at the top-left corner of a 4x4 grid. The robot can move either up, down, left, or right, but can not visit the same spot twice. The robot is trying to reach the bottom-right corner of the grid.The number of ways it can reach the bottom-right corner of the grid is? Now i know that if the robot can only move down or right ,then the answer would be 8C4 because it has to go 4 squares to the right and 4 squares down, in any order. But i am having difficulty in solving the

Dynamic nested loops level

﹥>﹥吖頭↗ 提交于 2021-02-07 13:35:40
问题 I'm trying to figure out a simple way to handle dynamic nested loops level. Consider the following function that takes in 2 parameters: #num of loops, and max value. void PrintLoop(int maxloop, int maxvalue) PrintLoop(1,2); // output 0 1 PrintLoop(2,2); // output 0, 0 0, 1 1, 0 1, 1 PrintLoop(3,2); // output 0, 0, 0 0, 0, 1 0, 1, 0 0, 1, 1 1, 0, 0 1, 0, 1 1, 1, 0 1, 1, 1 Etc... Is there a way to write a function that can generate this "dynamic nested loops" behavior? Thanks for any help 回答1:

Efficient algorithm to group points in clusters by distance between every two points

这一生的挚爱 提交于 2021-02-07 13:30:56
问题 I am looking for an efficient algorithm for the following problem: Given a set of points in 2D space, where each point is defined by its X and Y coordinates. Required to split this set of points into a set of clusters so that if distance between two arbitrary points is less then some threshold, these points must belong to the same cluster: In other words, such cluster is a set of points which are 'close enough' to each other. The naive algorithm may look like this: Let R be a resulting list

Fastest gap sequence for shell sort?

。_饼干妹妹 提交于 2021-02-07 11:17:23
问题 According to Marcin Ciura's Optimal (best known) sequence of increments for shell sort algorithm, the best sequence for shellsort is 1, 4, 10, 23, 57, 132, 301, 701..., but how can I generate such a sequence? In Marcin Ciura's paper, he said: Both Knuth’s and Hibbard’s sequences are relatively bad, because they are defined by simple linear recurrences. but most algorithm books I found tend to use Knuth’s sequence: k = 3k + 1, because it's easy to generate. What's your way of generating a

Fastest gap sequence for shell sort?

杀马特。学长 韩版系。学妹 提交于 2021-02-07 11:15:22
问题 According to Marcin Ciura's Optimal (best known) sequence of increments for shell sort algorithm, the best sequence for shellsort is 1, 4, 10, 23, 57, 132, 301, 701..., but how can I generate such a sequence? In Marcin Ciura's paper, he said: Both Knuth’s and Hibbard’s sequences are relatively bad, because they are defined by simple linear recurrences. but most algorithm books I found tend to use Knuth’s sequence: k = 3k + 1, because it's easy to generate. What's your way of generating a

Algorithm to determine which numbers in a list are the sum of a given number

五迷三道 提交于 2021-02-07 11:12:16
问题 I have a coding question that I am try to solve. I haven't been able to come up with a good algorithm yet. Given a number X (e.g. 200), determine which numbers from a list (e.g.: 4,5,10, 10, 23,67,889, 150, 50) will when summed up will be equal to X. In this case the answer is (50, 150). So far I thought about first sorting the the list (lowest to highest) then loop through adding the numbers until I get to a value greater than X. Discard all the remaining numbers in the list since they are