phareskrad / algs4 Goto Github PK
View Code? Open in Web Editor NEWProgramming Assignments & Job Interview Questions
Programming Assignments & Job Interview Questions
The answer to this question is wrong. Pay attention to line 64 queues
and query
, we should loop query
Reference to https://algs4.cs.princeton.edu/13stacks/MultiwordSearch.java.html for correct answer
`
public class MultiwordSearch {
public static void main(String[] args) {
String[] words = StdIn.readAllStrings();
// construct queues[j] = sequence of positions of jth query word
Queue<Integer>[] queues = (Queue<Integer>[]) new Queue[args.length];
for (int j = 0; j < args.length; j++) {
queues[j] = new Queue<Integer>();
}
for (int i = 0; i < words.length; i++) {
for (int j = 0; j < args.length; j++) {
if (words[i].equals(args[j])) queues[j].enqueue(i);
}
}
// repeatedly find smallest interval starting at position of queues[0]
boolean done = false;
int bestlo = -1, besthi = words.length;
while (!queues[0].isEmpty()) {
int lo = queues[0].dequeue();
int hi = lo;
for (int j = 1; j < args.length; j++) {
while (!queues[j].isEmpty() && queues[j].peek() <= hi) {
queues[j].dequeue();
}
if (queues[j].isEmpty()) {
done = true;
break;
}
else hi = queues[j].peek();
}
if (!done && hi - lo < besthi - bestlo) {
besthi = hi;
bestlo = lo;
}
}
if (bestlo >= 0) {
for (int i = bestlo; i <= besthi; i++)
StdOut.print(words[i] + " ");
StdOut.println();
}
else
StdOut.println("NOT FOUND");
}
}
`
algs4/assignments/8puzzle/Solver.java
Line 90 in 18abfcf
In order to implement critical optimization, the condition should be:
if (!b.equals(node.previous.board) && node.previous == null)
First condition is to compare the neibhoring board and the previous board. Otherwise the algorithm will be stuck in loop and the number of calls to the methods in MinPQ will exceed the upper limit.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.