algorithm

Linear Conflict violating admissibility and driving me insane

我们两清 提交于 2021-02-07 20:30:29
问题 When Two tiles tj and tk are in a linear conflict if tj and tk are in the same line, the goal positions of tj and tk are both in that line, tj is to the right of tk and goal position of tj is to the left of the goal position of tk. The linear conflict adds at least two moves to the Manhattan Distance of the two conflicting tiles, by forcing them to surround one another. Therefore the heuristic function will add a cost of 2 moves for each pair of conflicting tiles. The Linar Conflict Heuristic

Numbers of common distinct difference

拟墨画扇 提交于 2021-02-07 20:22:13
问题 Given two array A and B. Task to find the number of common distinct (difference of elements in two arrays). Example : A=[3,6,8] B=[1,6,10] so we get differenceSet for A differenceSetA=[abs(3-6),abs(6-8),abs(8-3)]=[3,5,2] similiarly differenceSetB=[abs(1-6),abs(1-10),abs(6-10)]=[5,9,4] Number of common elements=Intersection :{differenceSetA,differenceSetB}={5} Answer= 1 My approach O(N^2) int commonDifference(vector<int> A,vector<int> B){ int n=A.size(); int m=B.size(); unordered_set<int>

Minimum removed nodes required to cut path from A to B algorithm in Python

北城以北 提交于 2021-02-07 19:54:16
问题 I am trying to solve a problem related to graph theory but can't seem to remember/find/understand the proper/best approach so I figured I'd ask the experts... I have a list of paths from two nodes (1 and 10 in example code). I'm trying to find the minimum number of nodes to remove to cut all paths. I'm also only able to remove certain nodes. I currently have it implemented (below) as a brute force search. This works fine on my test set but is going to be an issue when scaling up to a graphs

Minimum removed nodes required to cut path from A to B algorithm in Python

余生长醉 提交于 2021-02-07 19:54:08
问题 I am trying to solve a problem related to graph theory but can't seem to remember/find/understand the proper/best approach so I figured I'd ask the experts... I have a list of paths from two nodes (1 and 10 in example code). I'm trying to find the minimum number of nodes to remove to cut all paths. I'm also only able to remove certain nodes. I currently have it implemented (below) as a brute force search. This works fine on my test set but is going to be an issue when scaling up to a graphs

Binary tree complexities

谁说我不能喝 提交于 2021-02-07 19:27:39
问题 I would like to know some complexities of binary search tree. I can't find complete information. I want to know complexities for the following operations on a binary search tree to add/insert an element to remove an element to find an element (as I know this one is O(log(n)) ) 回答1: Insertion, deletion and searching in a binary search tree are: O(N) in the worst case; O(log(N)) in the average case. 回答2: If you have balanced binary tree, all three complexities will be of O(log(N)) . If you are

sorting strings in Java based on substrings

只谈情不闲聊 提交于 2021-02-07 18:43:44
问题 I have a list of strings def123, abc999, zzz000, abc123, zzz111 . I want the list sorted such that first three characters are sorted in ascendng order and next three in descending. So the output should be abc999, abc123, def123, zzz111,zzz000 Is this possible? 回答1: Other answers have suggested you implement Comparator . That's no longer necessary with recent utility methods added to the interface in Java 8: list.sort(Comparator .comparing(s -> s.substring(0, 3)) .thenComparing(s -> s.subtring

What is the best algorithm for closest word

守給你的承諾、 提交于 2021-02-07 14:59:31
问题 What is the best algorithm for closest word. Possible word dictionary is given and first characters in the input word can be wrong. 回答1: One option is BK-trees - see my blog post about them here. Another, faster but more complex option is Levenshtein Automata, which I've also written about, here. 回答2: There are tools such as HunSpell (open-source spell-checker widely including OpenOffice) which have approached the problem from multiple perspectives. One widely used criterion for deciding how

What is the best algorithm for closest word

落花浮王杯 提交于 2021-02-07 14:59:14
问题 What is the best algorithm for closest word. Possible word dictionary is given and first characters in the input word can be wrong. 回答1: One option is BK-trees - see my blog post about them here. Another, faster but more complex option is Levenshtein Automata, which I've also written about, here. 回答2: There are tools such as HunSpell (open-source spell-checker widely including OpenOffice) which have approached the problem from multiple perspectives. One widely used criterion for deciding how

largest complete subtree in a binary tree

允我心安 提交于 2021-02-07 14:49:06
问题 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.

largest complete subtree in a binary tree

折月煮酒 提交于 2021-02-07 14:48:54
问题 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.