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 diff --git a/articles/find-common-characters.md b/articles/find-common-characters.md index 5de052d16..bd6e2fd68 100644 --- a/articles/find-common-characters.md +++ b/articles/find-common-characters.md @@ -132,7 +132,7 @@ class Solution { ```csharp public class Solution { - public IList CommonChars(string[] words) { + public List CommonChars(string[] words) { int[] cnt = new int[26]; Array.Fill(cnt, int.MaxValue); @@ -246,7 +246,6 @@ class Solution { } ``` - ```rust impl Solution { pub fn common_chars(words: Vec) -> Vec { @@ -274,6 +273,34 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {string[]} words + * @return {string[]} + */ + commonChars(words: string[]): string[] { + const cnt: number[] = new Array(26).fill(Infinity); + for (const word of words) { + const curCnt: number[] = new Array(26).fill(0); + for (const c of word) { + curCnt[c.charCodeAt(0) - 97]++; + } + for (let i = 0; i < 26; i++) { + cnt[i] = Math.min(cnt[i], curCnt[i]); + } + } + const res: string[] = []; + for (let i = 0; i < 26; i++) { + for (let j = 0; j < cnt[i]; j++) { + res.push(String.fromCharCode(i + 97)); + } + } + return res; + } +} +``` + ::tabs-end ### Time & Space Complexity 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 diff --git a/articles/time-needed-to-buy-tickets.md b/articles/time-needed-to-buy-tickets.md index 9e7378204..ef61e8458 100644 --- a/articles/time-needed-to-buy-tickets.md +++ b/articles/time-needed-to-buy-tickets.md @@ -140,6 +140,32 @@ class Solution { } ``` +```csharp +public class Solution { + public int TimeRequiredToBuy(int[] tickets, int k) { + int n = tickets.Length; + Queue queue = new Queue(); + for (int i = 0; i < n; i++) { + queue.Enqueue(i); + } + int time = 0; + while (queue.Count > 0) { + time++; + int cur = queue.Dequeue(); + tickets[cur]--; + if (tickets[cur] == 0) { + if (cur == k) { + return time; + } + } else { + queue.Enqueue(cur); + } + } + return time; + } +} +``` + ```go func timeRequiredToBuy(tickets []int, k int) int { n := len(tickets) @@ -248,6 +274,37 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {number[]} tickets + * @param {number} k + * @return {number} + */ + timeRequiredToBuy(tickets: number[], k: number): number { + const n = tickets.length; + const queue = new Queue(); + for (let i = 0; i < n; i++) { + queue.push(i); + } + let time = 0; + while (queue.size() > 0) { + time++; + const cur = queue.pop()!; + tickets[cur]--; + if (tickets[cur] === 0) { + if (cur === k) { + return time; + } + } else { + queue.push(cur); + } + } + return time; + } +} +``` + ::tabs-end ### Time & Space Complexity @@ -379,6 +436,29 @@ class Solution { } ``` +```csharp +public class Solution { + public int TimeRequiredToBuy(int[] tickets, int k) { + int n = tickets.Length; + int idx = 0; + int time = 0; + while (true) { + time++; + tickets[idx]--; + if (tickets[idx] == 0) { + if (idx == k) { + return time; + } + } + idx = (idx + 1) % n; + while (tickets[idx] == 0) { + idx = (idx + 1) % n; + } + } + } +} +``` + ```go func timeRequiredToBuy(tickets []int, k int) int { n := len(tickets) @@ -473,6 +553,34 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {number[]} tickets + * @param {number} k + * @return {number} + */ + timeRequiredToBuy(tickets: number[], k: number): number { + const n = tickets.length; + let idx = 0; + let time = 0; + while (true) { + time++; + tickets[idx]--; + if (tickets[idx] === 0) { + if (idx === k) { + return time; + } + } + idx = (idx + 1) % n; + while (tickets[idx] === 0) { + idx = (idx + 1) % n; + } + } + } +} +``` + ::tabs-end ### Time & Space Complexity @@ -576,6 +684,22 @@ class Solution { } ``` +```csharp +public class Solution { + public int TimeRequiredToBuy(int[] tickets, int k) { + int res = 0; + for (int i = 0; i < tickets.Length; i++) { + if (i <= k) { + res += Math.Min(tickets[i], tickets[k]); + } else { + res += Math.Min(tickets[i], tickets[k] - 1); + } + } + return res; + } +} +``` + ```go func timeRequiredToBuy(tickets []int, k int) int { res := 0 @@ -652,6 +776,27 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {number[]} tickets + * @param {number} k + * @return {number} + */ + timeRequiredToBuy(tickets: number[], k: number): number { + let res = 0; + for (let i = 0; i < tickets.length; i++) { + if (i <= k) { + res += Math.min(tickets[i], tickets[k]); + } else { + res += Math.min(tickets[i], tickets[k] - 1); + } + } + return res; + } +} +``` + ::tabs-end ### Time & Space Complexity