diff --git a/articles/average-waiting-time.md b/articles/average-waiting-time.md index 5cb9877e8..ae73aaef9 100644 --- a/articles/average-waiting-time.md +++ b/articles/average-waiting-time.md @@ -219,6 +219,29 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {number[][]} customers + * @return {number} + */ + averageWaitingTime(customers: number[][]): number { + let t = 0, + total = 0; + for (const [arrival, order] of customers) { + if (t > arrival) { + total += t - arrival; + } else { + t = arrival; + } + total += order; + t += order; + } + return total / customers.length; + } +} +``` + ::tabs-end ### Time & Space Complexity @@ -394,6 +417,24 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {number[][]} customers + * @return {number} + */ + averageWaitingTime(customers: number[][]): number { + let t = 0, + total = 0; + for (const [arrival, order] of customers) { + t = Math.max(t, arrival) + order; + total += t - arrival; + } + return total / customers.length; + } +} +``` + ::tabs-end ### Time & Space Complexity diff --git a/articles/count-vowel-strings-in-ranges.md b/articles/count-vowel-strings-in-ranges.md index df423b861..40125bb77 100644 --- a/articles/count-vowel-strings-in-ranges.md +++ b/articles/count-vowel-strings-in-ranges.md @@ -240,6 +240,31 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {string[]} words + * @param {number[][]} queries + * @return {number[]} + */ + vowelStrings(words: string[], queries: number[][]): number[] { + const vowels = new Set(['a', 'e', 'i', 'o', 'u']); + const res: number[] = []; + for (const [start, end] of queries) { + let count = 0; + for (let i = start; i <= end; i++) { + const word = words[i]; + if (vowels.has(word[0]) && vowels.has(word[word.length - 1])) { + count++; + } + } + res.push(count); + } + return res; + } +} +``` + ::tabs-end ### Time & Space Complexity @@ -503,6 +528,34 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {string[]} words + * @param {number[][]} queries + * @return {number[]} + */ + vowelStrings(words: string[], queries: number[][]): number[] { + const vowels = new Set(['a', 'e', 'i', 'o', 'u']); + const n = words.length; + const prefixCnt: number[] = new Array(n + 1).fill(0); + for (let i = 0; i < n; i++) { + prefixCnt[i + 1] = prefixCnt[i]; + const w = words[i]; + if (vowels.has(w[0]) && vowels.has(w[w.length - 1])) { + prefixCnt[i + 1]++; + } + } + const res: number[] = new Array(queries.length); + for (let i = 0; i < queries.length; i++) { + const [l, r] = queries[i]; + res[i] = prefixCnt[r + 1] - prefixCnt[l]; + } + return res; + } +} +``` + ::tabs-end ### Time & Space Complexity @@ -763,6 +816,30 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {string[]} words + * @param {number[][]} queries + * @return {number[]} + */ + vowelStrings(words: string[], queries: number[][]): number[] { + let vowels = 0; + for (const c of 'aeiou') { + vowels |= 1 << (c.charCodeAt(0) - 97); + } + const prefix: number[] = [0]; + for (const w of words) { + const f = w.charCodeAt(0) - 97; + const l = w.charCodeAt(w.length - 1) - 97; + const isVowel = (1 << f) & vowels && (1 << l) & vowels ? 1 : 0; + prefix.push(prefix[prefix.length - 1] + isVowel); + } + return queries.map(([l, r]) => prefix[r + 1] - prefix[l]); + } +} +``` + ::tabs-end ### Time & Space Complexity 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/relative-sort-array.md b/articles/relative-sort-array.md index b89b2be5a..65cd7788c 100644 --- a/articles/relative-sort-array.md +++ b/articles/relative-sort-array.md @@ -243,6 +243,32 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {number[]} arr1 + * @param {number[]} arr2 + * @return {number[]} + */ + relativeSortArray(arr1: number[], arr2: number[]): number[] { + const res: number[] = []; + for (const num2 of arr2) { + for (let i = 0; i < arr1.length; i++) { + if (arr1[i] === num2) { + res.push(arr1[i]); + arr1[i] = -1; + } + } + } + arr1.sort((a, b) => a - b); + for (let i = res.length; i < arr1.length; i++) { + res.push(arr1[i]); + } + return res; + } +} +``` + ::tabs-end ### Time & Space Complexity @@ -520,6 +546,33 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {number[]} arr1 + * @param {number[]} arr2 + * @return {number[]} + */ + relativeSortArray(arr1: number[], arr2: number[]): number[] { + const arr2Set = new Set(arr2); + const count: Record = {}; + const end: number[] = []; + for (const num of arr1) { + if (!arr2Set.has(num)) end.push(num); + count[num] = (count[num] || 0) + 1; + } + end.sort((a, b) => a - b); + const res: number[] = []; + for (const num of arr2) { + for (let i = 0; i < count[num]; i++) { + res.push(num); + } + } + return res.concat(end); + } +} +``` + ::tabs-end ### Time & Space Complexity @@ -811,6 +864,38 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {number[]} arr1 + * @param {number[]} arr2 + * @return {number[]} + */ + relativeSortArray(arr1: number[], arr2: number[]): number[] { + const count: Record = {}; + for (const num of arr1) { + count[num] = (count[num] || 0) + 1; + } + const res: number[] = []; + for (const num of arr2) { + for (let i = 0; i < count[num]; i++) { + res.push(num); + } + delete count[num]; + } + const remaining = Object.keys(count) + .map(Number) + .sort((a, b) => a - b); + for (const num of remaining) { + for (let i = 0; i < count[num]; i++) { + res.push(num); + } + } + return res; + } +} +``` + ::tabs-end ### Time & Space Complexity @@ -1071,6 +1156,29 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {number[]} arr1 + * @param {number[]} arr2 + * @return {number[]} + */ + relativeSortArray(arr1: number[], arr2: number[]): number[] { + const max = Math.max(...arr1); + const count: number[] = new Array(max + 1).fill(0); + for (const num of arr1) count[num]++; + const res: number[] = []; + for (const num of arr2) { + while (count[num]-- > 0) res.push(num); + } + for (let num = 0; num < count.length; num++) { + while (count[num]-- > 0) res.push(num); + } + return res; + } +} +``` + ::tabs-end ### Time & Space Complexity @@ -1255,6 +1363,25 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {number[]} arr1 + * @param {number[]} arr2 + * @return {number[]} + */ + relativeSortArray(arr1: number[], arr2: number[]): number[] { + const index = new Map(); + arr2.forEach((num, i) => index.set(num, i)); + return arr1.sort((a, b) => { + const ia = index.has(a) ? index.get(a)! : 1000 + a; + const ib = index.has(b) ? index.get(b)! : 1000 + b; + return ia - ib; + }); + } +} +``` + ::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/sort-the-people.md b/articles/sort-the-people.md index 2ae77add0..cbfcf2140 100644 --- a/articles/sort-the-people.md +++ b/articles/sort-the-people.md @@ -190,6 +190,28 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {string[]} names + * @param {number[]} heights + * @return {string[]} + */ + sortPeople(names: string[], heights: number[]): string[] { + const map: Record = {}; + for (let i = 0; i < heights.length; i++) { + map[heights[i]] = names[i]; + } + heights.sort((a, b) => a - b); + const res: string[] = []; + for (let i = heights.length - 1; i >= 0; i--) { + res.push(map[heights[i]]); + } + return res; + } +} +``` + ::tabs-end ### Time & Space Complexity @@ -370,6 +392,21 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {string[]} names + * @param {number[]} heights + * @return {string[]} + */ + sortPeople(names: string[], heights: number[]): string[] { + const arr: [number, string][] = names.map((name, i) => [heights[i], name]); + arr.sort((a, b) => b[0] - a[0]); + return arr.map((pair) => pair[1]); + } +} +``` + ::tabs-end ### Time & Space Complexity @@ -528,6 +565,21 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {string[]} names + * @param {number[]} heights + * @return {string[]} + */ + sortPeople(names: string[], heights: number[]): string[] { + const indices: number[] = names.map((_, i) => i); + indices.sort((a, b) => heights[b] - heights[a]); + return indices.map((i) => names[i]); + } +} +``` + ::tabs-end ### Time & Space Complexity diff --git a/articles/special-array-with-x-elements-greater-than-or-equal-x.md b/articles/special-array-with-x-elements-greater-than-or-equal-x.md index 6a1327ffd..deb7e5dbc 100644 --- a/articles/special-array-with-x-elements-greater-than-or-equal-x.md +++ b/articles/special-array-with-x-elements-greater-than-or-equal-x.md @@ -190,6 +190,29 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {number[]} nums + * @return {number} + */ + specialArray(nums: number[]): number { + for (let i = 1; i <= nums.length; i++) { + let count = 0; + for (const num of nums) { + if (num >= i) { + count++; + } + } + if (count === i) { + return i; + } + } + return -1; + } +} +``` + ::tabs-end ### Time & Space Complexity @@ -428,6 +451,30 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {number[]} nums + * @return {number} + */ + specialArray(nums: number[]): number { + let l = 1, + r = nums.length; + while (l <= r) { + const mid = Math.floor((l + r) / 2); + const cnt = nums.filter((num) => num >= mid).length; + if (cnt === mid) return mid; + if (cnt < mid) { + r = mid - 1; + } else { + l = mid + 1; + } + } + return -1; + } +} +``` + ::tabs-end ### Time & Space Complexity @@ -700,6 +747,36 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {number[]} nums + * @return {number} + */ + specialArray(nums: number[]): number { + nums.sort((a, b) => a - b); + let i = 0, + prev = -1, + totalRight = nums.length; + while (i < nums.length) { + if ( + nums[i] === totalRight || + (prev < totalRight && totalRight < nums[i]) + ) { + return totalRight; + } + while (i + 1 < nums.length && nums[i] === nums[i + 1]) { + i++; + } + prev = nums[i]; + i++; + totalRight = nums.length - i; + } + return -1; + } +} +``` + ::tabs-end ### Time & Space Complexity @@ -926,6 +1003,29 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {number[]} nums + * @return {number} + */ + specialArray(nums: number[]): number { + nums.sort((a, b) => a - b); + const n = nums.length; + let i = 0, + j = 1; + while (i < n && j <= n) { + while (i < n && j > nums[i]) i++; + if (j == n - i) { + return j; + } + j++; + } + return -1; + } +} +``` + ::tabs-end ### Time & Space Complexity @@ -1142,6 +1242,30 @@ impl Solution { } ``` +```typescript +class Solution { + /** + * @param {number[]} nums + * @return {number} + */ + specialArray(nums: number[]): number { + const count: number[] = new Array(nums.length + 1).fill(0); + for (const num of nums) { + const index = Math.min(num, nums.length); + count[index]++; + } + let totalRight = 0; + for (let i = nums.length; i >= 0; i--) { + totalRight += count[i]; + if (i === totalRight) { + return totalRight; + } + } + return -1; + } +} +``` + ::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