This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| 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; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| 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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * // 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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |