package leetcode;

/**
 * https://leetcode.com/problems/design-browser-history/
 */
public class Problem1472 {
    static class BrowserHistory {
        public BrowserHistory(String homepage) {
        }

        public void visit(String url) {
            // TODO
        }

        public String back(int steps) {
            // TODO
            return null;
        }

        public String forward(int steps) {
            // TODO
            return null;
        }
    }

    public static void main(String[] args) {
        BrowserHistory browserHistory = new BrowserHistory("leetcode.com");
        browserHistory.visit("google.com");            // You are in "leetcode.com". Visit "google.com"
        browserHistory.visit("facebook.com");          // You are in "google.com". Visit "facebook.com"
        browserHistory.visit("youtube.com");           // You are in "facebook.com". Visit "youtube.com"
        System.out.println(browserHistory.back(1));    // You are in "youtube.com", move back to "facebook.com" return "facebook.com"
        System.out.println(browserHistory.back(1));    // You are in "facebook.com", move back to "google.com" return "google.com"
        System.out.println(browserHistory.forward(1)); // You are in "google.com", move forward to "facebook.com" return "facebook.com"
        browserHistory.visit("linkedin.com");          // You are in "facebook.com". Visit "linkedin.com"
        System.out.println(browserHistory.forward(2)); // You are in "linkedin.com", you cannot move forward any steps.
        System.out.println(browserHistory.back(2));    // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com"
        System.out.println(browserHistory.back(7));    // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"
    }
}

@@ -12,16 +12,68 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 /**%0A * h
@@ -190,16 +190,102 @@
 story %7B%0A
+        private List%3CString%3E history = new ArrayList%3C%3E();%0A        private int index;%0A%0A
         
@@ -321,24 +321,59 @@
 homepage) %7B%0A
+            history.add(homepage);%0A
         %7D%0A%0A 
@@ -427,165 +427,111 @@
     
-// TODO%0A        %7D%0A%0A        public String back(int steps) %7B%0A            // TODO%0A            return null;%0A        %7D%0A%0A        public String forward(int steps) %7B
+index++;%0A            if (index %3C history.size()) %7B%0A                history = history.subList(0, index);
 %0A   
@@ -543,15 +543,9 @@
     
-// TODO
+%7D
 %0A   
@@ -557,19 +557,24 @@
     
-return null
+history.add(url)
 ;%0A  
@@ -581,23 +581,21 @@
       %7D%0A
+%0A
     
-%7D%0A%0A
     publ
@@ -601,1378 +601,401 @@
 lic 
-static void main(String%5B%5D args) %7B%0A        BrowserHistory browserHistory = new BrowserHistory(%22leetcode.com%22);%0A        browserHistory.visit(%22google.com%22);            // You are in %22leetcode.com%22. Visit %22google.com%22%0A        browserHistory.visit(%22facebook.com%22);          // You are in %22google.com%22. Visit %22facebook.com%22%0A        browserHistory.visit(%22youtube.com%22);
+String back(int steps) %7B%0A            index -= steps;%0A            if (index %3C 0) %7B%0A                index = 0;%0A        
     
+%7D%0A
        
-// You are in %22facebook.com%22. Visit %22youtube.com%22%0A        System.out.println(browserHistory.back(1)
+     return history.get(index
 );
+%0A
     
-// You are in %22youtube.com%22, move back to %22facebook.com%22 return %22facebook.com%22%0A        System.out.println(browserHistory.back(1));    // You are in %22facebook.com%22, move back to %22google.com%22 return %22google.com%22%0A        System.out.println(browserHistory.forward(1)); // You are in %22google.com%22, move forward to %22facebook.com%22 return %22facebook.com%22%0A        browserHistory.visit(%22linkedin.com%22);          // You are in %22facebook.com%22. Visit %22linkedin.com%22%0A        System.out.println(browserHistory.forward(2)); // You are in %22linkedin.com%22, you cannot move forward any steps.%0A        System.out.println(browserHistory.back(2));    // You are in %22linkedin.com%22, move back two steps to %22facebook.com%22 then to %22google.com%22. return %22google.com%22%0A        System.out.println(browserHistory.back(7));    // You are in %22google.com%22, you can move back only one step to %22leetcode.com%22. return %22leetcode.com%22
+    %7D%0A%0A        public String forward(int steps) %7B%0A            index += steps;%0A            if (index %3E= history.size()) %7B%0A                index = history.size() - 1;%0A            %7D%0A            return history.get(index);%0A        %7D
 %0A   

Solve problem 1472