data-structures

Binary tree number of nodes with a given level

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-11 12:42:57
问题 I need to write a program that counts the number of nodes from a certain level given in binary tree. I mean < numberofnodes(int level){} > I tried writing it without any success because I don't how to get to a certain level then go to count number of nodes. 回答1: Do it with a recursive function which only descends to a certain level. 回答2: Well, there are many ways you can do this. Best is to have a single dimensional array that keep track of the number of nodes that you add/remove at each

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

Finding Intersection point of two linked list?

a 夏天 提交于 2021-02-11 09:59:46
问题 Most of sites present this solution(see Method 3 at http://www.geeksforgeeks.org/write-a-function-to-get-the-intersection-point-of-two-linked-lists/ ) 1) Get count of the nodes in first list, let count be c1. 2) Get count of the nodes in second list, let count be c2. 3) Get the difference of counts d = abs(c1 – c2) 4) Now traverse the bigger list from the first node till d nodes so that from here onwards both the lists have equal no of nodes. 5) Then we can traverse both the lists in parallel

Finding Intersection point of two linked list?

拈花ヽ惹草 提交于 2021-02-11 09:56:47
问题 Most of sites present this solution(see Method 3 at http://www.geeksforgeeks.org/write-a-function-to-get-the-intersection-point-of-two-linked-lists/ ) 1) Get count of the nodes in first list, let count be c1. 2) Get count of the nodes in second list, let count be c2. 3) Get the difference of counts d = abs(c1 – c2) 4) Now traverse the bigger list from the first node till d nodes so that from here onwards both the lists have equal no of nodes. 5) Then we can traverse both the lists in parallel

Finding Intersection point of two linked list?

浪子不回头ぞ 提交于 2021-02-11 09:56:23
问题 Most of sites present this solution(see Method 3 at http://www.geeksforgeeks.org/write-a-function-to-get-the-intersection-point-of-two-linked-lists/ ) 1) Get count of the nodes in first list, let count be c1. 2) Get count of the nodes in second list, let count be c2. 3) Get the difference of counts d = abs(c1 – c2) 4) Now traverse the bigger list from the first node till d nodes so that from here onwards both the lists have equal no of nodes. 5) Then we can traverse both the lists in parallel

Filtering a list of dictionaries based on multiple values

℡╲_俬逩灬. 提交于 2021-02-10 12:13:11
问题 I have a list of dictionaries that I would like to filter based on multiple criteria. A shortened version of the list looks like so: orders = [{"name": "v", "price": 123, "location": "Mars"}, {"name": "x", "price": 223, "location": "Mars"}, {"name": "x", "price": 124, "location": "Mars"}, {"name": "y", "price": 456, "location": "Mars"}, {"name": "z", "price": 123, "location": "Mars"}, {"name": "z", "price": 5623, "location": "Mars"}] I am looking to end up with a list that contains the

Filtering a list of dictionaries based on multiple values

。_饼干妹妹 提交于 2021-02-10 12:12:38
问题 I have a list of dictionaries that I would like to filter based on multiple criteria. A shortened version of the list looks like so: orders = [{"name": "v", "price": 123, "location": "Mars"}, {"name": "x", "price": 223, "location": "Mars"}, {"name": "x", "price": 124, "location": "Mars"}, {"name": "y", "price": 456, "location": "Mars"}, {"name": "z", "price": 123, "location": "Mars"}, {"name": "z", "price": 5623, "location": "Mars"}] I am looking to end up with a list that contains the

Heap's Algorithm implementation in list of lists

僤鯓⒐⒋嵵緔 提交于 2021-02-10 06:14:54
问题 I'm using Heap's algorithm to create a list-of-lists containing each permutation of said list. Each permutation will be its own list. It works properly when I print it within the algorithm, but it doesn't work properly when I try to add it to my list-of-lists and they are all the same array (4, 1, 2, 3). I commented out the print that I tested to make sure it was working. My current code: public static ArrayList<int[]> lists = new ArrayList<>(); public static void main(String[] args) { int[]