From 88269a462304283d1669a7e24f8ded9fc50141a5 Mon Sep 17 00:00:00 2001 From: zhaocchen Date: Sun, 18 Jul 2021 16:28:45 +0800 Subject: [PATCH] feat: add typescript solution to lc problem: No.0274.H-Index --- solution/0200-0299/0274.H-Index/README.md | 24 ++++++++++++++++++++ solution/0200-0299/0274.H-Index/README_EN.md | 24 ++++++++++++++++++++ solution/0200-0299/0274.H-Index/Solution.ts | 19 ++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 solution/0200-0299/0274.H-Index/Solution.ts 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