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 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++) { |
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
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; |
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
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++; |
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
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(); |
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
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++) { |
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 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; | |
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
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(); |
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
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 |
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
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; |
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
/* | |
https://leetcode.com/problems/lemonade-change/ | |
*/ | |
class Solution { | |
public boolean lemonadeChange(int[] bills) { | |
int c1=0,c2=0,c3=0; | |
boolean flag = true; | |