Skip to content

Instantly share code, notes, and snippets.

@a947
a947 / KthSmallestInSortedMatrix.java
Last active August 1, 2019 10:41
KthSmallestInSortedMatrix
class KthSmallestInSortedMatrix {
public static int findKthSmallest(int[][] matrix, int k) {
int n = matrix.length;
int start = matrix[0][0], end = matrix[n - 1][n - 1];
while (start < end) {
int mid = start + (end - start) / 2;
// first number is the smallest and the second number is the largest
int[] smallLargePair = { matrix[0][0], matrix[n - 1][n - 1] };
int count = countLessEqual(matrix, mid, smallLargePair);
@a947
a947 / SearchInfiniteSortedArray.java
Last active August 1, 2019 10:21
SearchInfiniteSortedArray
class ArrayReader {
int[] arr;
ArrayReader(int[] arr) {
this.arr = arr;
}
public int get(int index) {
if (index >= arr.length)
return Integer.MAX_VALUE;
@a947
a947 / BinarySearch.java
Created August 1, 2019 10:12
BinarySearch
class BinarySearch {
public static int search(int[] arr, int key) {
int start = 0, end = arr.length - 1;
boolean isAscending = arr[start] < arr[end];
while (start <= end) {
// calculate the middle of the current range
int mid = start + (end - start) / 2;
if (key == arr[mid])
@a947
a947 / PairWithTargetSum.java
Created July 18, 2019 09:52
PairWithTargetSum
class PairWithTargetSum {
public static int[] search(int[] arr, int targetSum) {
int left = 0, right = arr.length - 1;
while (left < right) {
// comparing the sum of two numbers to the 'targetSum' can cause integer overflow
// so, we will try to find a target difference instead
int targetDiff = targetSum - arr[left];
if (targetDiff == arr[right])
return new int[] { left, right }; // found the pair
@a947
a947 / TreePathSum.java
Last active July 17, 2019 11:01
TreePathSum
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
};
@a947
a947 / Subsets.java
Last active July 17, 2019 10:59
Subsets
import java.util.*;
class Subsets {
public static List<List<Integer>> findSubsets(int[] nums) {
List<List<Integer>> subsets = new ArrayList<>();
// start by adding the empty subset
subsets.add(new ArrayList<>());
for (int currentNumber : nums) {
// we will take all existing subsets and insert the current number in them to
@a947
a947 / KClosestPointsToOrigin.java
Last active July 17, 2019 10:56
KClosestPointsToOrigin
import java.util.*;
class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@a947
a947 / WaterContainer.java
Created July 17, 2019 09:58
WaterContainer
class WaterContainer {
public static int findMaxWater(int[] buildingHeights) {
// use two pointer approach to find the maximum area
int left = 0, right = buildingHeights.length - 1;
int maxArea = 0, currentArea = 0;
while (left < right) {
if (buildingHeights[left] < buildingHeights[right]) {
currentArea = (right - left) * buildingHeights[left];
@a947
a947 / BAM.java
Created July 17, 2019 06:28
Bitonic Array Maximum
class MaxInBitonicArray {
public static int findMax(int[] arr) {
int start = 0, end = arr.length - 1;
while (start < end) {
int mid = start + (end - start) / 2;
if (arr[mid] > arr[mid + 1]) {
end = mid;
} else {
start = mid + 1;