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
23 changes: 23 additions & 0 deletions HIndex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//time complexity- O(n log(n)) where n is array length
//space complexity- O(1)
import java.util.*;
public class HIndex {
static int hIndex(int[] citations) {
int n = citations.length;
int max = 0;
Arrays.sort(citations);

for(int i = 1; i <= n; i++) {
int count = 0;
for(int j = 0; j < n; j++) {
if(citations[j] >= i) count++;
}
if(count >= i) max = Math.max(max, i);
}
return max;
}
public static void main(String[] args) {
int[] citations = {3,0,6,1,5};
System.out.println("H index is: "+ hIndex(citations));
}
}
28 changes: 28 additions & 0 deletions RotateArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.Arrays;
//time complexity- O(n) where n is array length
//space complexity- O(1)
public class RotateArray {
static void rotate(int[] nums, int k) {
k = k % nums.length;
reverse(nums, 0, nums.length-1);
reverse(nums, 0, k-1);
reverse(nums, k, nums.length-1);

}
static void reverse(int[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
public static void main(String[] args) {
int [] nums = {1, 2, 3, 4, 5,6, 7};
int k = 3;
System.out.println("Before rotation: " + Arrays.toString(nums));
rotate(nums, k);
System.out.println("After rotation: " + Arrays.toString(nums));
}
}
36 changes: 36 additions & 0 deletions TrapRainWater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//time complexity- O(n) where n is array length
//space complexity- O(1)
public class TrapRainWater {
static int trap(int[] height) {
int n = height.length;
int left = 0;
int right = n - 1;

int leftMax = 0;
int rightMax = 0;
int sum = 0;

while(left < right) {
if(height[left] <= height[right]) {
if(leftMax > height[left]) {
sum += leftMax - height[left];
} else {
leftMax = Math.max(leftMax, height[left]);
}
left++;
} else {
if(rightMax > height[right]) {
sum += rightMax - height[right];
} else {
rightMax = Math.max(rightMax, height[right]);
}
right--;
}
}
return sum;
}
public static void main(String[] args) {
int[] height = {0,1,0,2,1,0,1,3,2,1,2,1};
System.out.println("Maximum water that can be stored is " + trap(height));
}
}