Skip to content

Commit f0fe034

Browse files
committed
Commit #120 - Modified 6 file(s) - 12.06.2023 @16:41
1 parent 189a620 commit f0fe034

File tree

6 files changed

+173
-4
lines changed

6 files changed

+173
-4
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//Attempt #1 - 5mins
2+
//Link: https://leetcode.com/problems/snapshot-array/description/
3+
4+
class SnapshotArray {
5+
int arr[];
6+
HashMap<Integer,int[]> snapMap = new HashMap<Integer,int[]>();
7+
int ID = 0;
8+
9+
public SnapshotArray(int length) {
10+
arr = new int[length];
11+
}
12+
13+
public void set(int index, int val) {
14+
arr[index] = val;
15+
}
16+
17+
public int snap() {
18+
int arrCopy[] = arr.clone();
19+
snapMap.put(ID++, arrCopy);
20+
return ID-1;
21+
}
22+
23+
public int get(int index, int snap_id) {
24+
int arr[] = snapMap.get(snap_id);
25+
return arr[index];
26+
}
27+
}
28+
29+
/*
30+
MLE - 69/74
31+
*/
32+
33+
//Optimal Soln - using treemap
34+
35+
class SnapshotArray {
36+
TreeMap<Integer, Integer>[] Tm;
37+
int snap_id = 0;
38+
public SnapshotArray(int length) {
39+
Tm = new TreeMap[length];
40+
for (int i = 0; i < length; i++) {
41+
Tm[i] = new TreeMap<Integer, Integer>();
42+
Tm[i].put(0, 0);
43+
}
44+
}
45+
46+
public void set(int index, int val) {
47+
Tm[index].put(snap_id, val);
48+
}
49+
50+
public int snap() {
51+
return snap_id++;
52+
}
53+
54+
public int get(int index, int snap_id) {
55+
return Tm[index].floorEntry(snap_id).getValue();
56+
}
57+
}
58+
59+
/*
60+
Runtime
61+
67 ms
62+
Beats
63+
54.5%
64+
Memory
65+
77.6 MB
66+
Beats
67+
57.61%
68+
*/
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//Attempt #1 - 20mins
2+
//Link: https://leetcode.com/problems/summary-ranges/description/
3+
4+
class Solution {
5+
public List<String> summaryRanges(int[] nums) {
6+
ArrayList<String> al=new ArrayList<>();
7+
8+
for(int i=0;i<nums.length;i++){
9+
int start=nums[i];
10+
while(i+1<nums.length && nums[i]+1==nums[i+1])
11+
i++;
12+
13+
if(start!=nums[i]){
14+
al.add(Integer.toString(start)+"->"+Integer.toString(nums[i]));
15+
}
16+
else{
17+
al.add(Integer.toString(start));
18+
}
19+
}
20+
return al;
21+
}
22+
}
23+
24+
/*
25+
Runtime
26+
6 ms
27+
Beats
28+
33.52%
29+
Memory
30+
40.9 MB
31+
Beats
32+
73.61%
33+
*/

Things to Do.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
Things to Do:
22

33
1) Find Optimal way to create subarrays
4-
2) Finish Txt file for Collections + Study
5-
3) Finish Txt File for Arrays
6-
4) Suffix Trees, Quick Select Sort, KMP Algo
7-
5) Quick Sort, Merge Sort and Binary Search - Thoroughly
84
6) implement Linked List
95
7) Prepare Notes for Linked Lists
106
8) Copy List with Random Pointer
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public boolean isBalanced(TreeNode root) {
3+
return getHeight(root) != -1;
4+
}
5+
6+
private int getHeight(TreeNode root) {
7+
if (root == null) {
8+
return 0;
9+
}
10+
int leftHeight = getHeight(root. left);
11+
if (leftHeight == -1) {
12+
return -1;
13+
}
14+
int rightHeight = getHeight(root.right);
15+
if (rightHeight == -1) {
16+
return -1;
17+
}
18+
// The height difference between the left and right subtrees is greater than 1, and return -1 means that it is no longer a balanced tree
19+
if (Math. abs(leftHeight - rightHeight) > 1) {
20+
return -1;
21+
}
22+
return Math.max(leftHeight, rightHeight) + 1;
23+
}
24+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public int diameterOfBinaryTree(TreeNode root) {
3+
4+
// Create an array to hold the diameter of the tree
5+
int diameter[] = new int[1];
6+
7+
// Recursively calculate the height of the tree and update the diameter array
8+
height(root,diameter);
9+
//System.out.println(Arrays.toString(diameter));
10+
11+
// Return the diameter of the tree
12+
return diameter[0];
13+
}
14+
15+
public int height(TreeNode root, int diameter[]){
16+
17+
// Base case: if the root is null, the height is 0
18+
if(root == null){
19+
return 0;
20+
}
21+
22+
// Recursively calculate the height of the left and right subtrees
23+
int left = height(root.left,diameter);
24+
int right = height(root.right,diameter);
25+
26+
// Update the diameter array by taking the maximum diameter that passes through the current node
27+
diameter[0] = Math.max(diameter[0],left + right);
28+
29+
// Return the maximum depth of the current node by adding 1 to the maximum depth of its deepest subtree
30+
return Math.max(left,right)+1;
31+
}
32+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class Solution {
2+
public boolean isSubtree(TreeNode s, TreeNode t) {
3+
if (s == null) return false;
4+
if (isSame(s, t)) return true;
5+
return isSubtree(s.left, t) || isSubtree(s.right, t);
6+
}
7+
8+
private boolean isSame(TreeNode s, TreeNode t) {
9+
if (s == null && t == null) return true;
10+
if (s == null || t == null) return false;
11+
12+
if (s.val != t.val) return false;
13+
14+
return isSame(s.left, t.left) && isSame(s.right, t.right);
15+
}
16+
}

0 commit comments

Comments
 (0)