Skip to content

Commit c828b10

Browse files
committed
leetcode
1 parent 1e48136 commit c828b10

File tree

4 files changed

+328
-0
lines changed

4 files changed

+328
-0
lines changed

Diff for: DesignBrowserHistory/design_browser_history.dart

+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*
2+
3+
4+
--* 1472. Design Browser History *--
5+
6+
7+
8+
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps.
9+
10+
Implement the BrowserHistory class:
11+
12+
BrowserHistory(string homepage) Initializes the object with the homepage of the browser.
13+
void visit(string url) Visits url from the current page. It clears up all the forward history.
14+
string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps.
15+
string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.
16+
17+
18+
Example:
19+
20+
Input:
21+
["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"]
22+
[["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]]
23+
Output:
24+
[null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"]
25+
26+
Explanation:
27+
BrowserHistory browserHistory = new BrowserHistory("leetcode.com");
28+
browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com"
29+
browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com"
30+
browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com"
31+
browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com"
32+
browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com"
33+
browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com"
34+
browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com"
35+
browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps.
36+
browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com"
37+
browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"
38+
39+
40+
Constraints:
41+
42+
1 <= homepage.length <= 20
43+
1 <= url.length <= 20
44+
1 <= steps <= 100
45+
homepage and url consist of '.' or lower case English letters.
46+
At most 5000 calls will be made to visit, back, and forward.
47+
48+
*/
49+
50+
/*
51+
52+
53+
class BrowserHistory {
54+
55+
BrowserHistory(String homepage) {
56+
57+
}
58+
59+
void visit(String url) {
60+
61+
}
62+
63+
String back(int steps) {
64+
65+
}
66+
67+
String forward(int steps) {
68+
69+
}
70+
}
71+
72+
/**
73+
* Your BrowserHistory object will be instantiated and called as such:
74+
* BrowserHistory obj = BrowserHistory(homepage);
75+
* obj.visit(url);
76+
* String param2 = obj.back(steps);
77+
* String param3 = obj.forward(steps);
78+
*/
79+
80+
*/
81+
82+
// import 'dart:math';
83+
84+
// class BrowserHistory {
85+
// late List<String> list;
86+
// int total = 0;
87+
// int current = 0;
88+
// BrowserHistory(String homepage) {
89+
// list = <String>[];
90+
// list.add(homepage);
91+
// total++;
92+
// current++;
93+
// }
94+
95+
// void visit(String url) {
96+
// if (list.length > current) {
97+
// list[current] = url;
98+
// } else {
99+
// list.add(url);
100+
// }
101+
// current++;
102+
// total = current;
103+
// }
104+
105+
// String back(int steps) {
106+
// current = max(1, current - steps);
107+
// return list.elementAt(current - 1);
108+
// }
109+
110+
// String forward(int steps) {
111+
// current = min(total, current + steps);
112+
// return list.elementAt(current - 1);
113+
// }
114+
// }
115+
116+
// /**
117+
// * Your BrowserHistory object will be instantiated and called as such:
118+
// * BrowserHistory obj = BrowserHistory(homepage);
119+
// * obj.visit(url);
120+
// * String param2 = obj.back(steps);
121+
// * String param3 = obj.forward(steps);
122+
// */
123+
124+
// Doubly Linked List
125+
126+
class Node {
127+
String? value;
128+
Node? next;
129+
Node? previous;
130+
Node(String source) {
131+
value = source;
132+
previous = next = null;
133+
}
134+
}
135+
136+
class BrowserHistory {
137+
late Node root;
138+
BrowserHistory(String homepage) {
139+
root = Node(homepage);
140+
}
141+
142+
void visit(String url) {
143+
Node node = Node(url);
144+
root.next = node;
145+
node.previous = root;
146+
root = node;
147+
}
148+
149+
String back(int steps) {
150+
Node current = root;
151+
while (steps > 0 && current.previous != null) {
152+
current = current.previous!;
153+
steps--;
154+
}
155+
root = current;
156+
return current.value!;
157+
}
158+
159+
String forward(int steps) {
160+
Node cur = root;
161+
while (steps > 0 && cur.next != null) {
162+
cur = cur.next!;
163+
steps--;
164+
}
165+
root = cur;
166+
return cur.value!;
167+
}
168+
}

Diff for: DesignBrowserHistory/design_browser_history.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
type BrowserHistory struct {
4+
root *Node
5+
}
6+
7+
type Node struct {
8+
value string
9+
next *Node
10+
previous *Node
11+
}
12+
13+
func Constructor(homepage string) BrowserHistory {
14+
return BrowserHistory{
15+
root: &Node{
16+
value: homepage,
17+
},
18+
}
19+
}
20+
21+
func (this *BrowserHistory) Visit(url string) {
22+
node := Node{
23+
value: url,
24+
}
25+
node.previous = this.root
26+
this.root.next = &node
27+
this.root = this.root.next
28+
}
29+
30+
func (this *BrowserHistory) Back(steps int) string {
31+
32+
for steps > 0 && this.root.previous != nil {
33+
this.root = this.root.previous
34+
steps -= 1
35+
}
36+
return this.root.value
37+
}
38+
39+
func (this *BrowserHistory) Forward(steps int) string {
40+
for steps > 0 && this.root.next != nil {
41+
this.root = this.root.next
42+
steps -= 1
43+
}
44+
return this.root.value
45+
}
46+
47+
/**
48+
* Your BrowserHistory object will be instantiated and called as such:
49+
* obj := Constructor(homepage);
50+
* obj.Visit(url);
51+
* param_2 := obj.Back(steps);
52+
* param_3 := obj.Forward(steps);
53+
54+
*/

Diff for: DesignBrowserHistory/design_browser_history.md

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# 🔥 Design Browser History 🔥 || Simple Fast and Easy || with Explanation
2+
3+
## Using List
4+
5+
The current URL is represented by the index "current", which starts at 1 (since the first visited URL is at index 0). The total number of visited URLs is stored in the "total" variable.
6+
7+
The "visit" method adds a new URL to the history by either setting the element at index "current" to the new URL (if there are already visited URLs after the current position), or by adding the new URL to the end of the list. The "current" and "total" variables are updated accordingly.
8+
9+
The "back" method navigates the history backward by decrementing "current" by the specified number of steps, but ensuring that it never goes below 1 (the beginning of the history). The URL at index "current-1" is then returned.
10+
11+
The "forward" method navigates the history forward by incrementing "current" by the specified number of steps, but ensuring that it never goes above the "total" number of visited URLs. The URL at index "current-1" is then returned.
12+
13+
```dart
14+
import 'dart:math';
15+
16+
class BrowserHistory {
17+
late List<String> list;
18+
int total = 0;
19+
int current = 0;
20+
BrowserHistory(String homepage) {
21+
list = <String>[];
22+
list.add(homepage);
23+
total++;
24+
current++;
25+
}
26+
27+
void visit(String url) {
28+
if (list.length > current) {
29+
list[current] = url;
30+
} else {
31+
list.add(url);
32+
}
33+
current++;
34+
total = current;
35+
}
36+
37+
String back(int steps) {
38+
current = max(1, current - steps);
39+
return list.elementAt(current - 1);
40+
}
41+
42+
String forward(int steps) {
43+
current = min(total, current + steps);
44+
return list.elementAt(current - 1);
45+
}
46+
}
47+
```
48+
49+
## Using Node - Doubly Linked List
50+
51+
We will create a Node class. Each node in the linked list represents a visited URL, and has a "val" (URL string), "prev" (previous node) and "next" (next node) field.
52+
53+
The "visit" method adds a new URL to the history by creating a new node and setting its "prev" field to the current "root" node, and the "next" field of the current "root" node to the new node. The "root" variable is then updated to point to the new node.
54+
55+
The "back" method navigates the history backward by following the "prev" pointers of nodes until the desired number of steps is reached, or until the beginning of the history is reached. The "root" variable is then updated to point to the current node, and its "val" field is returned.
56+
57+
The "forward" method navigates the history forward by following the "next" pointers of nodes until the desired number of steps is reached, or until the end of the history is reached. The "root" variable is then updated to point to the current node, and its "val" field is returned.
58+
59+
```dart
60+
class Node {
61+
String? val;
62+
Node? next;
63+
Node? prev;
64+
Node(String s) {
65+
val = s;
66+
prev = next = null;
67+
}
68+
}
69+
70+
class BrowserHistory {
71+
late Node root;
72+
BrowserHistory(String homepage) {
73+
root = Node(homepage);
74+
}
75+
76+
void visit(String url) {
77+
Node node = Node(url);
78+
root.next = node;
79+
node.prev = root;
80+
root = node;
81+
}
82+
83+
String back(int steps) {
84+
Node cur = root;
85+
while (steps > 0 && cur.prev != null) {
86+
cur = cur.prev!;
87+
steps--;
88+
}
89+
root = cur;
90+
return cur.val!;
91+
}
92+
93+
String forward(int steps) {
94+
Node cur = root;
95+
while (steps > 0 && cur.next != null) {
96+
cur = cur.next!;
97+
steps--;
98+
}
99+
root = cur;
100+
return cur.val!;
101+
}
102+
}
103+
```
104+
105+
### [GitHub Link](https://github.com/ayoubzulfiqar/leetcode)

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
212212
- [**2477.** Minimum Fuel Cost to Report to the Capital](MinimumFuelCostToReportToTheCapital/minimum_fuel_cost_to_report_to_the_capital.dart)
213213
- [**1523.** Count Odd Numbers in an Interval Range](CountOddNumbersInAnIntervalRange/count_odd_numbers_in_an_interval_range.dart)
214214
- [**989.** Add to Array-Form of Integer](AddToArrayFormOfInteger/add_to_array_form_of_integer.dart)
215+
- [**1472.** Design Browser History](DesignBrowserHistory/design_browser_history.dart)
215216

216217
## Reach me via
217218

0 commit comments

Comments
 (0)