Skip to content

Commit b3dfd0e

Browse files
committed
splay tree started
1 parent bcf2a1e commit b3dfd0e

3 files changed

Lines changed: 82 additions & 3 deletions

File tree

‎src/algo/dp/MaxWeightTransformation.java‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public int maxWeightTransformationDPSol(char str1[], int i, int size, int lookup
5656
// weight gained is 3 after transformation
5757
if (str1[i] == str1[i + 1]) {
5858
res = Math.max(res, 3 + maxWeightTransformationDPSol(str1, i + 2, size, lookup));
59-
} else if (str1[i] != str1[i + 1]) { // if consecutive elements are different weight gained is 4
59+
} else if (str1[i] != str1[i
60+
+ 1]) { // if consecutive elements are different weight gained is 4
6061
res = Math.max(res, 4 + maxWeightTransformationDPSol(str1, i + 2, size, lookup));
6162
}
6263
}
@@ -86,14 +87,17 @@ public int maxWeightTransformationNoDPSol(char str1[], int i, int size) {
8687

8788
// is pair is made
8889
if (i + 1 <= size) {
89-
if (str1[i] == str1[i + 1]) {// if consecutive elements are same, weight gained is 3 after transformation
90+
if (str1[i] == str1[i
91+
+ 1]) {// if consecutive elements are same, weight gained is 3 after transformation
9092
res = Math.max(res, 3 + maxWeightTransformationNoDPSol(str1, i + 2, size));
91-
} else if (str1[i] != str1[i + 1]) {// if consecutive elements are different, weight gained is 4
93+
} else if (str1[i] != str1[i
94+
+ 1]) {// if consecutive elements are different, weight gained is 4
9295
res = Math.max(res, 4 + maxWeightTransformationNoDPSol(str1, i + 2, size));
9396
}
9497
}
9598

9699
return res;
97100
}
101+
}
98102

99103

‎src/ds/SplayTree.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
/**
44
* Created by sherxon on 6/30/17.
55
*/
6+
7+
/**
8+
* This is implementation os splay tree. Running time is amortized O(logN) for lookup, insertion,
9+
* deletion. Splay trees splay the tree after each insertion and lookup operations so that accessed
10+
* nodes get closer to root. Garbage Collection and Cache are best use cases. No need to store additional
11+
* pointers with each node. There are three cases to consider when splaying:
12+
* Zig-Zig=> parent node (p) and X node are both left or right children.
13+
* Zig-Zag =>p is not root and x is the right child and p is the left child, or vice versa
14+
* Zig => when there is no grandparent of x. (parent is root )
15+
*/
616
public class SplayTree {
717

818
}

‎src/problems/medium/LogSystem.java‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package problems.medium;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.TreeMap;
6+
7+
/**
8+
* Created by sherxon on 7/1/17.
9+
*/
10+
public class LogSystem {
11+
12+
13+
TreeMap<Long, Integer> logs = new TreeMap<>();
14+
15+
public LogSystem() {
16+
17+
}
18+
19+
public static void main(String[] args) {
20+
LogSystem log = new LogSystem();
21+
System.out.println(log.getDate("2004:09:23:19:31:46", "Year", false));
22+
System.out.println(log.getDate("2003:12:12:20:30:51", "Year", false));
23+
24+
}
25+
26+
public void put(int id, String timestamp) {
27+
logs.put(getDate(timestamp, "Second", false), id);
28+
}
29+
30+
public List<Integer> retrieve(String s, String e, String gra) {
31+
32+
long fromDay = getDate(s, gra, true);
33+
long toDay = getDate(e, gra, false);
34+
return new ArrayList<>(logs.subMap(fromDay, true, toDay, true).values());
35+
36+
}
37+
38+
39+
private long getDate(String s, String gra, boolean b) {
40+
int year = 0;
41+
int month = b ? 1 : 11;
42+
int day = b ? 1 : 31;
43+
int hour = b ? 0 : 23;
44+
int min = b ? 0 : 59;
45+
int sec = b ? 0 : 59;
46+
String to[] = s.split(":");
47+
switch (gra) {
48+
case "Second":
49+
sec = Integer.parseInt(to[5]);
50+
case "Minute":
51+
min = Integer.parseInt(to[4]);
52+
case "Hour":
53+
hour = Integer.parseInt(to[3]);
54+
case "Day":
55+
day = Integer.parseInt(to[2]);
56+
case "Month":
57+
month = Integer.parseInt(to[1]) - 1;
58+
case "Year":
59+
year = Integer.parseInt(to[0]);
60+
}
61+
62+
return year * 100000 + month * 10000 + day * 1000 + hour * 100 + min * 10 + sec;
63+
}
64+
}
65+

0 commit comments

Comments
 (0)