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
use rand::prelude::SliceRandom; | |
use rand::random; | |
use std::fmt::Debug; | |
use std::sync::mpsc; | |
use std::thread; | |
use std::time::Duration; | |
use tokio::sync::oneshot; | |
use tracing::{info, instrument, span, trace, Level}; | |
use tracing_subscriber::layer::SubscriberExt; | |
use tracing_subscriber::util::SubscriberInitExt; |
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
use std::io::{BufRead, BufReader, BufWriter, Read, Write}; | |
use std::net::{Shutdown, SocketAddr, TcpListener, TcpStream}; | |
fn server(addr: SocketAddr) { | |
let listener = TcpListener::bind(addr).unwrap(); | |
let (con, _) = listener.accept().unwrap(); | |
let reader = BufReader::new(&con); | |
let mut writer = BufWriter::new(&con); | |
for msg in reader.lines() { | |
let Ok(msg) = msg else { |
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
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; |
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[][] 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; |
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 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; |
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
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 |
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: | |
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: |
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
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 ( |
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
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): |