Git Product home page Git Product logo

algs4's Introduction

algs4's People

Contributors

leon-good-life avatar phareskrad avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

algs4's Issues

the comparison needs to be modified

if (!b.equals(node.board)) {

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.

BalancedSearchTrees.java Question2

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");
}

}
`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.