JAVA-12097: renamed algorithms-module to algorithms-modules
This commit is contained in:
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.algorithms.balancedbrackets;
|
||||
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class BalancedBracketsUsingDeque {
|
||||
|
||||
public boolean isBalanced(String str) {
|
||||
if (null == str || ((str.length() % 2) != 0)) {
|
||||
return false;
|
||||
} else {
|
||||
char[] ch = str.toCharArray();
|
||||
for (char c : ch) {
|
||||
if (!(c == '{' || c == '[' || c == '(' || c == '}' || c == ']' || c == ')')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Deque<Character> deque = new LinkedList<>();
|
||||
for (char ch : str.toCharArray()) {
|
||||
if (ch == '{' || ch == '[' || ch == '(') {
|
||||
deque.addFirst(ch);
|
||||
} else {
|
||||
if (!deque.isEmpty() && ((deque.peekFirst() == '{' && ch == '}') || (deque.peekFirst() == '[' && ch == ']') || (deque.peekFirst() == '(' && ch == ')'))) {
|
||||
deque.removeFirst();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return deque.isEmpty();
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.algorithms.balancedbrackets;
|
||||
|
||||
public class BalancedBracketsUsingString {
|
||||
|
||||
public boolean isBalanced(String str) {
|
||||
if (null == str || ((str.length() % 2) != 0)) {
|
||||
return false;
|
||||
} else {
|
||||
char[] ch = str.toCharArray();
|
||||
for (char c : ch) {
|
||||
if (!(c == '{' || c == '[' || c == '(' || c == '}' || c == ']' || c == ')')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
while (str.contains("()") || str.contains("[]") || str.contains("{}")) {
|
||||
str = str.replaceAll("\\(\\)", "")
|
||||
.replaceAll("\\[\\]", "")
|
||||
.replaceAll("\\{\\}", "");
|
||||
}
|
||||
return (str.length() == 0);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
package com.baeldung.algorithms.boruvka;
|
||||
|
||||
import com.google.common.graph.EndpointPair;
|
||||
import com.google.common.graph.MutableValueGraph;
|
||||
import com.google.common.graph.ValueGraphBuilder;
|
||||
|
||||
public class BoruvkaMST {
|
||||
|
||||
private static MutableValueGraph<Integer, Integer> mst = ValueGraphBuilder.undirected()
|
||||
.build();
|
||||
private static int totalWeight;
|
||||
|
||||
public BoruvkaMST(MutableValueGraph<Integer, Integer> graph) {
|
||||
|
||||
int size = graph.nodes().size();
|
||||
|
||||
UnionFind uf = new UnionFind(size);
|
||||
|
||||
// repeat at most log N times or until we have N-1 edges
|
||||
for (int t = 1; t < size && mst.edges().size() < size - 1; t = t + t) {
|
||||
|
||||
EndpointPair<Integer>[] closestEdgeArray = new EndpointPair[size];
|
||||
|
||||
// foreach tree in graph, find closest edge
|
||||
for (EndpointPair<Integer> edge : graph.edges()) {
|
||||
int u = edge.nodeU();
|
||||
int v = edge.nodeV();
|
||||
int uParent = uf.find(u);
|
||||
int vParent = uf.find(v);
|
||||
if (uParent == vParent) {
|
||||
continue; // same tree
|
||||
}
|
||||
|
||||
int weight = graph.edgeValueOrDefault(u, v, 0);
|
||||
|
||||
if (closestEdgeArray[uParent] == null) {
|
||||
closestEdgeArray[uParent] = edge;
|
||||
}
|
||||
if (closestEdgeArray[vParent] == null) {
|
||||
closestEdgeArray[vParent] = edge;
|
||||
}
|
||||
|
||||
int uParentWeight = graph.edgeValueOrDefault(closestEdgeArray[uParent].nodeU(), closestEdgeArray[uParent].nodeV(), 0);
|
||||
int vParentWeight = graph.edgeValueOrDefault(closestEdgeArray[vParent].nodeU(), closestEdgeArray[vParent].nodeV(), 0);
|
||||
|
||||
if (weight < uParentWeight) {
|
||||
closestEdgeArray[uParent] = edge;
|
||||
}
|
||||
if (weight < vParentWeight) {
|
||||
closestEdgeArray[vParent] = edge;
|
||||
}
|
||||
}
|
||||
|
||||
// add newly discovered edges to MST
|
||||
for (int i = 0; i < size; i++) {
|
||||
EndpointPair<Integer> edge = closestEdgeArray[i];
|
||||
if (edge != null) {
|
||||
int u = edge.nodeU();
|
||||
int v = edge.nodeV();
|
||||
int weight = graph.edgeValueOrDefault(u, v, 0);
|
||||
// don't add the same edge twice
|
||||
if (uf.find(u) != uf.find(v)) {
|
||||
mst.putEdgeValue(u, v, weight);
|
||||
totalWeight += weight;
|
||||
uf.union(u, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public MutableValueGraph<Integer, Integer> getMST() {
|
||||
return mst;
|
||||
}
|
||||
|
||||
public int getTotalWeight() {
|
||||
return totalWeight;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "MST: " + mst.toString() + " | Total Weight: " + totalWeight;
|
||||
}
|
||||
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.algorithms.boruvka;
|
||||
|
||||
public class UnionFind {
|
||||
private int[] parents;
|
||||
private int[] ranks;
|
||||
|
||||
public UnionFind(int n) {
|
||||
parents = new int[n];
|
||||
ranks = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
parents[i] = i;
|
||||
ranks[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public int find(int u) {
|
||||
while (u != parents[u]) {
|
||||
u = parents[u];
|
||||
}
|
||||
return u;
|
||||
}
|
||||
|
||||
public void union(int u, int v) {
|
||||
int uParent = find(u);
|
||||
int vParent = find(v);
|
||||
if (uParent == vParent) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ranks[uParent] < ranks[vParent]) {
|
||||
parents[uParent] = vParent;
|
||||
} else if (ranks[uParent] > ranks[vParent]) {
|
||||
parents[vParent] = uParent;
|
||||
} else {
|
||||
parents[vParent] = uParent;
|
||||
ranks[uParent]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
package com.baeldung.algorithms.caesarcipher;
|
||||
|
||||
import org.apache.commons.math3.stat.inference.ChiSquareTest;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class CaesarCipher {
|
||||
|
||||
private final Logger log = LoggerFactory.getLogger(CaesarCipher.class);
|
||||
|
||||
private static final char LETTER_A = 'a';
|
||||
private static final char LETTER_Z = 'z';
|
||||
private static final int ALPHABET_SIZE = LETTER_Z - LETTER_A + 1;
|
||||
private static final double[] ENGLISH_LETTERS_PROBABILITIES = {0.073, 0.009, 0.030, 0.044, 0.130, 0.028, 0.016, 0.035, 0.074, 0.002, 0.003, 0.035, 0.025, 0.078, 0.074, 0.027, 0.003, 0.077, 0.063, 0.093, 0.027, 0.013, 0.016, 0.005, 0.019, 0.001};
|
||||
|
||||
public String cipher(String message, int offset) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
for (char character : message.toCharArray()) {
|
||||
if (character != ' ') {
|
||||
int originalAlphabetPosition = character - LETTER_A;
|
||||
int newAlphabetPosition = (originalAlphabetPosition + offset) % ALPHABET_SIZE;
|
||||
char newCharacter = (char) (LETTER_A + newAlphabetPosition);
|
||||
result.append(newCharacter);
|
||||
} else {
|
||||
result.append(character);
|
||||
}
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public String decipher(String message, int offset) {
|
||||
return cipher(message, ALPHABET_SIZE - (offset % ALPHABET_SIZE));
|
||||
}
|
||||
|
||||
public int breakCipher(String message) {
|
||||
return probableOffset(chiSquares(message));
|
||||
}
|
||||
|
||||
private double[] chiSquares(String message) {
|
||||
double[] expectedLettersFrequencies = expectedLettersFrequencies(message.length());
|
||||
|
||||
double[] chiSquares = new double[ALPHABET_SIZE];
|
||||
|
||||
for (int offset = 0; offset < chiSquares.length; offset++) {
|
||||
String decipheredMessage = decipher(message, offset);
|
||||
long[] lettersFrequencies = observedLettersFrequencies(decipheredMessage);
|
||||
double chiSquare = new ChiSquareTest().chiSquare(expectedLettersFrequencies, lettersFrequencies);
|
||||
chiSquares[offset] = chiSquare;
|
||||
}
|
||||
|
||||
return chiSquares;
|
||||
}
|
||||
|
||||
private long[] observedLettersFrequencies(String message) {
|
||||
return IntStream.rangeClosed(LETTER_A, LETTER_Z)
|
||||
.mapToLong(letter -> countLetter((char) letter, message))
|
||||
.toArray();
|
||||
}
|
||||
|
||||
private long countLetter(char letter, String message) {
|
||||
return message.chars()
|
||||
.filter(character -> character == letter)
|
||||
.count();
|
||||
}
|
||||
|
||||
private double[] expectedLettersFrequencies(int messageLength) {
|
||||
return Arrays.stream(ENGLISH_LETTERS_PROBABILITIES)
|
||||
.map(probability -> probability * messageLength)
|
||||
.toArray();
|
||||
}
|
||||
|
||||
private int probableOffset(double[] chiSquares) {
|
||||
int probableOffset = 0;
|
||||
for (int offset = 0; offset < chiSquares.length; offset++) {
|
||||
log.debug(String.format("Chi-Square for offset %d: %.2f", offset, chiSquares[offset]));
|
||||
if (chiSquares[offset] < chiSquares[probableOffset]) {
|
||||
probableOffset = offset;
|
||||
}
|
||||
}
|
||||
|
||||
return probableOffset;
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.algorithms.gradientdescent;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public class GradientDescent {
|
||||
|
||||
private final double precision = 0.000001;
|
||||
|
||||
public double findLocalMinimum(Function<Double, Double> f, double initialX) {
|
||||
double stepCoefficient = 0.1;
|
||||
double previousStep = 1.0;
|
||||
double currentX = initialX;
|
||||
double previousX = initialX;
|
||||
double previousY = f.apply(previousX);
|
||||
int iter = 100;
|
||||
|
||||
currentX += stepCoefficient * previousY;
|
||||
|
||||
while (previousStep > precision && iter > 0) {
|
||||
iter--;
|
||||
double currentY = f.apply(currentX);
|
||||
if (currentY > previousY) {
|
||||
stepCoefficient = -stepCoefficient / 2;
|
||||
}
|
||||
previousX = currentX;
|
||||
currentX += stepCoefficient * previousY;
|
||||
previousY = currentY;
|
||||
previousStep = StrictMath.abs(currentX - previousX);
|
||||
}
|
||||
return currentX;
|
||||
}
|
||||
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
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" ;
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
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" +
|
||||
" }";
|
||||
}
|
||||
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
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) {
|
||||
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());
|
||||
return max;
|
||||
} else {
|
||||
return max;
|
||||
}
|
||||
}
|
||||
|
||||
public FollowersPath getFollowers() {
|
||||
return fp;
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
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) {
|
||||
if (counter < 0)
|
||||
throw new IllegalStateException ("API limit reached");
|
||||
else {
|
||||
if(this.isCounterEnabled) counter--;
|
||||
for(SocialUser user : users) {
|
||||
if (user.getUsername().equals(account)) {
|
||||
return user.getFollowers();
|
||||
}
|
||||
}
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
package com.baeldung.algorithms.kruskal;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CycleDetector {
|
||||
|
||||
List<DisjointSetInfo> nodes;
|
||||
|
||||
public CycleDetector(int totalNodes) {
|
||||
initDisjointSets(totalNodes);
|
||||
}
|
||||
|
||||
public boolean detectCycle(Integer u, Integer v) {
|
||||
Integer rootU = pathCompressionFind(u);
|
||||
Integer rootV = pathCompressionFind(v);
|
||||
if (rootU.equals(rootV)) {
|
||||
return true;
|
||||
}
|
||||
unionByRank(rootU, rootV);
|
||||
return false;
|
||||
}
|
||||
|
||||
private void initDisjointSets(int totalNodes) {
|
||||
nodes = new ArrayList<>(totalNodes);
|
||||
for (int i = 0; i < totalNodes; i++) {
|
||||
nodes.add(new DisjointSetInfo(i));
|
||||
}
|
||||
}
|
||||
|
||||
private Integer find(Integer node) {
|
||||
Integer parent = nodes.get(node).getParentNode();
|
||||
if (parent.equals(node)) {
|
||||
return node;
|
||||
} else {
|
||||
return find(parent);
|
||||
}
|
||||
}
|
||||
|
||||
private Integer pathCompressionFind(Integer node) {
|
||||
DisjointSetInfo setInfo = nodes.get(node);
|
||||
Integer parent = setInfo.getParentNode();
|
||||
if (parent.equals(node)) {
|
||||
return node;
|
||||
} else {
|
||||
Integer parentNode = find(parent);
|
||||
setInfo.setParentNode(parentNode);
|
||||
return parentNode;
|
||||
}
|
||||
}
|
||||
|
||||
private void union(Integer rootU, Integer rootV) {
|
||||
DisjointSetInfo setInfoU = nodes.get(rootU);
|
||||
setInfoU.setParentNode(rootV);
|
||||
}
|
||||
|
||||
private void unionByRank(int rootU, int rootV) {
|
||||
DisjointSetInfo setInfoU = nodes.get(rootU);
|
||||
DisjointSetInfo setInfoV = nodes.get(rootV);
|
||||
int rankU = setInfoU.getRank();
|
||||
int rankV = setInfoV.getRank();
|
||||
if (rankU < rankV) {
|
||||
setInfoU.setParentNode(rootV);
|
||||
} else {
|
||||
setInfoV.setParentNode(rootU);
|
||||
if (rankU == rankV) {
|
||||
setInfoU.setRank(rankU + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.algorithms.kruskal;
|
||||
|
||||
public class DisjointSetInfo {
|
||||
|
||||
private Integer parentNode;
|
||||
private int rank;
|
||||
|
||||
DisjointSetInfo(Integer nodeNumber) {
|
||||
setParentNode(nodeNumber);
|
||||
setRank(1);
|
||||
}
|
||||
|
||||
public Integer getParentNode() {
|
||||
return parentNode;
|
||||
}
|
||||
|
||||
public void setParentNode(Integer parentNode) {
|
||||
this.parentNode = parentNode;
|
||||
}
|
||||
|
||||
public int getRank() {
|
||||
return rank;
|
||||
}
|
||||
|
||||
public void setRank(int rank) {
|
||||
this.rank = rank;
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.algorithms.kruskal;
|
||||
|
||||
import com.google.common.graph.EndpointPair;
|
||||
import com.google.common.graph.MutableValueGraph;
|
||||
import com.google.common.graph.ValueGraph;
|
||||
import com.google.common.graph.ValueGraphBuilder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class Kruskal {
|
||||
|
||||
public ValueGraph<Integer, Double> minSpanningTree(ValueGraph<Integer, Double> graph) {
|
||||
|
||||
return spanningTree(graph, true);
|
||||
}
|
||||
|
||||
public ValueGraph<Integer, Double> maxSpanningTree(ValueGraph<Integer, Double> graph) {
|
||||
return spanningTree(graph, false);
|
||||
}
|
||||
|
||||
private ValueGraph<Integer, Double> spanningTree(ValueGraph<Integer, Double> graph, boolean minSpanningTree) {
|
||||
Set<EndpointPair<Integer>> edges = graph.edges();
|
||||
List<EndpointPair<Integer>> edgeList = new ArrayList<>(edges);
|
||||
|
||||
if (minSpanningTree) {
|
||||
edgeList.sort(Comparator.comparing(e -> graph.edgeValue(e).get()));
|
||||
} else {
|
||||
edgeList.sort(Collections.reverseOrder(Comparator.comparing(e -> graph.edgeValue(e).get())));
|
||||
}
|
||||
|
||||
int totalNodes = graph.nodes().size();
|
||||
CycleDetector cycleDetector = new CycleDetector(totalNodes);
|
||||
int edgeCount = 0;
|
||||
|
||||
MutableValueGraph<Integer, Double> spanningTree = ValueGraphBuilder.undirected().build();
|
||||
for (EndpointPair<Integer> edge : edgeList) {
|
||||
if (cycleDetector.detectCycle(edge.nodeU(), edge.nodeV())) {
|
||||
continue;
|
||||
}
|
||||
spanningTree.putEdgeValue(edge.nodeU(), edge.nodeV(), graph.edgeValue(edge).get());
|
||||
edgeCount++;
|
||||
if (edgeCount == totalNodes - 1) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return spanningTree;
|
||||
}
|
||||
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
public class LinkedListReversal {
|
||||
|
||||
ListNode reverseList(ListNode head) {
|
||||
ListNode previous = null;
|
||||
ListNode current = head;
|
||||
while (current != null) {
|
||||
ListNode nextElement = current.getNext();
|
||||
current.setNext(previous);
|
||||
previous = current;
|
||||
current = nextElement;
|
||||
}
|
||||
return previous;
|
||||
}
|
||||
|
||||
ListNode reverseListRecursive(ListNode head) {
|
||||
if (head == null) {
|
||||
return null;
|
||||
}
|
||||
if (head.getNext() == null) {
|
||||
return head;
|
||||
}
|
||||
ListNode node = reverseListRecursive(head.getNext());
|
||||
head.getNext().setNext(head);
|
||||
head.setNext(null);
|
||||
return node;
|
||||
}
|
||||
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
public class ListNode {
|
||||
|
||||
private int data;
|
||||
private ListNode next;
|
||||
|
||||
ListNode(int data) {
|
||||
this.data = data;
|
||||
this.next = null;
|
||||
}
|
||||
|
||||
public int getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public ListNode getNext() {
|
||||
return next;
|
||||
}
|
||||
|
||||
public void setData(int data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public void setNext(ListNode next) {
|
||||
this.next = next;
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.algorithms.minheapmerge;
|
||||
|
||||
public class HeapNode {
|
||||
|
||||
int element;
|
||||
int arrayIndex;
|
||||
int nextElementIndex = 1;
|
||||
|
||||
public HeapNode(int element, int arrayIndex) {
|
||||
this.element = element;
|
||||
this.arrayIndex = arrayIndex;
|
||||
}
|
||||
}
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
package com.baeldung.algorithms.minheapmerge;
|
||||
|
||||
public class MinHeap {
|
||||
|
||||
HeapNode[] heapNodes;
|
||||
|
||||
public MinHeap(HeapNode heapNodes[]) {
|
||||
this.heapNodes = heapNodes;
|
||||
heapifyFromLastLeafsParent();
|
||||
}
|
||||
|
||||
void heapifyFromLastLeafsParent() {
|
||||
int lastLeafsParentIndex = getParentNodeIndex(heapNodes.length);
|
||||
while (lastLeafsParentIndex >= 0) {
|
||||
heapify(lastLeafsParentIndex);
|
||||
lastLeafsParentIndex--;
|
||||
}
|
||||
}
|
||||
|
||||
void heapify(int index) {
|
||||
int leftNodeIndex = getLeftNodeIndex(index);
|
||||
int rightNodeIndex = getRightNodeIndex(index);
|
||||
int smallestElementIndex = index;
|
||||
if (leftNodeIndex < heapNodes.length && heapNodes[leftNodeIndex].element < heapNodes[index].element) {
|
||||
smallestElementIndex = leftNodeIndex;
|
||||
}
|
||||
if (rightNodeIndex < heapNodes.length && heapNodes[rightNodeIndex].element < heapNodes[smallestElementIndex].element) {
|
||||
smallestElementIndex = rightNodeIndex;
|
||||
}
|
||||
if (smallestElementIndex != index) {
|
||||
swap(index, smallestElementIndex);
|
||||
heapify(smallestElementIndex);
|
||||
}
|
||||
}
|
||||
|
||||
int getParentNodeIndex(int index) {
|
||||
return (index - 1) / 2;
|
||||
}
|
||||
|
||||
int getLeftNodeIndex(int index) {
|
||||
return (2 * index + 1);
|
||||
}
|
||||
|
||||
int getRightNodeIndex(int index) {
|
||||
return (2 * index + 2);
|
||||
}
|
||||
|
||||
HeapNode getRootNode() {
|
||||
return heapNodes[0];
|
||||
}
|
||||
|
||||
void heapifyFromRoot() {
|
||||
heapify(0);
|
||||
}
|
||||
|
||||
void swap(int i, int j) {
|
||||
HeapNode temp = heapNodes[i];
|
||||
heapNodes[i] = heapNodes[j];
|
||||
heapNodes[j] = temp;
|
||||
}
|
||||
|
||||
static int[] merge(int[][] array) {
|
||||
HeapNode[] heapNodes = new HeapNode[array.length];
|
||||
int resultingArraySize = 0;
|
||||
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
HeapNode node = new HeapNode(array[i][0], i);
|
||||
heapNodes[i] = node;
|
||||
resultingArraySize += array[i].length;
|
||||
}
|
||||
|
||||
MinHeap minHeap = new MinHeap(heapNodes);
|
||||
int[] resultingArray = new int[resultingArraySize];
|
||||
|
||||
for (int i = 0; i < resultingArraySize; i++) {
|
||||
HeapNode root = minHeap.getRootNode();
|
||||
resultingArray[i] = root.element;
|
||||
|
||||
if (root.nextElementIndex < array[root.arrayIndex].length) {
|
||||
root.element = array[root.arrayIndex][root.nextElementIndex++];
|
||||
} else {
|
||||
root.element = Integer.MAX_VALUE;
|
||||
}
|
||||
minHeap.heapifyFromRoot();
|
||||
}
|
||||
return resultingArray;
|
||||
}
|
||||
}
|
||||
+208
@@ -0,0 +1,208 @@
|
||||
package com.baeldung.algorithms.play2048;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Board {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Board.class);
|
||||
|
||||
private final int[][] board;
|
||||
|
||||
private final int score;
|
||||
|
||||
public Board(int size) {
|
||||
assert(size > 0);
|
||||
|
||||
this.board = new int[size][];
|
||||
this.score = 0;
|
||||
|
||||
for (int x = 0; x < size; ++x) {
|
||||
this.board[x] = new int[size];
|
||||
for (int y = 0; y < size; ++y) {
|
||||
board[x][y] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Board(int[][] board, int score) {
|
||||
this.score = score;
|
||||
this.board = new int[board.length][];
|
||||
|
||||
for (int x = 0; x < board.length; ++x) {
|
||||
this.board[x] = Arrays.copyOf(board[x], board[x].length);
|
||||
}
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return board.length;
|
||||
}
|
||||
|
||||
public int getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
public int getCell(Cell cell) {
|
||||
int x = cell.getX();
|
||||
int y = cell.getY();
|
||||
assert(x >= 0 && x < board.length);
|
||||
assert(y >= 0 && y < board.length);
|
||||
|
||||
return board[x][y];
|
||||
}
|
||||
|
||||
public boolean isEmpty(Cell cell) {
|
||||
return getCell(cell) == 0;
|
||||
}
|
||||
|
||||
public List<Cell> emptyCells() {
|
||||
List<Cell> result = new ArrayList<>();
|
||||
for (int x = 0; x < board.length; ++x) {
|
||||
for (int y = 0; y < board[x].length; ++y) {
|
||||
Cell cell = new Cell(x, y);
|
||||
if (isEmpty(cell)) {
|
||||
result.add(cell);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Board placeTile(Cell cell, int number) {
|
||||
if (!isEmpty(cell)) {
|
||||
throw new IllegalArgumentException("That cell is not empty");
|
||||
}
|
||||
|
||||
Board result = new Board(this.board, this.score);
|
||||
result.board[cell.getX()][cell.getY()] = number;
|
||||
return result;
|
||||
}
|
||||
|
||||
public Board move(Move move) {
|
||||
// Clone the board
|
||||
int[][] tiles = new int[this.board.length][];
|
||||
for (int x = 0; x < this.board.length; ++x) {
|
||||
tiles[x] = Arrays.copyOf(this.board[x], this.board[x].length);
|
||||
}
|
||||
|
||||
LOG.debug("Before move: {}", Arrays.deepToString(tiles));
|
||||
// If we're doing an Left/Right move then transpose the board to make it a Up/Down move
|
||||
if (move == Move.LEFT || move == Move.RIGHT) {
|
||||
tiles = transpose(tiles);
|
||||
LOG.debug("After transpose: {}", Arrays.deepToString(tiles));
|
||||
}
|
||||
// If we're doing a Right/Down move then reverse the board.
|
||||
// With the above we're now always doing an Up move
|
||||
if (move == Move.DOWN || move == Move.RIGHT) {
|
||||
tiles = reverse(tiles);
|
||||
LOG.debug("After reverse: {}", Arrays.deepToString(tiles));
|
||||
}
|
||||
LOG.debug("Ready to move: {}", Arrays.deepToString(tiles));
|
||||
|
||||
// Shift everything up
|
||||
int[][] result = new int[tiles.length][];
|
||||
int newScore = 0;
|
||||
for (int x = 0; x < tiles.length; ++x) {
|
||||
LinkedList<Integer> thisRow = new LinkedList<>();
|
||||
for (int y = 0; y < tiles[0].length; ++y) {
|
||||
if (tiles[x][y] > 0) {
|
||||
thisRow.add(tiles[x][y]);
|
||||
}
|
||||
}
|
||||
|
||||
LOG.debug("Unmerged row: {}", thisRow);
|
||||
LinkedList<Integer> newRow = new LinkedList<>();
|
||||
while (thisRow.size() >= 2) {
|
||||
int first = thisRow.pop();
|
||||
int second = thisRow.peek();
|
||||
LOG.debug("Looking at numbers {} and {}", first, second);
|
||||
if (second == first) {
|
||||
LOG.debug("Numbers match, combining");
|
||||
int newNumber = first * 2;
|
||||
newRow.add(newNumber);
|
||||
newScore += newNumber;
|
||||
thisRow.pop();
|
||||
} else {
|
||||
LOG.debug("Numbers don't match");
|
||||
newRow.add(first);
|
||||
}
|
||||
}
|
||||
newRow.addAll(thisRow);
|
||||
LOG.debug("Merged row: {}", newRow);
|
||||
|
||||
result[x] = new int[tiles[0].length];
|
||||
for (int y = 0; y < tiles[0].length; ++y) {
|
||||
if (newRow.isEmpty()) {
|
||||
result[x][y] = 0;
|
||||
} else {
|
||||
result[x][y] = newRow.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
LOG.debug("After moves: {}", Arrays.deepToString(result));
|
||||
|
||||
// Un-reverse the board
|
||||
if (move == Move.DOWN || move == Move.RIGHT) {
|
||||
result = reverse(result);
|
||||
LOG.debug("After reverse: {}", Arrays.deepToString(result));
|
||||
}
|
||||
// Un-transpose the board
|
||||
if (move == Move.LEFT || move == Move.RIGHT) {
|
||||
result = transpose(result);
|
||||
LOG.debug("After transpose: {}", Arrays.deepToString(result));
|
||||
}
|
||||
return new Board(result, this.score + newScore);
|
||||
}
|
||||
|
||||
private static int[][] transpose(int[][] input) {
|
||||
int[][] result = new int[input.length][];
|
||||
|
||||
for (int x = 0; x < input.length; ++x) {
|
||||
result[x] = new int[input[0].length];
|
||||
for (int y = 0; y < input[0].length; ++y) {
|
||||
result[x][y] = input[y][x];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int[][] reverse(int[][] input) {
|
||||
int[][] result = new int[input.length][];
|
||||
|
||||
for (int x = 0; x < input.length; ++x) {
|
||||
result[x] = new int[input[0].length];
|
||||
for (int y = 0; y < input[0].length; ++y) {
|
||||
result[x][y] = input[x][input.length - y - 1];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return Arrays.deepToString(board);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Board board1 = (Board) o;
|
||||
return Arrays.deepEquals(board, board1.board);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Arrays.deepHashCode(board);
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.algorithms.play2048;
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
public class Cell {
|
||||
private int x;
|
||||
private int y;
|
||||
|
||||
public Cell(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringJoiner(", ", Cell.class.getSimpleName() + "[", "]").add("x=" + x).add("y=" + y).toString();
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.algorithms.play2048;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Computer {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Computer.class);
|
||||
|
||||
private final SecureRandom rng = new SecureRandom();
|
||||
|
||||
public Board makeMove(Board input) {
|
||||
List<Cell> emptyCells = input.emptyCells();
|
||||
LOG.info("Number of empty cells: {}", emptyCells.size());
|
||||
|
||||
double numberToPlace = rng.nextDouble();
|
||||
LOG.info("New number probability: {}", numberToPlace);
|
||||
|
||||
int indexToPlace = rng.nextInt(emptyCells.size());
|
||||
Cell cellToPlace = emptyCells.get(indexToPlace);
|
||||
LOG.info("Placing number into empty cell: {}", cellToPlace);
|
||||
|
||||
return input.placeTile(cellToPlace, numberToPlace >= 0.9 ? 4 : 2);
|
||||
}
|
||||
}
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
package com.baeldung.algorithms.play2048;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.commons.math3.util.Pair;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Human {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Human.class);
|
||||
|
||||
public Board makeMove(Board input) {
|
||||
// For each move in MOVE
|
||||
// Generate board from move
|
||||
// Generate Score for Board
|
||||
// Return board with the best score
|
||||
//
|
||||
// Generate Score
|
||||
// If Depth Limit
|
||||
// Return Final Score
|
||||
// Total Score = 0
|
||||
// For every empty square in new board
|
||||
// Generate board with "2" in square
|
||||
// Calculate Score
|
||||
// Total Score += (Score * 0.9)
|
||||
// Generate board with "4" in square
|
||||
// Calculate Score
|
||||
// Total Score += (Score * 0.1)
|
||||
//
|
||||
// Calculate Score
|
||||
// For each move in MOVE
|
||||
// Generate board from move
|
||||
// Generate score for board
|
||||
// Return the best generated score
|
||||
|
||||
return Arrays.stream(Move.values())
|
||||
.parallel()
|
||||
.map(input::move)
|
||||
.filter(board -> !board.equals(input))
|
||||
.max(Comparator.comparingInt(board -> generateScore(board, 0)))
|
||||
.orElse(input);
|
||||
}
|
||||
|
||||
private int generateScore(Board board, int depth) {
|
||||
if (depth >= 3) {
|
||||
int finalScore = calculateFinalScore(board);
|
||||
LOG.debug("Final score for board {}: {}", board,finalScore);
|
||||
return finalScore;
|
||||
}
|
||||
|
||||
return board.emptyCells().stream()
|
||||
.parallel()
|
||||
.flatMap(cell -> Stream.of(new Pair<>(cell, 2), new Pair<>(cell, 4)))
|
||||
.mapToInt(move -> {
|
||||
LOG.debug("Simulating move {} at depth {}", move, depth);
|
||||
Board newBoard = board.placeTile(move.getFirst(), move.getSecond());
|
||||
int boardScore = calculateScore(newBoard, depth + 1);
|
||||
int calculatedScore = (int) (boardScore * (move.getSecond() == 2 ? 0.9 : 0.1));
|
||||
LOG.debug("Calculated score for board {} and move {} at depth {}: {}", newBoard, move, depth, calculatedScore);
|
||||
return calculatedScore;
|
||||
})
|
||||
.sum();
|
||||
}
|
||||
|
||||
private int calculateScore(Board board, int depth) {
|
||||
return Arrays.stream(Move.values())
|
||||
.parallel()
|
||||
.map(board::move)
|
||||
.filter(moved -> !moved.equals(board))
|
||||
.mapToInt(newBoard -> generateScore(newBoard, depth))
|
||||
.max()
|
||||
.orElse(0);
|
||||
}
|
||||
|
||||
private int calculateFinalScore(Board board) {
|
||||
List<List<Integer>> rowsToScore = new ArrayList<>();
|
||||
for (int i = 0; i < board.getSize(); ++i) {
|
||||
List<Integer> row = new ArrayList<>();
|
||||
List<Integer> col = new ArrayList<>();
|
||||
|
||||
for (int j = 0; j < board.getSize(); ++j) {
|
||||
row.add(board.getCell(new Cell(i, j)));
|
||||
col.add(board.getCell(new Cell(j, i)));
|
||||
}
|
||||
|
||||
rowsToScore.add(row);
|
||||
rowsToScore.add(col);
|
||||
}
|
||||
|
||||
return rowsToScore.stream()
|
||||
.parallel()
|
||||
.mapToInt(row -> {
|
||||
List<Integer> preMerged = row.stream()
|
||||
.filter(value -> value != 0)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
int numMerges = 0;
|
||||
int monotonicityLeft = 0;
|
||||
int monotonicityRight = 0;
|
||||
for (int i = 0; i < preMerged.size() - 1; ++i) {
|
||||
Integer first = preMerged.get(i);
|
||||
Integer second = preMerged.get(i + 1);
|
||||
if (first.equals(second)) {
|
||||
++numMerges;
|
||||
} else if (first > second) {
|
||||
monotonicityLeft += first - second;
|
||||
} else {
|
||||
monotonicityRight += second - first;
|
||||
}
|
||||
}
|
||||
|
||||
int score = 1000;
|
||||
score += 250 * row.stream().filter(value -> value == 0).count();
|
||||
score += 750 * numMerges;
|
||||
score -= 10 * row.stream().mapToInt(value -> value).sum();
|
||||
score -= 50 * Math.min(monotonicityLeft, monotonicityRight);
|
||||
return score;
|
||||
})
|
||||
.sum();
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.algorithms.play2048;
|
||||
|
||||
public enum Move {
|
||||
UP,
|
||||
DOWN,
|
||||
LEFT,
|
||||
RIGHT
|
||||
}
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
package com.baeldung.algorithms.play2048;
|
||||
|
||||
public class Play2048 {
|
||||
private static final int SIZE = 4;
|
||||
private static final int INITIAL_NUMBERS = 2;
|
||||
|
||||
public static void main(String[] args) {
|
||||
// The board and players
|
||||
Board board = new Board(SIZE);
|
||||
Computer computer = new Computer();
|
||||
Human human = new Human();
|
||||
|
||||
// The computer has two moves first
|
||||
System.out.println("Setup");
|
||||
System.out.println("=====");
|
||||
for (int i = 0; i < INITIAL_NUMBERS; ++i) {
|
||||
board = computer.makeMove(board);
|
||||
}
|
||||
|
||||
printBoard(board);
|
||||
do {
|
||||
board = human.makeMove(board);
|
||||
System.out.println("Human move");
|
||||
System.out.println("==========");
|
||||
printBoard(board);
|
||||
|
||||
board = computer.makeMove(board);
|
||||
System.out.println("Computer move");
|
||||
System.out.println("=============");
|
||||
printBoard(board);
|
||||
} while (!board.emptyCells().isEmpty());
|
||||
|
||||
System.out.println("Final Score: " + board.getScore());
|
||||
|
||||
}
|
||||
|
||||
private static void printBoard(Board board) {
|
||||
StringBuilder topLines = new StringBuilder();
|
||||
StringBuilder midLines = new StringBuilder();
|
||||
for (int x = 0; x < board.getSize(); ++x) {
|
||||
topLines.append("+--------");
|
||||
midLines.append("| ");
|
||||
}
|
||||
topLines.append("+");
|
||||
midLines.append("|");
|
||||
|
||||
|
||||
for (int y = 0; y < board.getSize(); ++y) {
|
||||
System.out.println(topLines);
|
||||
System.out.println(midLines);
|
||||
for (int x = 0; x < board.getSize(); ++x) {
|
||||
Cell cell = new Cell(x, y);
|
||||
System.out.print("|");
|
||||
if (board.isEmpty(cell)) {
|
||||
System.out.print(" ");
|
||||
} else {
|
||||
StringBuilder output = new StringBuilder(Integer.toString(board.getCell(cell)));
|
||||
while (output.length() < 8) {
|
||||
output.append(" ");
|
||||
if (output.length() < 8) {
|
||||
output.insert(0, " ");
|
||||
}
|
||||
}
|
||||
System.out.print(output);
|
||||
}
|
||||
}
|
||||
System.out.println("|");
|
||||
System.out.println(midLines);
|
||||
}
|
||||
System.out.println(topLines);
|
||||
System.out.println("Score: " + board.getScore());
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.algorithms.topkelements;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BruteForceTopKElementsFinder implements TopKElementsFinder<Integer> {
|
||||
|
||||
public List<Integer> findTopK(List<Integer> input, int k) {
|
||||
List<Integer> array = new ArrayList<>(input);
|
||||
List<Integer> topKList = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < k; i++) {
|
||||
int maxIndex = 0;
|
||||
|
||||
for (int j = 1; j < array.size(); j++) {
|
||||
if (array.get(j) > array.get(maxIndex)) {
|
||||
maxIndex = j;
|
||||
}
|
||||
}
|
||||
|
||||
topKList.add(array.remove(maxIndex));
|
||||
}
|
||||
|
||||
return topKList;
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.algorithms.topkelements;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.PriorityQueue;
|
||||
|
||||
public class MaxHeapTopKElementsFinder implements TopKElementsFinder<Integer> {
|
||||
|
||||
public List<Integer> findTopK(List<Integer> input, int k) {
|
||||
PriorityQueue<Integer> maxHeap = new PriorityQueue<>();
|
||||
|
||||
input.forEach(number -> {
|
||||
maxHeap.add(number);
|
||||
|
||||
if (maxHeap.size() > k) {
|
||||
maxHeap.poll();
|
||||
}
|
||||
});
|
||||
|
||||
List<Integer> topKList = new ArrayList<>(maxHeap);
|
||||
Collections.reverse(topKList);
|
||||
|
||||
return topKList;
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.algorithms.topkelements;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface TopKElementsFinder<T extends Comparable<T>> {
|
||||
List<T> findTopK(List<T> input, int k);
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.algorithms.topkelements;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class TreeSetTopKElementsFinder implements TopKElementsFinder<Integer> {
|
||||
|
||||
public List<Integer> findTopK(List<Integer> input, int k) {
|
||||
Set<Integer> sortedSet = new TreeSet<>(Comparator.reverseOrder());
|
||||
sortedSet.addAll(input);
|
||||
|
||||
return sortedSet.stream().limit(k).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user