algorithm

Minimum subarray difference in python

浪尽此生 提交于 2021-02-11 13:28:55
问题 Consider I have a non-empty array of integers: A0..An . And consider a parameter P where 0 < P <=n . I need to find a minimum absolute difference between left and right subarray splited by P. For example: A[0] = 3 A[1] = 1 A[2] = 2 A[3] = 4 A[4] = 3 P = 1, difference = |3 − 10| = 7 P = 2, difference = |4 − 9| = 5 P = 3, difference = |6 − 7| = 1 P = 4, difference = |10 − 3| = 7 The solution in this case is 1 I finished with the code below: def solution(A): lsum, rsum = A[0], sum(A[1:]) diff =

Sudoku in JS and HTML

被刻印的时光 ゝ 提交于 2021-02-11 12:54:18
问题 I am trying to randomly fill a 2 dimensional array in JS but i want to have the numbers generated in each row and column to be unique.Here is my progression.This can do it for 3x3 grid I mean randomly generated numbers but I want the size of 9x9 like a real sudoku.Thanx. //create table function UpdateTable() { var arr = []; while(arr.length < 10){ var randomnumber = Math.floor(Math.random()*10); if(arr.indexOf(randomnumber) > -1 ) continue; arr[arr.length] = randomnumber; } tmp1 = 'cell' + 1;

Time Complexity of finding all partitions of a set

99封情书 提交于 2021-02-11 12:46:41
问题 We know that this problem is np-complete, hence, no polynomial algorithm can be found for it. Also, we know that number of all partitions of a set is equal to the bell's number. I see there are few algorithms to generate all partitions of a set but couldn't find what is the time complexity to solve this problem. For example, this python code generates all partitions of a set recursively. What is the time complexity of this algorithm? Could this problem be solved with better time complexity?

Time Complexity of finding all partitions of a set

隐身守侯 提交于 2021-02-11 12:46:33
问题 We know that this problem is np-complete, hence, no polynomial algorithm can be found for it. Also, we know that number of all partitions of a set is equal to the bell's number. I see there are few algorithms to generate all partitions of a set but couldn't find what is the time complexity to solve this problem. For example, this python code generates all partitions of a set recursively. What is the time complexity of this algorithm? Could this problem be solved with better time complexity?

how to select some lines created by points based on their distances in python

纵饮孤独 提交于 2021-02-11 12:30:14
问题 I have some lines created by connecting points of a regular grid and want to pair the correct lines to create surfces. This is coordinates of my point array: coord=np.array([[0.,0.,2.], [0.,1.,3.], [0.,2.,2.], [1.,0.,1.], [1.,1.,3.],\ [1.,2.,1.], [2.,0.,1.], [2.,1.,1.], [3.,0.,1.], [4.,0.,1.]]) Then, I created lines by connecting points. My points are from a regular grid. So, I have two perpendicular sets of lines. I called them blue (vertical) and red (horizontal) lines. To do so: blue_line=

Calculating the similarity of 2 sets of convex polygons?

点点圈 提交于 2021-02-11 12:20:01
问题 I have generated 2 sets of convex polygons from with different algorithms. Every polygon in each set is described by an array of coordinates[n_points, xy_coords], so a square is described by an array [4,2] but a pentagon with rounded corners has [80,2], with the extra 75 points being used to describe the curvatures. My goal is to quantify how similar the two sets of geometries are. Can anyone recommend any methods of doing so? So far I've come across: Hamming Distance Hausdorff distance I

Calculating the similarity of 2 sets of convex polygons?

安稳与你 提交于 2021-02-11 12:17:48
问题 I have generated 2 sets of convex polygons from with different algorithms. Every polygon in each set is described by an array of coordinates[n_points, xy_coords], so a square is described by an array [4,2] but a pentagon with rounded corners has [80,2], with the extra 75 points being used to describe the curvatures. My goal is to quantify how similar the two sets of geometries are. Can anyone recommend any methods of doing so? So far I've come across: Hamming Distance Hausdorff distance I

Matrix grouping based on relation

元气小坏坏 提交于 2021-02-11 10:17:24
问题 I got into problem to solve matrix grouping problem based on its relation. Problem Consider a group of people giving books to each other. More formally, group is composed of all the people who know one another, whether directly to transitively. Example Consider a matrix of input M Input 1100 1110 0110 0001 Output: 2 There are n = 4 people numbered related[0] through related[3]. There are 2 pairs who directly know each other: (related[0], related[1]) and (related[1], related[2]). Because a

Matrix grouping based on relation

≡放荡痞女 提交于 2021-02-11 10:16:28
问题 I got into problem to solve matrix grouping problem based on its relation. Problem Consider a group of people giving books to each other. More formally, group is composed of all the people who know one another, whether directly to transitively. Example Consider a matrix of input M Input 1100 1110 0110 0001 Output: 2 There are n = 4 people numbered related[0] through related[3]. There are 2 pairs who directly know each other: (related[0], related[1]) and (related[1], related[2]). Because a

Matrix grouping based on relation

本秂侑毒 提交于 2021-02-11 10:16:23
问题 I got into problem to solve matrix grouping problem based on its relation. Problem Consider a group of people giving books to each other. More formally, group is composed of all the people who know one another, whether directly to transitively. Example Consider a matrix of input M Input 1100 1110 0110 0001 Output: 2 There are n = 4 people numbered related[0] through related[3]. There are 2 pairs who directly know each other: (related[0], related[1]) and (related[1], related[2]). Because a