Skip to content

Instantly share code, notes, and snippets.

View wkdalsgh192's full-sized avatar
🏠
Working from home

Blue_Avocado wkdalsgh192

🏠
Working from home
View GitHub Profile
class Solution {
public int solution(int n, int[] lost, int[] reserve) {
int[] numbers = new int[n+2];
for (int i : lost) numbers[i]--;
for (int i : reserve) numbers[i]++;
for (int i = 1; i < numbers.length-1; i++) {
import java.util.*;
class Solution {
public boolean isRobotBounded(String instructions) {
boolean answer = false; // the robot is set to leave the circle at first
char[] arr = instructions.toCharArray();
int idx = 0;
int dir = 0;
@wkdalsgh192
wkdalsgh192 / gist:4a06521adfdf5adb56573f1c97590071
Created December 30, 2020 14:18
794. Valid Tic-Tac-Toe State
public class TicTacToe {
public boolean validTicTacToe(String[] board) {
// count O and X
int cntX = 0;
int cntO = 0;
for (String str: board) {
for (char ch: str.toCharArray()) {
if (ch=='X') cntX++;
@wkdalsgh192
wkdalsgh192 / gist:3b148c2afc517a7d78f136ecc3e200d9
Created December 31, 2020 02:54
535. Encode and Decode TinyURL
import java.util.HashMap;
import java.util.Map;
public class Codec {
// Encodes a URL to a shortened URL.
private final static String prefix = "http://tinyurl.com/";
public static Map<String, String> map = new HashMap<>();
public String encode(String longUrl) {
Integer hashcode = longUrl.hashCode();
public class BST {
public int numTrees(int n) {
int[] arr = new int[20];
arr[0] = 1;
arr[1] = 1;
int cnt = 0;
for (int i = 2; i <= n; i++) {
class Solution {
public int kConcatenationMaxSum(int[] arr, int k) {
//case 1 when k==1
if(k==1)
return kadanes(arr);
long sum=0;
for(int ele:arr)
sum+=ele;
import java.util.*;
class Solution {
public String solution(String number, int k) {
char[] arr = number.toCharArray();
Stack<Integer> list = new Stack<>();
for (int i = 0; i < arr.length; i++) {
int curr = (int) arr[i] - '0';
while (!list.isEmpty() && k>0 && list.peek() < curr) {
list.pop();
KRUSKAL(G):
A = βˆ…
For each vertex v ∈ G.V:
MAKE-SET(v)
For each edge (u, v) ∈ G.E ordered by increasing order by weight(u, v):
if FIND-SET(u) β‰  FIND-SET(v):
A = A βˆͺ {(u, v)}
UNION(u, v)
return A
public class ConnectingIsland {
static class Edge implements Comparable<Edge> {
int from;
int to;
int cost;
public Edge(int from, int to, int cost) {
super();
this.from = from;
this.to = to;
/*
https://leetcode.com/problems/lemonade-change/
*/
class Solution {
public boolean lemonadeChange(int[] bills) {
int c1=0,c2=0,c3=0;
boolean flag = true;