-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSome Concepts.txt
145 lines (126 loc) · 5.24 KB
/
Some Concepts.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
---------------------------------Mathematical concepts-----------------------
1. We have to add digits until it becomes only a single digit => 38 -> 11 -> '2'
In Digital Root Formula See Wikipedia:
num == 0 => sum = 0
num % 9 == 9 => sum = 9
num % 9 != 9 =>sum = num % 9
shortcut for that -> return 1 + (n-1)%9;
----------------------------------------->
2. Hashing
-> Hashing works in a way like make a hashtable, and suppose if we have some key firstly we convert that key in hash using a hash function which will give the address of the given key in the hashtable for constant access. same happens in case of insert,remove,search.
To Reduce Collisions in hashing
-> we can use a prime no table size like Hash value of a string/integer = (hash value)%(prime no table size).
Problems with String hash functions
a) Anagrams
-> cat, act
b) Non uniform Distribution
-> cat,sad,rat,bat <-this will only take small region of table, so no uniform distribution.
-> Can be resolved by making Stronger hash function h = pow(d,m-1) where m is no of letters in the character.
-> If again collision happens then the same key will have multiple values in the form linkedList
-> But make sure the size of linkedlist should be min as it does not exceed loading factor of hashtables.
-> If it exceeds then there will be rehashing by double the size of hashtable.
Design HashMap->
class MyHashMap {
final ListNode[]nodes;
public MyHashMap() {
nodes = new ListNode[10000];
}
public void put(int key, int value) {
int i = idx(key);
if(nodes[i]==null){
nodes[i]=new ListNode(-1,-1);
}
ListNode prev = find(nodes[i],key);
if(prev.next==null){
prev.next = new ListNode(key,value);
}else{
prev.next.val = value; //updating key's value
}
}
public int get(int key) {
int i = idx(key);
if(nodes[i]==null){
return -1;
}else{
ListNode prev = find(nodes[i],key);
return (prev.next==null)?-1:prev.next.val;
}
}
public void remove(int key) {
int i = idx(key);
if(nodes[i]==null){
return;
}else{
ListNode prev = find(nodes[i],key);
if(prev.next!=null){
prev.next = prev.next.next;
}
}
}
public int idx(int key){return Integer.hashCode(key) % nodes.length;}
public ListNode find(ListNode bucket,int key){
ListNode node = bucket, prev = null;
for(;node!=null && node.key!=key; node=node.next){
prev = node;
}
return prev;
}
public class ListNode{
int key;
int val;
ListNode next;
public ListNode(int k,int v){
key=k;
val=v;
}
}
}
----------------------------------------------------------------------------------->
3. Shortcut
-> to convert list into array => res.stream().mapToInt(i->i).toArray();
---------------------------------------------------------------------------------->
4. Integer Break (LC-343)
-> Divide a number into 2 or more parts such that its sum makes the number itself, and product will be maximum.
-> Dry run to understand the pattern.
-> Mathematical proof -> https://leetcode.com/problems/integer-break/discuss/80689/A-simple-explanation-of-the-math-part-and-a-O(n)-solution
public int integerBreak(int n) {
if(n == 2)
return 1;
else if(n == 3)
return 2;
else if(n%3 == 0)
return (int)Math.pow(3, n/3);
else if(n%3 == 1)
return 2 * 2 * (int) Math.pow(3, (n - 4) / 3);
else
return 2 * (int) Math.pow(3, (n-2)/3);
}
---------------------------------------------------------------------------------->
5. Opposite of split is join in String.
String []s = text.toLowerCase().split(" ");
Arrays.sort(s, (a,b) -> a.length() - b.length());
String ans = String.join(" ", s);
---------------------------------------------------------------------------------->
6. Find number of times a string occurs as a subsequence in given string
-> Use LCS for this
// If last characters are same
// Recur for remaining strings by
// 1. considering last characters of
// both strings
// 2. ignoring last character of
// first string
if (a.charAt(m - 1) == b.charAt(n - 1))
return count(a, b, m - 1, n - 1) +
count(a, b, m - 1, n);
else
// If last characters are different,
// ignore last char of first string
// and recur for remaining string
return count(a, b, m - 1, n);
---------------------------------------------------------------------------------->
---------------------------------------------------------------------------------->
---------------------------------------------------------------------------------->
---------------------------------------------------------------------------------->
---------------------------------------------------------------------------------->
---------------------------------------------------------------------------------->
---------------------------------------------------------------------------------->