BAEL-3855

This commit is contained in:
Unknown 2020-02-19 23:26:24 +01:00
parent 34d04156fc
commit 72ea7a14e6
6 changed files with 0 additions and 229 deletions

View File

@ -1,20 +0,0 @@
package com.baeldung.algorithms.greedy;
import lombok.Getter;
public class Follower {
@Getter String username;
@Getter long count;
public Follower(String username, long count) {
super();
this.username = username;
this.count = count;
}
@Override
public String toString() {
return "User: " + username + ", Followers: " + count + "\n\r" ;
}
}

View File

@ -1,44 +0,0 @@
package com.baeldung.algorithms.greedy;
import java.util.ArrayList;
import java.util.List;
public class FollowersPath {
private List<Follower> accounts;
private long count;
public FollowersPath() {
super();
this.accounts = new ArrayList<>();
}
public List<Follower> getAccounts() {
return accounts;
}
public long getCount() {
return count;
}
public void addFollower(String username, long count) {
accounts.add(new Follower(username, count));
}
public void addCount(long count) {
this.count += count;
}
@Override
public String toString() {
String details = "";
for(Follower a : accounts) {
details+=a.toString() + ", ";
}
return "Total: " + count + ", \n\r" +
" Details: { " + "\n\r" +
details + "\n\r" +
" }";
}
}

View File

@ -1,47 +0,0 @@
package com.baeldung.algorithms.greedy;
import java.util.List;
public class GreedyAlgorithm {
int currentLevel = 0;
final int maxLevel = 3;
SocialConnector sc;
FollowersPath fp;
public GreedyAlgorithm(SocialConnector sc) {
super();
this.sc = sc;
this.fp = new FollowersPath();
}
public long findMostFollowersPath(String account) throws Exception {
long max = 0;
SocialUser toFollow = null;
List<SocialUser> followers = sc.getFollowers(account);
for (SocialUser el : followers) {
long followersCount = el.getFollowersCount();
if (followersCount > max) {
toFollow = el;
max = followersCount;
}
}
if (currentLevel < maxLevel - 1) {
currentLevel++;
max += findMostFollowersPath(toFollow.getUsername());
//fp.addFollower(toFollow.getUsername(), max);
//fp.addCount(max);
return max;
} else {
//fp.addFollower(toFollow.getUsername(), max);
//fp.addCount(max);
return max;
}
}
public FollowersPath getFollowers() {
return fp;
}
}

View File

@ -1,44 +0,0 @@
package com.baeldung.algorithms.greedy;
import java.util.List;
public class NonGreedyAlgorithm {
int currentLevel = 0;
final int maxLevel = 3;
SocialConnector tc;
public NonGreedyAlgorithm(SocialConnector tc, int level) {
super();
this.tc = tc;
this.currentLevel = level;
}
public long findMostFollowersPath(String account) throws Exception {
List<SocialUser> followers = tc.getFollowers(account);
long total = currentLevel > 0 ? followers.size() : 0;
if (currentLevel < maxLevel ) {
currentLevel++;
long[] count = new long[followers.size()];
int i = 0;
for (SocialUser el : followers) {
NonGreedyAlgorithm sub = new NonGreedyAlgorithm(tc, currentLevel);
count[i] = sub.findMostFollowersPath(el.getUsername());
i++;
}
long max = 0;
for (; i > 0; i--) {
if (count[i-1] > max )
max = count[i-1];
}
return total + max;
}
return total;
}
}

View File

@ -1,36 +0,0 @@
package com.baeldung.algorithms.greedy;
import java.util.ArrayList;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
public class SocialConnector {
private boolean isCounterEnabled = true;
private int counter = 4;
@Getter @Setter List<SocialUser> users;
public SocialConnector() {
users = new ArrayList<>();
}
public boolean switchCounter() {
this.isCounterEnabled = !this.isCounterEnabled;
return this.isCounterEnabled;
}
public List<SocialUser> getFollowers(String account) throws Exception {
if (counter < 0)
throw new Exception ("API limit reached");
else {
if(this.isCounterEnabled) counter--;
for(SocialUser user : users) {
if (user.getUsername().equals(account)) {
return user.getFollowers();
}
}
}
return new ArrayList<>();
}
}

View File

@ -1,38 +0,0 @@
package com.baeldung.algorithms.greedy;
import java.util.ArrayList;
import java.util.List;
import lombok.Getter;
public class SocialUser {
@Getter private String username;
@Getter private List<SocialUser> followers;
public SocialUser(String username) {
super();
this.username = username;
this.followers = new ArrayList<>();
}
public SocialUser(String username, List<SocialUser> followers) {
super();
this.username = username;
this.followers = followers;
}
public long getFollowersCount() {
return followers.size();
}
public void addFollowers(List<SocialUser> followers) {
this.followers.addAll(followers);
}
@Override
public boolean equals(Object obj) {
return ((SocialUser) obj).getUsername().equals(username);
}
}