Skip to content

Instantly share code, notes, and snippets.

View wushbin's full-sized avatar

Shengbin Wu wushbin

  • San Francisco Bay Area
View GitHub Profile
class Solution {
public int smallestDivisor(int[] nums, int threshold) {
int max = Integer.MIN_VALUE;
for (int num : nums) {
max = Math.max(num, max);
}
int l = 1;
int r = max;
class Solution {
public int findRadius(int[] houses, int[] heaters) {
Arrays.sort(heaters);
int res = Integer.MIN_VALUE;
for (int h : houses) {
int currDist = getMinDist(heaters, h);
res = Math.max(currDist, res);
}
return res;
}
class Solution {
public int hIndex(int[] citations) {
if (citations == null || citations.length == 0) {
return 0;
}
int len = citations.length;
int l = 0;
int r = len - 1;
while(l < r) {
/**
Time: O (N * log(max - min) )
**/
class Solution {
// binary search: search range
public int kthSmallest(int[][] matrix, int k) {
int row = matrix.length;
int col = matrix[0].length;
class Solution {
public int mySqrt(int x) {
if (x <= 1) {
return x;
}
int l = 1;
int h = x;
// num -> search for the smallest number whose square is larger than x
// result = num - 1
while(l < h) {
/**
Solution 1: Dijkstra: Best first search
Time O(N * N): every point at most enqueue onece and dequeue once
Space O(N * N)
**/
class SolutionOne {
class Point implements Comparable<Point> {
int x;
int y;
/**
* // This is MountainArray's API interface.
* // You should not implement it, or speculate about its implementation
* interface MountainArray {
* public int get(int index) {}
* public int length() {}
* }
*/
class Solution {
/**
n = nums1.length, m = nums2.length
Time O(Log(min(n, m)))
Space: constant
**/
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
if (nums1.length > nums2.length) {
int[] temp = nums1;
nums1 = nums2;
/**
Solution1: prefix sum and binary search
Time: O(NlogN)
space; O(N)
**/
class Solution {
public int minSubArrayLen(int s, int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
class Solution {
public int nthMagicalNumber(int N, int A, int B) {
long f = (long) lcm(A, B);
long l = 2;
long r =(long) 1e14;
long base = (long)(1e9 + 7);
while(l < r) {
long mid = l + (r - l) / 2;