class Solution {
public int binaryGap(int N) {
String binary = Integer.toBinaryString(N);
int first = 0;
while (first < binary.length() && binary.charAt(first) != '1'){
first ++;}
if (first == binary.length()) return 0;
int diff = Integer.MIN_VALUE;
int current = first;
for (int i = first+1; i diff){
diff = i - current;
}
current = i;
}
}
if (diff == Integer.MIN_VALUE) return 0;
return diff;
}
}
Author Archives: omargamaleldeen
884. Uncommon Words from Two Sentences
884. Uncommon Words from Two Sentences
class Solution {
public String[] uncommonFromSentences(String A, String B) {
List outputList = new ArrayList();
String[] As = A.split(" ");
String[] Bs = B.split(" ");
Map s = new HashMap();
for (int i = 0; i < As.length; i++){
if(s.get(As[i]) == null) s.put(As[i],1);
else s.put(As[i],s.get(As[i])+1);
}
for (int i = 0; i < Bs.length; i++){
if(s.get(Bs[i]) == null) s.put(Bs[i],1);
else s.put(Bs[i],s.get(Bs[i])+1);
}
for (String a: s.keySet()){
if ( s.get (a) == 1) outputList.add(a);
}
return outputList.toArray(new String[outputList.size()]);
}
}
938. Range Sum of BST
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int rangeSumBST(TreeNode root, int L, int R) {
int out = 0;
out = rangeSumBSTHelper(root,L,R);
return out;
}
public static int rangeSumBSTHelper(TreeNode root, int L, int R){
if(root == null) return 0;
int total = 0;
if( root.val >= L && root.val <= R)
total+= root.val;
if (root.right != null)
total+= rangeSumBSTHelper(root.right,L,R);
if (root.left != null)
total+=rangeSumBSTHelper(root.left,L,R);
return total;
}
}
872. Leaf-Similar Trees
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
ArrayList tree1 = new ArrayList();
ArrayList tree2 = new ArrayList();
traverse(root1, tree1);
traverse(root2, tree2);
if (tree1.size() != tree2.size()) return false;
for (int i = 0; i < tree1.size(); i++){
if (tree1.get(i) != tree2.get(i)) return false;
}
return true;
}
public static void traverse (TreeNode root, ArrayList tree){
if( root.right == null && root.left == null){
tree.add(root.val);
return;
}
if (root.right != null) traverse (root.right, tree);
if (root.left != null) traverse (root.left, tree);
}
}
How to view all of a channel’s uploads in a playlist?
Go to a video from this channel
click on channel name
Copy the string UC************** that is in the end of channel URL https://www.youtube.com/channel/UC**************
replace UC************** with UU**************
Go to this url that is a list of all uploads:
Enjoy!
Reference:
807. Max Increase to Keep City Skyline
807. Max Increase to Keep City Skyline
class Solution {
public int maxIncreaseKeepingSkyline(int[][] grid) {
int[] rowMax = new int[grid.length];
int[] colMax = new int[grid[0].length];
for (int i = 0; i < grid.length; i++){
int max = grid[i][0];
for (int j = 1; j max ){
max = grid[i][j];
}
}
rowMax[i] = max;
}
for (int j = 0 ; j < grid[0].length; j++){
int max = grid[0][j];
for (int i = 0; i max ){
max = grid[i][j];
}
}
colMax[j] = max;
}
int ans = 0;
for (int i = 0; i < grid.length; i++){
for (int j = 0; j < grid[i].length; j++){
int min = Math.min(rowMax[i],colMax[j]);
if(grid[i][j] <span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>< min){
int add = min - grid[i][j];
ans+= add;
}
}
}
return ans;
}
}
Note:
Write colMax and rowMax in only one loop, instead of two loops
int[] rowMaxes = new int[N];
int[] colMaxes = new int[N];
for (int r = 0; r < N; ++r)
for (int c = 0; c < N; ++c) {
rowMaxes[r] = Math.max(rowMaxes[r], grid[r][c]);
colMaxes[c] = Math.max(colMaxes[c], grid[r][c]);
}
String-3 > sumNumbers
public int sumNumbers(String str) {
return sumNumbersHelper(str,0);
}
public int sumNumbersHelper(String str, int index){
if (index >= str.length()) return 0;
int currentInt = 0;
if(Character.isDigit(str.charAt(index))){
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(str.charAt(index));
index++;
while(index < str.length() && Character.isDigit(str.charAt(index))){
stringBuilder.append(str.charAt(index));
index++;
}
currentInt = Integer.parseInt(stringBuilder.toString());
}
return currentInt +sumNumbersHelper(str, index+1);
}
350. Intersection of Two Arrays II
350. Intersection of Two Arrays II
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
Arrays.sort(nums1);
Arrays.sort(nums2);
int i = 0;
int j = 0;
ArrayList intersection = new ArrayList();
while (i < nums1.length && j < nums2.length){
if(nums1[i] == nums2[j]){
intersection.add(nums1[i]);
i++;
j++;
}else if (nums1[i] < nums2[j]){
i++;
} else if (nums2[j] < nums1[i]){
j++;
}
}
int[] out = new int[intersection.size()];
for(int k = 0; k < intersection.size(); k++)
out[k] = intersection.get(k);
return out;
}
}
961. N-Repeated Element in Size 2N Array
961. N-Repeated Element in Size 2N Array
class Solution {
public int repeatedNTimes(int[] A) {
int N = A.length/2;
HashMap freq = new HashMap();
for(int i = 0; i < A.length; i++){
if(freq.get(A[i]) == null)
freq.put(A[i],1);
else if(freq.get(A[i]) != null){
freq.put(A[i],freq.get(A[i]) + 1);
if(freq.get(A[i]) == N) return A[i];
}
}
return 0;
}
}
654. Maximum Binary Tree
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
return getMaximumNodeHelper(nums,0,nums.length - 1, new TreeNode(0));
}
public static TreeNode getMaximumNodeHelper(int[] nums, int start, int end, TreeNode n){
if(start == end && start end) return null;
int maxValue = Integer.MIN_VALUE;
int maxIndex = 0;
for (int i = start; i maxValue){
maxValue = nums[i];
maxIndex = i;
}
}
n.val = maxValue;
///
int maxValueRight = Integer.MIN_VALUE;
int maxIndexRight = maxIndex+1;
for (int i = maxIndex+1; i maxValueRight){
maxValueRight = nums[i];
maxIndexRight = i;
}
}
n.right = getMaximumNodeHelper(nums,maxIndex+1,end,new TreeNode(maxValueRight));
////
int maxValueLeft = Integer.MIN_VALUE;
int maxIndexLeft = maxIndex+1;
for (int i = start; i maxValueLeft){
maxValueLeft = nums[i];
maxIndexLeft = i;
}
}
n.left = getMaximumNodeHelper(nums,start,maxIndex-1,new TreeNode(maxValueLeft));
return n;
}
}