Skip to content

Commit 60b64d1

Browse files
committed
Added some Easy questions 🎉
1 parent 8b39092 commit 60b64d1

40 files changed

+561
-23
lines changed

Easy/BalancedBinaryTree.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
public class TreeNode {
2+
int val;
3+
TreeNode left;
4+
TreeNode right;
5+
TreeNode(int x) { val = x; }
6+
}
7+
class Solution {
8+
9+
public boolean isBalanced(TreeNode root) {
10+
int left;
11+
int right;
12+
13+
if(root == null)
14+
return true;
15+
16+
left = height(root.left);
17+
right = height(root.right);
18+
19+
if(Math.abs(left-right) <=1 && isBalanced(root.left) && isBalanced(root.right))
20+
return true;
21+
22+
return false;
23+
}
24+
25+
int height(TreeNode node)
26+
{
27+
/* base case tree is empty */
28+
if (node == null)
29+
return 0;
30+
31+
/* If tree is not empty then height = 1 + max of left
32+
height and right heights */
33+
return 1 + Math.max(height(node.left), height(node.right));
34+
}
35+
}

Easy/StockBuySell.java renamed to Easy/BestTimeToBuyAndSellStock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class StockBuySell {
1+
class Solution {
22
public static void main(String[] args) {
33
int[] arr = {7,1,5,3,6,4};
44
System.out.println(maxProfit(arr));
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class TreeNode {
2+
int val;
3+
TreeNode left;
4+
TreeNode right;
5+
TreeNode(int x) { val = x; }
6+
}
7+
class ConvertSortedArrayToBinarySearchTree {
8+
9+
public static void main(String[] args) {
10+
int[] nums = {-10,-3,0,5,9};
11+
TreeNode head = sortedArrayToBST(nums);
12+
System.out.println(head.val);
13+
/*System.out.println(head.val);
14+
System.out.println(head.left.val);
15+
System.out.println(head.left.right.val);
16+
System.out.println(head.right.val);
17+
System.out.println(head.right.right.val);
18+
*/
19+
}
20+
21+
public static TreeNode sortedArrayToBST(int[] nums) {
22+
if(nums.length == 0)
23+
return null;
24+
25+
return buildBST(nums, 0, nums.length - 1);
26+
}
27+
28+
public static TreeNode buildBST(int[] nums, int start, int end) {
29+
if(start > end)
30+
return null;
31+
32+
int mid = start + (end - start) / 2;
33+
TreeNode root = new TreeNode(nums[mid]);
34+
35+
root.left = buildBST(nums, start, mid - 1);
36+
root.right = buildBST(nums, mid + 1, end);
37+
38+
return root;
39+
}
40+
}

Easy/CountAndSay.class

-956 Bytes
Binary file not shown.

Easy/DistributeCandies.class

-1.16 KB
Binary file not shown.

Easy/ExcelSheetColumnNumber.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int titleToNumber(String s) {
3+
4+
int result = 0;
5+
6+
for (int i = 0; i < s.length(); i++){ //for s = A => 65-65=0 => Add 1 to it
7+
8+
result *= 26; //For AA = 1*26 = 26 + 1 = 27
9+
result += ((s.charAt(i) - 'A') + 1);
10+
}
11+
12+
return result;
13+
}
14+
}

Easy/FactorialTrailingZeroes.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution{
2+
3+
public int trailingZero(int n) { //Eg 25
4+
5+
return n == 0 ? 0 : (n/2 + trailingZero(n/2));
6+
7+
//25/2 = 5 + (25/2) => 5 + 1 = 6
8+
9+
}
10+
}

Easy/HappyNumber.class

-894 Bytes
Binary file not shown.

Easy/HouseRobber.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,5 @@
11
class Solution {
22

3-
/* BEST SOLUTION
4-
TIME : O(n)
5-
SPACE: O(1)
6-
*/
7-
public int robEff(int[] nums) {
8-
if (nums.length == 0) return 0;
9-
10-
int prev1 = 0;
11-
int prev2 = 0;
12-
13-
for (int num : nums) {
14-
int tmp = prev1;
15-
prev1 = Math.max(prev2 + num, prev1);
16-
prev2 = tmp;
17-
}
18-
19-
return prev1;
20-
}
21-
22-
/* ANOTHER WAY */
233
public int rob(int[] nums) { //Eg 2, 7 ,9 ,3 ,1
244

255
int n = nums.length; //n=5
@@ -39,7 +19,6 @@ public int rob(int[] nums) { //Eg 2, 7 ,9 ,3 ,1
3919
return Math.max(nums[n-1], nums[n-2]); //Max(12,10) => return 12 correct ans
4020
}
4121

42-
4322
/* ANOTHER APPROACH */
4423
public int rob2(int[] nums) {
4524
if (nums.length == 0) return 0;

Easy/IntersectionOfTwoArray.class

-1.59 KB
Binary file not shown.

0 commit comments

Comments
 (0)