Skip to content

Instantly share code, notes, and snippets.

View riyafa's full-sized avatar

Riyafa Abdul Hameed riyafa

View GitHub Profile
@riyafa
riyafa / ArrangingCoinsBS.java
Created March 15, 2022 07:33
Binary search solution for 441. Arranging Coins: https://youtu.be/H-Tdu8qJ_uk
class ArrangingCoinsBS {
public int arrangeCoins(int n) {
int low = 1;
int high = n;
while(low < high) {
int mid = low +(high -low+1)/2;
long num = (((long)mid * ((long)mid +1l))/2l);
if(num == n) {
return mid;
} else if(n < num){
class RomanToInteger {
public int romanToInt(String s) {
int prev = 0;
int result = 0;
for(int i = s.length() -1; i >= 0; i--) {
char cur = s.charAt(i);
int curVal = getInteger(cur);
if(curVal < prev) {
result -= curVal;
} else {
class MinimumDepthBinaryTreeDFS {
public int minDepth(TreeNode root) {
if(root == null) {
return 0;
}
int leftHeight = minDepth(root.left);
int rightHeight = minDepth(root.right);
if(leftHeight == 0 || rightHeight == 0) {
return leftHeight + rightHeight + 1;
}
class MinimumDepthBinaryTreeBFS {
public int minDepth(TreeNode root) {
if(root == null) {
return 0;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
int level = 0;
while(!queue.isEmpty()) {
level++;
Hello World!