Skip to content
Open
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
54 changes: 54 additions & 0 deletions h-index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Time complexity - O(nlog(n)), n number of citations
// Space complexity - O(1)
function hIndex(citations: number[]): number {
let result = 0;
citations.sort((a, b) => a - b);
let n = citations.length;
let l = 0;
let r = n - 1;

while (l <= r) {
const mid = Math.floor((l + r) / 2);

// possible h-index: number of papers from mid to n-1 (n - mid)
const diff = n - mid;

// if we found exact number of citations, return the h-index
if (citations[mid] === diff) return diff;

// citations at mid is higher than possible h-index
// to find a more possible citations count, search on the left side
if (citations[mid] > diff) {
r = mid - 1;
} else {
// citations at mid is lower than possible h-index
// to find a possible citations count, search on the right side
l = mid + 1;
}
}

// after the loop is complete, "l" points to the first index where citations[l] >= n - l.
return n - l;
}

// Time complexity - O(n), n number of citations
// Space complexity - O(n + 1)

// function hIndex(citations: number[]): number {
// const n = citations.length;
// const bucket = new Array(n + 1).fill(0);
// for (let citation of citations) {
// if (citation > n) {
// bucket[n]++;
// } else {
// bucket[citation]++;
// }
// }
// let sum = 0;
// for (let i = n; i >= 0; i--) {
// sum += bucket[i];

// if (sum >= i) return i;
// }
// return -1;
// }
29 changes: 29 additions & 0 deletions rotate-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Time complexity - O(n), n is the number of elements in nums array
// Space complexity - O(1), no extra space used
function rotate(nums: number[], k: number): void {
const n = nums.length;

// handling case if k is larger than n
k = k % n;

// reverse the entire array
reverse(0, n - 1);
// reverse the first k element
reverse(0, k - 1);
// reverse the k...n-1 element
reverse(k, n - 1);

function reverse(left: number, right: number) {
while (left < right) {
swap(left, right);
left++;
right--;
}
}

function swap(i: number, j: number) {
const temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
29 changes: 29 additions & 0 deletions trapping-rain-water.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function trap(height: number[]): number {
const n = height.length;
let r = n - 1;
let l = 0;
let lw = 0;
let rw = 0;
let result = 0;

while (l <= r) {
if (rw > lw) {
// rw acting as a dam, so we can trap water on the left side
if (height[l] > lw) {
lw = height[l];
} else {
result += 1 * lw - height[l];
}
l++;
} else {
// lw acting as a dam, so we can trap water on the right side
if (height[r] > rw) {
rw = height[r];
} else {
result += 1 * rw - height[r];
}
r--;
}
}
return result;
}