diff --git a/solution/0200-0299/0274.H-Index/README.md b/solution/0200-0299/0274.H-Index/README.md index c5434f8f1199a..9b74ede761104 100644 --- a/solution/0200-0299/0274.H-Index/README.md +++ b/solution/0200-0299/0274.H-Index/README.md @@ -85,6 +85,30 @@ class Solution { } ``` +### **TypeScript** + +```ts +function hIndex(citations: number[]): number { + let n = citations.length; + let cnt = new Array(n + 1).fill(0); + for (let c of citations) { + if ( c <= n) { + ++cnt[c]; + } else { + ++cnt[n]; + } + } + let sum = 0; + for (let i = n; i > -1; --i) { + sum += cnt[i]; + if (sum >= i) { + return i; + } + } + return 0; +}; +``` + ### **Go** 利用二分查找,定位符合条件的最大值 diff --git a/solution/0200-0299/0274.H-Index/README_EN.md b/solution/0200-0299/0274.H-Index/README_EN.md index f7d9bc8a30384..797338eea4f4a 100644 --- a/solution/0200-0299/0274.H-Index/README_EN.md +++ b/solution/0200-0299/0274.H-Index/README_EN.md @@ -89,6 +89,30 @@ class Solution { } ``` +### **TypeScript** + +```ts +function hIndex(citations: number[]): number { + let n = citations.length; + let cnt = new Array(n + 1).fill(0); + for (let c of citations) { + if ( c <= n) { + ++cnt[c]; + } else { + ++cnt[n]; + } + } + let sum = 0; + for (let i = n; i > -1; --i) { + sum += cnt[i]; + if (sum >= i) { + return i; + } + } + return 0; +}; +``` + ### **Go** Use binary search to locate the maximum value that meets the conditions diff --git a/solution/0200-0299/0274.H-Index/Solution.ts b/solution/0200-0299/0274.H-Index/Solution.ts new file mode 100644 index 0000000000000..c4bccbcf178bb --- /dev/null +++ b/solution/0200-0299/0274.H-Index/Solution.ts @@ -0,0 +1,19 @@ +function hIndex(citations: number[]): number { + let n = citations.length; + let cnt = new Array(n + 1).fill(0); + for (let c of citations) { + if ( c <= n) { + ++cnt[c]; + } else { + ++cnt[n]; + } + } + let sum = 0; + for (let i = n; i > -1; --i) { + sum += cnt[i]; + if (sum >= i) { + return i; + } + } + return 0; +}; \ No newline at end of file