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
21 changes: 21 additions & 0 deletions H-Index.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public int hIndex(int[] citations) {
Arrays.sort(citations);
int low = 0;
int high = citations.length - 1;

while(low <= high){
int mid = low + (high - low)/2;
int diff = citations.length - mid;
if(diff == citations[mid]){
return diff;
}else if(diff > citations[mid]){
low = mid + 1;
}else{
high = mid - 1;
}
}

return citations.length - low;
}
}
19 changes: 19 additions & 0 deletions RotateArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public void rotate(int[] nums, int k) {
int n = nums.length;
k = k % n;
reverse(nums, 0, n - k-1);
reverse(nums, n-k, n-1);
reverse(nums, 0, n-1);
}

private void reverse(int[] arr, int start, int end){
while(start < end){
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
}
18 changes: 18 additions & 0 deletions TrapRainWater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public int trap(int[] height) {
int result = 0;
int n = height.length;
Stack<Integer> st = new Stack<>();
for(int i = 0; i < n; i++){
while(!st.isEmpty() && height[i] > height[st.peek()]){
int popped = st.pop();
if(st.isEmpty()) break;
int w = i - st.peek() - 1;
int h = Math.min(height[st.peek()], height[i]) - height[popped];
result+= h*w;
}
st.push(i);
}
return result;
}
}