Skip to content

Instantly share code, notes, and snippets.

View vishalg0wda's full-sized avatar

Vishal Gowda vishalg0wda

  • Lacework
  • London, UK
View GitHub Profile
@vishalg0wda
vishalg0wda / PayNotificationListener.java
Last active July 16, 2021 10:07
Composite annotation that applies required property overrides and proxies other commonly used parameters to meta annotations.
package com.booking.payments.point.common.core.annotation;
import com.booking.payments.point.common.core.model.MetricNames;
import io.micrometer.core.annotation.Timed;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.beans.factory.annotation.Qualifier;
class Solution {
public int[][] merge(int[][] intervals) {
Map<Integer, Integer> intervalMap = new HashMap<>();
for (int[] interval: intervals) {
intervalMap.putIfAbsent(interval[0], 0);
intervalMap.compute(interval[0], (k, v) -> v > interval[1] ? v : interval[1]);
}
Map<Integer, Integer> merged = new HashMap<>();
List<Integer[]> out = new LinkedList<>();
int minStart = -1, maxEnd = -1;
@vishalg0wda
vishalg0wda / FindWord.java
Created June 6, 2021 10:16
FindWord.java
class Solution {
public boolean exist(char[][] board, String word) {
if (word == null || word.length() == 0) return true;
boolean[][] visited = new boolean[board.length][board.length];
Character startChar = word.charAt(0);
for (int r = 0; r < board.length; r++) {
for (int c = 0; c < board.length; c++) {
visited[r][c] = true;
if (board[r][c] == startChar && backtracking(1, r, c, visited, board, word)) return true;
visited[r][c] = false;
@vishalg0wda
vishalg0wda / rotting-oranges.py
Last active May 8, 2021 12:12
Rotting Oranges
from collections import deque
class Solution:
def __init__(self):
self.grid = None
self.rows = 0
self.cols = 0
def bfs(self, rottens: list[tuple[int]]) -> int:
q = deque([(rr, rc, 0) for (rr, rc) in rottens])
minutes = 0
@vishalg0wda
vishalg0wda / knights_probability.py
Created March 28, 2021 21:29
knights_probability - OS
class Solution:
def knightProbability(self, N: int, K: int, r: int, c: int) -> float:
moves = [(-2, 1), (-2, -1), (-1, 2), (-1, -2), (1, 2), (1, -2), (2, 1), (2, -1)]
dp0 = [[1] * N for _ in range(N)]
to_visit = {(r, c)}
count = 0
for _ in range(K):
dp1 = [[0] * N for _ in range(N)]
new_to_visit = set()
for (r, c) in to_visit:
@vishalg0wda
vishalg0wda / knight_probability.py
Created March 28, 2021 11:47
knight-probability-in-chessboard (BF)
def knightProbability(self, N: int, K: int, r: int, c: int) -> float:
grid = [[0] * N for _ in range(N)]
# utility to count number of moves given a starting position
def recur(r: int, c: int, k: int) -> int:
if not (0 <= r < N and 0 <= c < N):
return 0
if k == 0:
return 1
# try all 8 possible moves
return (
@vishalg0wda
vishalg0wda / kth_lowest_with_updates.py
Last active March 23, 2021 11:03
kth_lowest_with_updates.py
from typing import Generator
class TreeNode:
def __init__(self, key, val):
self.key = key
self.val = val
self.left = None
self.right = None
def __str__(self):