lydiahappycat
BAN USERIn my view, the first thing is to define what makes a friend have a high priority. We use G(v,e,w) as the social graph, G.adj(me) as the sub-graph of my friends network, and W(v1,v2) as the familiar weight of v1 and v2. Here are my rules to choose friends.
1. Friends in the party are my closer ones.
2. Friends are familiar with each other.
So my goal is to max the total familiarity of top n friends and me.
Greedy Algorithm:
1. Fset is empty, put me in the Fset.
2. while(|Fset|<=n+1)
{
choose v in G.adj(me) that max(Familiar(v+Fset));
Fset.insert(v);
G.adj(me) = G.adj(me)-'v';
}
Familiar(Set) = the sum familiar weight of each pair(v1,v2) which v1 and v2 belongs to Set;
This greedy algorithm can't find the global optimal solution. Wish better ideas!
I'm not sure I have understood this question. From my view, there is only one way to get to (m,n). If m>n, robot must come from (m-n,n). Otherwise comes from (m, n-m). So Here is my code:
int minSteps(int m, int n)
{
if(m<1 || n <1) return -1;
int count = 0;
while(abs(m-n) && m>=1 && n>=1)
{
if(m>n)
m=m-n;
else n = n-m;
count++;
}
if(m==1 && n==1)
return count;
else return -1;
}
I will level reverse the tree and create a new tree which has the same structure as the original tree but the node's value is the length of consecutive number sequence calculated from its root. The initial value of new tree's node is 1. If the child is continues to its root then child's node value ++. In your case, newTree(2)=1, newTree(3)=2, newTree(4)=3, newTree(6)=1.
- lydiahappycat March 10, 2015I'm not sure whether have to consider case like (3,2,1). If that is the case, we can design the new tree node that has two values, upLen and downLen.