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

+35
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

+1-1
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));
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

+14
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

+10
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

-21
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.

Easy/IntersectionOfTwoArrays.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.util.*;
2+
3+
class IntersectionOfTwoArrays {
4+
public static void main(String[] args) {
5+
int[] nums1 = {1,2,2,1};
6+
int[] nums2 = {2,2};
7+
Integer res[] = intersect(nums1, nums2);
8+
for(int i: res) System.out.println(i);
9+
}
10+
11+
public static Integer[] intersect(int[] nums1, int[] nums2) {
12+
13+
// freq count for nums1
14+
HashMap<Integer, Integer> map = new HashMap<>();
15+
16+
for (int num : nums1)
17+
map.put(num, map.getOrDefault(num, 0) + 1);
18+
19+
// collect result
20+
ArrayList<Integer> result = new ArrayList<>();
21+
22+
for (int num: nums2) {
23+
24+
if (map.containsKey(num)){
25+
result.add(num);
26+
map.put(num, map.get(num) - 1);
27+
map.remove(num, 0);
28+
}
29+
}
30+
31+
Integer[] res = result.toArray(new Integer[result.size()]);
32+
33+
// convert result MANUAL WAY OF CONVERTING TO INT[]
34+
int[] r = new int[result.size()];
35+
for(int i = 0; i < result.size(); i++) {
36+
r[i] = result.get(i);
37+
}
38+
39+
return res;
40+
}
41+
}

Easy/IsomorphicString.class

-1.24 KB
Binary file not shown.

Easy/LongestPrefix.class

-1.2 KB
Binary file not shown.

Easy/MaxDistanceToClosest.class

-770 Bytes
Binary file not shown.

Easy/MaximumDepthOfBinaryTree.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class TreeNode {
2+
int val;
3+
TreeNode left;
4+
TreeNode right;
5+
6+
TreeNode(int x) {
7+
val = x;
8+
}
9+
}
10+
11+
class MaxDepth {
12+
13+
public int maxDepth(TreeNode root) {
14+
15+
if(root == null){
16+
return 0;
17+
}
18+
19+
int leftDepth = maxDepth(root.left);
20+
int rightDepth = maxDepth(root.right);
21+
22+
if(leftDepth > rightDepth)
23+
return leftDepth+1;
24+
else
25+
return rightDepth+1;
26+
}
27+
}

Easy/MergeTwoSortedLists.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class ListNode {
2+
3+
int val;
4+
ListNode next;
5+
6+
ListNode(int x) {
7+
val = x;
8+
}
9+
}
10+
11+
class Solution {
12+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
13+
if (l1 == null)
14+
return l2;
15+
if (l2 == null)
16+
return l1;
17+
18+
if (l1.val < l2.val) {
19+
l1.next = mergeTwoLists(l1.next, l2);
20+
return l1;
21+
22+
} else {
23+
l2.next = mergeTwoLists(l1, l2.next);
24+
return l2;
25+
}
26+
}
27+
}

Easy/MinCostClimbingStairs.class

-670 Bytes
Binary file not shown.
File renamed without changes.

Easy/NumberOf1Bits.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
public class NumberOf1Bits {
2+
// you need to treat n as an unsigned value
3+
public int hammingWeight(int n) {
4+
5+
6+
//1 way of doing
7+
//return Integer.bitCount(n);
8+
9+
10+
//2nd way of doing
11+
/*int count = 0;
12+
13+
while(n != 0){
14+
n = n & (n - 1);
15+
count++;
16+
}
17+
18+
return count;*/
19+
20+
//3rd way of doing
21+
int bits = 0;
22+
int mask = 1;
23+
24+
for (int i = 0; i < 32; i++) {
25+
if ((n & mask) != 0) {
26+
bits++;
27+
}
28+
mask <<= 1;
29+
}
30+
return bits;
31+
}
32+
}
File renamed without changes.

Easy/Parenthesis.class

-1.02 KB
Binary file not shown.

Easy/PascalsTriangle.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public List<List<Integer>> generate(int numRows) {
5+
6+
List<List<Integer>> result = new ArrayList<>();
7+
8+
for(int i = 0 ; i < numRows; i++) {
9+
List<Integer> list = new ArrayList<>();
10+
11+
for(int j = 0; j < i + 1 ; j++) {
12+
if(j == 0 || j == i) {
13+
list.add(1);
14+
}
15+
else {
16+
int a = result.get(i - 1).get(j - 1);
17+
int b = result.get(i - 1).get(j);
18+
list.add(a + b);
19+
}
20+
}
21+
result.add(list);
22+
}
23+
24+
return result;
25+
26+
}
27+
}

Easy/PowerOfNumber.class

-555 Bytes
Binary file not shown.

Easy/RemoveDupInArray.class

-912 Bytes
Binary file not shown.
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode deleteDuplicates(ListNode head) {
11+
ListNode list = head;
12+
13+
while(list != null) {
14+
if (list.next == null) {
15+
break;
16+
}
17+
if (list.val == list.next.val) {
18+
list.next = list.next.next;
19+
} else {
20+
list = list.next;
21+
}
22+
}
23+
24+
return head;
25+
}
26+
}

Easy/Reverse32bit.java renamed to Easy/ReverseBits.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class Reverse32bit {
1+
class ReverseBits {
22
public int reverse(int x) {
33
boolean overflowed = false;
44
int result = 0;

Easy/ReverseOnlyLetters.class

-1.29 KB
Binary file not shown.

Easy/RomanToInteger.class

-1.07 KB
Binary file not shown.

Easy/ScoreOfParenthesis.class

-1.11 KB
Binary file not shown.

Easy/SearchInsertPosition.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int searchInsert(int[] A, int target) {
3+
int low = 0, high = A.length-1;
4+
5+
while(low <= high){
6+
7+
int mid = (low + high)/2;
8+
9+
if(A[mid] == target)
10+
return mid;
11+
12+
else if(A[mid] > target)
13+
high = mid-1;
14+
15+
else low = mid+1;
16+
}
17+
return low;
18+
}
19+
}

Easy/Solution.class

-922 Bytes
Binary file not shown.

Easy/SortedArrayToBST.class

-908 Bytes
Binary file not shown.

Easy/SplitInPattern.class

-1010 Bytes
Binary file not shown.

Easy/StockBuySell.class

-974 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)