This file contains 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
<!DOCTYPE html> | |
<html style="background-color:#F6F7FC;"> | |
<head> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<style> | |
/* width */ | |
::-webkit-scrollbar { | |
width: 10px; | |
} | |
This file contains 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
public class MinEmptySpaceInChessboard { | |
/** | |
* From Google: | |
* Given n * n chessboard and pos array means position (i, j) has chess. | |
* Calculate min distance from each chess to the empty space. | |
* ex: n = 3, pos = {{0, 0}, {0, 1}, {0, 2}, {1, 1}, {1, 2}, {2, 0}} | |
* ans: [1, 2, 2, 1, 1, 1] | |
* | |
* follow up: if n = ∞ , pos has just few chess. How to improve? |
This file contains 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 boolean canPartitionKSubsets(int[] nums, int k) { | |
int sum = 0; | |
int max = 0; | |
for(int e: nums) { | |
sum += e; | |
max = Math.max(max, e); | |
} | |
if(sum % k > 0 || max > (sum / k)) return false; |
This file contains 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
//dijkstra TLE | |
public int minimumObstacles(int[][] grid) { | |
//dijkstra | |
int[][] ref = new int[][]{{-1,0}, {1,0}, {0,-1}, {0,1}}; | |
int[][] minDistance = new int[grid.length][grid[0].length]; | |
for(int[] e: minDistance) { | |
Arrays.fill(e, Integer.MAX_VALUE); |
This file contains 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
//Total time complexity O(n), space complexity is O(k) | |
public class findMoreThanKthLargest { | |
public static void main(String[] args) { | |
int[][] example = new int[][]{{1426828011, 9}, | |
{1426828028, 350}, | |
{1426828037, 25}, | |
{1426828056, 231}, | |
{1426828058, 109}, | |
{1426828066, 111}}; |
This file contains 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
public int largestCombination(int[] candidates) { | |
int min = Integer.MAX_VALUE; | |
int max = Integer.MIN_VALUE; | |
for(int i = 0; i < candidates.length; i++) { | |
min = Math.min(min, candidates[i]); | |
max = Math.max(max, candidates[i]); | |
} | |
while((min & min - 1) > 0) { | |
min &= min - 1; |
This file contains 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 boolean hasValidPath(char[][] grid) { | |
int m = grid.length; | |
int n = grid[0].length; | |
int[][] ref = new int[m][n]; | |
for(int i = 0; i < m; i++) { | |
for(int j = 0; j < n; j++) { | |
if(grid[i][j] == '(') { | |
ref[i][j] = 1; |
This file contains 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 shortestPathLength(int[][] graph) { | |
if(graph.length == 1) return 0; | |
int n = graph.length; | |
//for example n = 0,1,2. The bitmask finish criteria = 111(7) | |
//so we can easily count (1 << 3) - 1 = 7 or use pow. | |
int finish = (int) (1 << n) - 1; | |
Queue<int[]> queue = new LinkedList<>(); |
This file contains 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
// (X) 63 / 67 test cases passed. | |
class Solution { | |
long BASE = 26; | |
long MOD = 1 << 31 - 1; | |
public String longestDupSubstring(String s) { | |
//rolling hash + binary search | |
//ex: banana, is any length contains duplicate string? | |
//(Y)length1: b, a, n | |
//(Y)length2: ba, an, na | |
//(Y)length3: ban,ana,nan |
This file contains 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 long subArrayRanges(int[] nums) { | |
//Monotonic Stack O(n) | |
int n = nums.length; | |
Stack<Integer> stack = new Stack<>(); | |
int[] leftMin = new int[n]; | |
int[] rightMin = new int[n]; | |
int[] leftMax = new int[n]; | |
int[] rightMax = new int[n]; | |
for(int i = 0; i < n; i++) { |
NewerOlder