Skip to content

Instantly share code, notes, and snippets.

@chrisynchen
chrisynchen / gist:72d9c1ebcb54818da2a465cf5247e50d
Created May 8, 2022 05:04
2267. Check if There Is a Valid Parentheses String Path
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;
@chrisynchen
chrisynchen / gist:8b6816cee07c8e6d4a9cff33e8f37b9b
Last active May 15, 2022 03:38
6065. Largest Combination With Bitwise AND Greater Than Zero
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;
//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}};
@chrisynchen
chrisynchen / gist:68591760c95ca595560ba2b0641f59c8
Created May 29, 2022 16:02
2290. Minimum Obstacle Removal to Reach Corner
//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);
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;
@chrisynchen
chrisynchen / MinEmptySpaceInChessboard
Last active August 11, 2022 10:14
MinEmptySpaceInChessboard
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?
<!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;
}