class Solution {
public int[] countBits(int n) {
int[] res = new int[n+1];
res[0] = 0;
for(int i=1;i<=n ;i++){
int num = i;
while(num>0){
int temp = num%2;
res[i] = res[i]+temp;
num = num/2;
}
}
return res;
}
}
class Solution {
public int maxProfit(int[] prices) {
int res = 0 ;
int min = prices[0];
if(prices.length <= 1){
return res;
}
for(int i = 1;i<prices.length;i++){
min = Math.min(prices[i],min);
res = Math.max(prices[i] - min,res);
}
return res;
}
}
class Solution {
public void moveZeroes(int[] nums) {
int j=0;
for(int i=0;i<nums.length;i++){
if(nums[i] != 0){
nums[j] = nums[i];
j++;
}
}
for(int i = j;i<nums.length;i++){
nums[i] = 0;
}
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private int dia = 0;
public int diameterOfBinaryTree(TreeNode root) {
calculateHeight(root);
return dia;
}
private int calculateHeight(TreeNode node){
if(node == null){
return 0;
}
int left = calculateHeight(node.left);
int right = calculateHeight(node.right);
dia = Math.max(dia,left +right);
return Math.max(left,right)+1;
}
}