From 9a8f3de25dcc57376ed8cc59010863019f188c99 Mon Sep 17 00:00:00 2001 From: Srihari Date: Sat, 16 May 2026 19:55:25 +0530 Subject: [PATCH 1/2] update articles --- articles/design-browser-history.md | 201 +++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) diff --git a/articles/design-browser-history.md b/articles/design-browser-history.md index d8ef61a7e..3d6362d83 100644 --- a/articles/design-browser-history.md +++ b/articles/design-browser-history.md @@ -336,6 +336,53 @@ impl BrowserHistory { } ``` +```typescript +class BrowserHistory { + private backHistory: string[]; + private frontHistory: string[]; + + /** + * @constructor + * @param {string} homepage + */ + constructor(homepage: string) { + this.backHistory = [homepage]; + this.frontHistory = []; + } + + /** + * @param {string} url + * @return {void} + */ + visit(url: string): void { + this.backHistory.push(url); + this.frontHistory = []; + } + + /** + * @param {number} steps + * @return {string} + */ + back(steps: number): string { + while (steps-- > 0 && this.backHistory.length > 1) { + this.frontHistory.push(this.backHistory.pop()!); + } + return this.backHistory[this.backHistory.length - 1]; + } + + /** + * @param {number} steps + * @return {string} + */ + forward(steps: number): string { + while (steps-- > 0 && this.frontHistory.length > 0) { + this.backHistory.push(this.frontHistory.pop()!); + } + return this.backHistory[this.backHistory.length - 1]; + } +} +``` + ::tabs-end ### Time & Space Complexity @@ -640,6 +687,50 @@ impl BrowserHistory { } ``` +```typescript +class BrowserHistory { + private history: string[]; + private cur: number; + + /** + * @constructor + * @param {string} homepage + */ + constructor(homepage: string) { + this.history = [homepage]; + this.cur = 0; + } + + /** + * @param {string} url + * @return {void} + */ + visit(url: string): void { + this.cur++; + this.history = this.history.slice(0, this.cur); + this.history.push(url); + } + + /** + * @param {number} steps + * @return {string} + */ + back(steps: number): string { + this.cur = Math.max(0, this.cur - steps); + return this.history[this.cur]; + } + + /** + * @param {number} steps + * @return {string} + */ + forward(steps: number): string { + this.cur = Math.min(this.history.length - 1, this.cur + steps); + return this.history[this.cur]; + } +} +``` + ::tabs-end ### Time & Space Complexity @@ -1002,6 +1093,57 @@ impl BrowserHistory { } ``` +```typescript +class BrowserHistory { + private history: string[]; + private cur: number; + private n: number; + + /** + * @constructor + * @param {string} homepage + */ + constructor(homepage: string) { + this.history = [homepage]; + this.cur = 0; + this.n = 1; + } + + /** + * @param {string} url + * @return {void} + */ + visit(url: string): void { + this.cur++; + if (this.cur === this.history.length) { + this.history.push(url); + this.n++; + } else { + this.history[this.cur] = url; + this.n = this.cur + 1; + } + } + + /** + * @param {number} steps + * @return {string} + */ + back(steps: number): string { + this.cur = Math.max(0, this.cur - steps); + return this.history[this.cur]; + } + + /** + * @param {number} steps + * @return {string} + */ + forward(steps: number): string { + this.cur = Math.min(this.n - 1, this.cur + steps); + return this.history[this.cur]; + } +} +``` + ::tabs-end ### Time & Space Complexity @@ -1408,6 +1550,65 @@ impl BrowserHistory { } ``` +```typescript +class ListNode { + val: string; + prev: ListNode | null; + next: ListNode | null; + + constructor(val: string, prev: ListNode | null = null, next: ListNode | null = null) { + this.val = val; + this.prev = prev; + this.next = next; + } +} + +class BrowserHistory { + private cur: ListNode; + + /** + * @constructor + * @param {string} homepage + */ + constructor(homepage: string) { + this.cur = new ListNode(homepage); + } + + /** + * @param {string} url + * @return {void} + */ + visit(url: string): void { + this.cur.next = new ListNode(url, this.cur, null); + this.cur = this.cur.next; + } + + /** + * @param {number} steps + * @return {string} + */ + back(steps: number): string { + while (this.cur.prev !== null && steps > 0) { + this.cur = this.cur.prev; + steps--; + } + return this.cur.val; + } + + /** + * @param {number} steps + * @return {string} + */ + forward(steps: number): string { + while (this.cur.next !== null && steps > 0) { + this.cur = this.cur.next; + steps--; + } + return this.cur.val; + } +} +``` + ::tabs-end ### Time & Space Complexity From 25a8e98e9d274d7968c009303b3a5e506b7ad077 Mon Sep 17 00:00:00 2001 From: Srihari Date: Sun, 17 May 2026 10:20:34 +0530 Subject: [PATCH 2/2] update articles --- .../remove-duplicates-from-sorted-array-ii.md | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/articles/remove-duplicates-from-sorted-array-ii.md b/articles/remove-duplicates-from-sorted-array-ii.md index db6bad9ce..ae882722a 100644 --- a/articles/remove-duplicates-from-sorted-array-ii.md +++ b/articles/remove-duplicates-from-sorted-array-ii.md @@ -292,6 +292,39 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {number[]} nums + * @return {number} + */ + removeDuplicates(nums: number[]): number { + let n = nums.length; + if (n <= 2) return n; + let i = 0; + while (i < n - 1) { + if (nums[i] === nums[i + 1]) { + let j = i + 2, + cnt = 0; + while (j < n && nums[i] === nums[j]) { + j++; + cnt++; + } + for (let k = i + 2; k < n; k++) { + if (j >= n) break; + nums[k] = nums[j++]; + } + n -= cnt; + i += 2; + } else { + i++; + } + } + return n; + } +} +``` + ::tabs-end ### Time & Space Complexity @@ -550,6 +583,31 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {number[]} nums + * @return {number} + */ + removeDuplicates(nums: number[]): number { + const count = new Map(); + for (const num of nums) { + count.set(num, (count.get(num) || 0) + 1); + } + let i = 0; + for (const [num, cnt] of count) { + nums[i++] = num; + count.set(num, cnt - 1); + if (count.get(num)! >= 1) { + nums[i++] = num; + count.set(num, cnt - 1); + } + } + return i; + } +} +``` + ::tabs-end ### Time & Space Complexity @@ -799,6 +857,32 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {number[]} nums + * @return {number} + */ + removeDuplicates(nums: number[]): number { + let l = 0, + r = 0; + while (r < nums.length) { + let count = 1; + while (r + 1 < nums.length && nums[r] === nums[r + 1]) { + r++; + count++; + } + for (let i = 0; i < Math.min(2, count); i++) { + nums[l] = nums[r]; + l++; + } + r++; + } + return l; + } +} +``` + ::tabs-end ### Time & Space Complexity @@ -957,6 +1041,25 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {number[]} nums + * @return {number} + */ + removeDuplicates(nums: number[]): number { + let l = 0; + for (const num of nums) { + if (l < 2 || num !== nums[l - 2]) { + nums[l] = num; + l++; + } + } + return l; + } +} +``` + ::tabs-end ### Time & Space Complexity