Skip to content

feat: add typescript solution to lc problem: No.0274.H-Index #524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions solution/0200-0299/0274.H-Index/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**

利用二分查找,定位符合条件的最大值
Expand Down
24 changes: 24 additions & 0 deletions solution/0200-0299/0274.H-Index/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions solution/0200-0299/0274.H-Index/Solution.ts
Original file line number Diff line number Diff line change
@@ -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;
};