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
10 changes: 10 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-14">
<attributes>
<attribute name="owner.project.facets" value="java"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="build/classes"/>
</classpath>
23 changes: 23 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Array-3</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
10 changes: 10 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=14
org.eclipse.jdt.core.compiler.compliance=14
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=14
4 changes: 4 additions & 0 deletions .settings/org.eclipse.wst.common.project.facet.core.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<installed facet="java" version="14"/>
</faceted-project>
Binary file added build/classes/HIndex.class
Binary file not shown.
Binary file added build/classes/MainHIndex.class
Binary file not shown.
Binary file added build/classes/RotatingArrayKToRight.class
Binary file not shown.
Binary file added build/classes/TrappingRainWater.class
Binary file not shown.
51 changes: 51 additions & 0 deletions src/HIndex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
*
*/

/**
* @author Aditya Sinha
* Create a count array (bucket sort) to store frequencies of citation counts.
* We only need a size up to n, as an h-index cannot exceed the total number of papers.
* Citations greater than n are grouped into the n-th bucket.
*
* Time Complexity - O(n)
* Space Complexity - O(n)
*
*/
public class HIndex {
public int hIndex(int[] citations) {
int n = citations.length;
int[] counts = new int[n + 1];

for (int c : citations) {
if (c >= n) {
counts[n]++;
} else {
counts[c]++;
}
}

int totalPapers = 0;
for (int h = n; h >= 0; h--) {
// Add the number of papers that have exactly 'h' citations to our running total.
totalPapers += counts[h];

// If the total number of papers with AT LEAST 'h' citations
// is greater than or equal to 'h' itself, we found the h-index.
if (totalPapers >= h) {
return h;
}
}

return 0;
}

}

class MainHIndex {
public static void main(String[] args) {
HIndex hi = new HIndex();
int [] citations = {3,0,6,1,5};
System.out.println("h-Index will be = " + hi.hIndex(citations));
}
}
47 changes: 47 additions & 0 deletions src/RotatingArrayKToRight.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
*
*/

/**
* @author Aditya Sinha
*
* first reverse the two parts of the array split at n - k separately.
* then reverse the whole array to get the rotated result.
* For left shift, reverse the whole array first, then reverse two parts accordingly.
*
* Time Complexity - O(n)
* Space Complexity - O(1)
*
*/
public class RotatingArrayKToRight {

public void rotate(int[] nums, int k) {
int n = nums.length;
k = k % n;

// Right shift
reverse(nums, 0, n - k - 1);
reverse(nums, n - k, n - 1);
reverse(nums, 0, n - 1);

/* Left shift
reverse(nums, 0, n - 1);
reverse(nums, 0, n - k - 1);
reverse(nums, n - k, n - 1);
*/
}

private void reverse(int[] nums, int left, int right) {
while(left < right) {
swap(nums, left, right);
left++;
right--;
}
}

private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
44 changes: 44 additions & 0 deletions src/TrappingRainWater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
*
*/

/**
* @author Aditya Sinha
*
* use two pointers from both ends and keep track of the tallest walls on each side.
* At each step, process the side with the smaller wall since that determines water trapping.
* add trapped water if current height is less than the known max wall on that side.
*
* Time Complexity - O(n)
* Space Complexity - O(1)
*
*/
public class TrappingRainWater {
public int trap(int[] height) {
int n = height.length;

int lw = 0, l = 0;
int rw = n - 1, r = n - 1;
int result = 0;

while(l <= r) {
if(height[rw] > height[lw]) { // if we have a bigger right wall, just process about the left wall
if(height[lw] > height[l]) {
result += height[lw] - height[l];
} else {
lw = l;
}
l++;
} else { // if we have a bigger left wall, just process about the right wall
if(height[rw] > height[r]) {
result += height[rw] - height[r];
} else {
rw = r;
}
r--;
}
}

return result;
}
}