Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 26e986f

Browse files
abhiabhiabhiabhi
abhiabhi
authored and
abhiabhi
committed
design material
1 parent 085e857 commit 26e986f

17 files changed

+1417
-1
lines changed

src/main/images/class.png

4.66 KB
Loading

src/main/images/classdia.png

92.9 KB
Loading

src/main/images/solidPrinciple.png

58.6 KB
Loading

src/main/images/uml.png

24.7 KB
Loading
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
he bitwise method evaluates the binary representation of the values of the two inputs. For each bit in the binary representation, a Boolean Exclusive Or is performed. In the Boolean evaluation, if one of the input values for the bit is true and the other is false, the output is one for the bit; if both input values are true or if both are false, the output is zero for the bit. When one or both input values are NoData, the bitwise expression outputs NoData.
2+
For example, the input values for a particular cell location on two rasters are 5 and 3. The Boolean Exclusive Or is performed, producing a new binary value. When the value of this number is printed as a decimal integer, its base10 value is assigned to the output. The example below is of an 8-bit integer.
3+
4+
Value Binary representation
5+
Input 1 5 00000101
6+
Input 2 3 00000011
7+
(Bitwise XOr)
8+
Output 6 00000110
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.arrays;
2+
3+
import java.time.Duration;
4+
import java.time.Instant;
5+
6+
/**
7+
* Input: arr[] = {1, 2, 4, 6, 3, 7, 8}
8+
* Output: 5
9+
* Explanation: The missing number from 1 to 8 is 5
10+
* <p>
11+
* Input: arr[] = {1, 2, 3, 5}
12+
* Output: 4
13+
* Explanation: The missing number from 1 to 5 is 4
14+
* <p>
15+
* Approach:
16+
* XOR has certain properties
17+
* Assume a1 ^ a2 ^ a3 ^ …^ an = a and a1 ^ a2 ^ a3 ^ …^ an-1 = b
18+
* Then a ^ b = an
19+
* Using this property, the missing element can be found. Calculate XOR of all the natural number from 1 to n and store it as a.
20+
* Now calculate XOR of all the elements of the array and store it as b.
21+
* The missing number will be a ^ b.
22+
* ^ is XOR operator.
23+
* <p>
24+
* Algorithm:
25+
* Create two variables a = 0 and b = 0
26+
* Run a loop from 1 to n with i as counter.
27+
* For every index update a as a = a ^ i
28+
* Now traverse the array from start to end.
29+
* For every index update b as b = b ^ array[i]
30+
* Print the missing number as a ^ b.
31+
* <p>
32+
* Compelxity Analysis:
33+
* Time Complexity: O(n).
34+
* Only one traversal of array is needed.
35+
* Space Complexity: O(1).
36+
* No extra space is needed
37+
* <p>
38+
* Bitwise operations are incredibly simple and thus usually faster than arithmetic operations.
39+
*/
40+
41+
public class FindMissingNumber {
42+
43+
public static long missingNumber(int[] nums, int n) {
44+
System.out.println("time start");
45+
46+
long naturalNumberXOR = 1;
47+
long arrayXOR = nums[0];
48+
49+
for (int i = 2; i <= n + 1; i++) {
50+
naturalNumberXOR = naturalNumberXOR ^ i;
51+
}
52+
53+
for (int i = 1; i < nums.length; i++) {
54+
arrayXOR = arrayXOR ^ nums[i];
55+
}
56+
System.out.println("time end");
57+
return naturalNumberXOR ^ arrayXOR;
58+
59+
60+
}
61+
62+
63+
public static long missingNumberUsingSUm(int[] nums, int n) {
64+
System.out.println("time start");
65+
long naturalNumberSum = 1;
66+
long arraySum = nums[0];
67+
68+
naturalNumberSum = (n * (n + 1)) / 2; //Summation formula GUass's formula
69+
70+
71+
for (int i = 1; i < nums.length; i++) {
72+
arraySum = arraySum + nums[i];
73+
}
74+
System.out.println("time end");
75+
return naturalNumberSum - arraySum;
76+
77+
}
78+
79+
public static void main(String args[]) {
80+
int[] nums = {1, 2, 3, 4, 5, 7, 8, 9, 10};
81+
System.out.println("Missing Number:" + missingNumberUsingSUm(nums, 10));
82+
}
83+
}

src/main/java/com/arrays/MinimumPlatforms_Greedy.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ public class MinimumPlatforms_Greedy {
3535
// Returns minimum number of platforms required
3636
static int findPlatform(int arr[], int dep[], int n) {
3737

38+
//Edge case for input.
39+
if (arr.length==0){
40+
return 0;
41+
}
42+
if(dep.length==0){
43+
return arr.length;
44+
}
45+
3846
/**important*/
3947
// Sort arrival and departure arrays
4048
Arrays.sort(arr);
@@ -48,7 +56,7 @@ static int findPlatform(int arr[], int dep[], int n) {
4856
while (i < n && j < n) { /** end the loop when any one iterator completes any one array.*/
4957
// If next event in sorted order is arrival,
5058
// increment count of platforms needed
51-
if (arr[i] <= dep[j]) /**means one more train came before any train could depart so increment the max_platfroms */ {
59+
if (arr[i] <= dep[j]) { /**means one more train came before any train could depart so increment the max_platfroms */
5260
max_platforms_so_far++;
5361
i++;
5462

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.design;
2+
3+
import com.linkedlist.model.DoublyLLNode;
4+
5+
import java.util.HashMap;
6+
7+
/**
8+
* Design and implement a data structure for Least Recently Used (LRU) cache, which supports get and put.
9+
* The key to solve this problem is using a double linked list which enables us to quickly move nodes.
10+
*
11+
* The LRU cache is a hash table of keys and double linked nodes.
12+
* The hash table makes the time of get() to be O(1). The list of double linked nodes make the nodes adding/removal operations O(1).
13+
*
14+
* By analyzing the get and put, we can summarize there are 2 basic operations:
15+
* 1) removeNode(Node t), 2) offerNode(Node t).
16+
*
17+
*/
18+
class LRUCache {
19+
DoublyLLNode head;
20+
DoublyLLNode tail;
21+
HashMap<Integer, DoublyLLNode> map = null;
22+
int cap = 0;
23+
24+
public LRUCache(int capacity) {
25+
this.cap = capacity;
26+
this.map = new HashMap<>();
27+
}
28+
29+
public int get(int key) {
30+
if(map.get(key)==null){
31+
return -1;
32+
}
33+
34+
//move to tail
35+
DoublyLLNode t = map.get(key);
36+
37+
removeDoublyLLNode(t);
38+
offerDoublyLLNode(t);
39+
40+
return t.value;
41+
}
42+
43+
public void put(int key, int value) {
44+
if(map.containsKey(key)){
45+
DoublyLLNode t = map.get(key);
46+
t.value = value;
47+
48+
//move to tail
49+
removeDoublyLLNode(t);
50+
offerDoublyLLNode(t);
51+
}else{
52+
if(map.size()>=cap){
53+
//delete head
54+
map.remove(head.key);
55+
removeDoublyLLNode(head);
56+
}
57+
58+
//add to tail
59+
DoublyLLNode DoublyLLNode = new DoublyLLNode(key, value);
60+
offerDoublyLLNode(DoublyLLNode);
61+
map.put(key, DoublyLLNode);
62+
}
63+
}
64+
65+
private void removeDoublyLLNode(DoublyLLNode n){
66+
if(n.prev!=null){
67+
n.prev.next = n.next;
68+
}else{
69+
head = n.next;
70+
}
71+
72+
if(n.next!=null){
73+
n.next.prev = n.prev;
74+
}else{
75+
tail = n.prev;
76+
}
77+
}
78+
79+
private void offerDoublyLLNode(DoublyLLNode n){
80+
if(tail!=null){
81+
tail.next = n;
82+
}
83+
84+
n.prev = tail;
85+
n.next = null;
86+
tail = n;
87+
88+
if(head == null){
89+
head = tail;
90+
}
91+
}
92+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.design;
2+
3+
import javax.jms.Message;
4+
import javax.jms.MessageListener;
5+
import javax.jms.TextMessage;
6+
7+
/**
8+
* This design uses Strategy Pattern
9+
*
10+
* Strategy pattern and the open closed principle
11+
* According to the strategy pattern, the behaviour of a class should be encapsulated
12+
* using interfaces and should not be inherited.
13+
* This is compatible with the open closed principle,
14+
*/
15+
16+
public class LowLevelDesign {
17+
18+
MessageProcessor processor;
19+
20+
class MessageConsumer implements MessageListener{
21+
22+
@JMSListener
23+
public void onMessage(Message message){
24+
if (message instanceof TextMessage){
25+
processor = new MessageProcessor();
26+
MessageParser mp = new swiftParser();
27+
processor.setParser(mp);
28+
processor.process(message);
29+
}
30+
if (message instanceof BytesMessage){
31+
32+
}
33+
34+
35+
}
36+
37+
}
38+
39+
class MessageProcessor {
40+
41+
MessageParser parser;
42+
43+
public void setParser(MessageParser p){
44+
this.parser = p;
45+
}
46+
47+
public void process(Message message){
48+
parser.parse(message);
49+
}
50+
51+
}
52+
53+
interface MessageParser{
54+
void parse(Message message);
55+
}
56+
57+
class SwiftParser implements MessageParser{
58+
59+
@Override
60+
public void parse(Message message) {
61+
62+
}
63+
}
64+
class JsonParser implements MessageParser{
65+
66+
@Override
67+
public void parse(Message message) {
68+
69+
}
70+
}
71+
class XMLParser implements MessageParser{
72+
73+
@Override
74+
public void parse(Message message) {
75+
76+
}
77+
}
78+
79+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.design;
2+
3+
// Java code skeleton to design an online hotel
4+
// booking system.
5+
6+
enum RoomStatus {
7+
EMPTY,
8+
NOT_EMPTY;
9+
}
10+
11+
enum RoomType {
12+
SINGLE,
13+
DOUBLE,
14+
TRIPLE;
15+
}
16+
17+
enum PaymentStatus {
18+
PAID,
19+
UNPAID;
20+
}
21+
22+
public enum Facility {
23+
LIFT;
24+
POWER_BACKUP;
25+
HOT_WATERR;
26+
BREAKFAST_FREE;
27+
SWIMMING_POOL;
28+
}
29+
30+
class User {
31+
32+
int userId;
33+
String name;
34+
Date dateOfBirth;
35+
String mobNo;
36+
String emailId;
37+
String sex;
38+
}
39+
40+
// For the room in any hotel
41+
class Room {
42+
43+
int roomId; // roomNo
44+
int hotelId;
45+
RoomType roomType;
46+
RoomStatus roomStatus;
47+
}
48+
49+
class Hotel {
50+
51+
int hotelId;
52+
String hotelName;
53+
Adress adress;
54+
55+
// hotel contains the list of rooms
56+
List<Room> rooms;
57+
float rating;
58+
Facilities facilities;
59+
}
60+
61+
// a new booking is created for each booking
62+
// done by any user
63+
class Booking {
64+
int bookingId;
65+
int userId;
66+
int hotelId;
67+
68+
// We are assuming that in a single
69+
// booking we can book only the rooms
70+
// of a single hotel
71+
List<Rooms> bookedRooms;
72+
73+
int amount;
74+
PaymentStatus status_of_payment;
75+
Date bookingTime;
76+
Duration duration;
77+
}
78+
79+
class Address {
80+
81+
String city;
82+
String pinCode;
83+
String state;
84+
String streetNo;
85+
String landmark;
86+
}
87+
88+
class Duration {
89+
90+
Date from;
91+
Date to;
92+
93+
}
94+
95+
class Facilities {
96+
97+
List<Facility> facilitiesList;
98+
}
99+

0 commit comments

Comments
 (0)