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

Commit c149c21

Browse files
committed
More problems added
1 parent 72e72e6 commit c149c21

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1226
-63
lines changed
Loading
242 KB
Loading
228 KB
Loading
224 KB
Loading
226 KB
Loading
114 KB
Loading
115 KB
Loading
152 KB
Loading

src/main/images/LRUCache.png

33.4 KB
Loading
202 KB
Loading
Loading
320 KB
Loading

src/main/images/spring_actuator.png

18.5 KB
Loading
149 KB
Loading
86.8 KB
Loading

src/main/images/spring_beans.png

96.1 KB
Loading

src/main/java/com/arrays/InversionCountInArray_MergeSort.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
* <p>
2020
* <p>
2121
* Complexity Analysis:
22-
* Time Complexity: O(n log n), The algorithm used is divide and conquer, So in each level one full array traversal is needed and there are log n levels so the time complexity is O(n log n).
22+
* Time Complexity: O(n log n), The algorithm used is divide and conquer,
23+
* So in each level one full array traversal is needed and there are log n levels so the time complexity is O(n log n).
2324
* Space Compelxity:O(1), No extra space is required.
2425
*/
2526
public class InversionCountInArray_MergeSort {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.arrays;
2+
3+
import java.util.HashSet;
4+
5+
/**
6+
* Given an unsorted array of integers,
7+
* find the length of the longest consecutive elements sequence.
8+
*
9+
* For example, given [100, 4, 200, 1, 3, 2],
10+
* the longest consecutive elements sequence should be [1, 2, 3, 4]. Its length is 4.
11+
*
12+
* Because it requires O(n) complexity,
13+
* we can not solve the problem by sorting the array first. Sorting takes at least O(nlogn) time.
14+
*
15+
* We can use a HashSet to add and remove elements.
16+
* The add, remove and contains methods have constant time complexity O(1).
17+
*/
18+
public class LongestConsecutiveSubsequence {
19+
20+
public static int longestConsecutive(int[] nums) {
21+
22+
/** Add all the elements of array to Set */
23+
HashSet<Integer> set = new HashSet<>();
24+
for (int num : nums)
25+
set.add(num);
26+
27+
int result = 0;
28+
29+
/** Iterate over each element in array */
30+
for (int num : nums) {
31+
int count = 1;
32+
33+
/** calculate previous element to current element to see if present in set */
34+
int down = num - 1;
35+
while (set.contains(down)) {
36+
set.remove(down);
37+
down--;
38+
count++;
39+
}
40+
41+
/** calculate next element to current element to see if present in set */
42+
int up = num + 1;
43+
while (set.contains(up)) {
44+
set.remove(up);
45+
up++;
46+
count++;
47+
}
48+
49+
/** update the result if current counter is bigger than result */
50+
result = Math.max(result, count);
51+
}
52+
53+
return result;
54+
}
55+
56+
public static void main(String args[]) {
57+
int nums[] = {1, 5, 4, 100, 200, 2, 3};
58+
int result = longestConsecutive(nums);
59+
System.out.println("longestConsecutive : " + result);
60+
}
61+
}

src/main/java/com/arrays/MaxSumSubArray_KadaneAlgo_DynamicProgram.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.arrays;
2-
/*
2+
/**
33
44
Dynamic programming solves a problem by dividing it into smaller subproblems. This is very similar to the divide-and-conquer algorithm solving technique.
55
The major difference, however, is that dynamic programming solves a subproblem only once.
@@ -13,13 +13,13 @@
1313
1414
Complexity of finding largest sum subarray in an array is O(N) in time and O(1) in space.
1515
16-
*/
16+
*/
1717

1818
public class MaxSumSubArray_KadaneAlgo_DynamicProgram {
1919

2020
public static void main(String[] args) {
2121

22-
int[] arr = {-1, 3, -5, 4, 6, -1, 2, -7, 3, -3};
22+
int[] arr = {-1, -3, -5, -4, -1, -1, -2, -7, -3, -3};
2323

2424
int[] result = findMaxSumIndex(arr);
2525
System.out.println("start index :" + result[0]);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
image::../../../images/MinimumPlatforms_Greedy.png[]
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.arrays;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Write a program to find the minimum number of platforms needed in a railway station.
7+
* <p>
8+
* The arrival and departure time of several trains are provided.
9+
* Two disparate arrays are given: one with all the arrival times and another with the departure time in 24 hours clock.
10+
* Write a program to find the minimum number of platforms needed in a given railway station.
11+
* <p>
12+
* Solution:
13+
* <p>
14+
* Clue: The minimum number of platforms is nothing but the maximum number of trains that rest in the given railway station
15+
* from the time limit between the arrival of the first train to the departure of the last train.
16+
* Input: arr[] = {9:00, 9:40, 9:50, 11:00, 15:00, 18:00}
17+
* dep[] = {9:10, 12:00, 11:20, 11:30, 19:00, 20:00}
18+
* Output: 3
19+
* There are at-most three trains at a time (time between 9:40 to 11:00)
20+
* <p>
21+
* Sort the timing values given in both the arrays and later check the count of trains at every given time of arrival and departure.
22+
* The maximum number of trains in the process is taken as the output.
23+
* <p>
24+
* arr[] = {9:00, 9:40, 9:50, 11:00, 15:00, 18:00}
25+
* dep[] = {9:10, 12:00, 11:20, 11:30, 19:00, 20:00}
26+
* <p>
27+
* Time complexity is O(n + nlog n). n = traverse the array of n elements + nlogn (for sorting the array)
28+
* Space Complexity: O(1).
29+
* As no extra space is required.
30+
* <p>
31+
* dynamic programming is used to conserve memory.
32+
*/
33+
public class MinimumPlatforms_Greedy {
34+
35+
// Returns minimum number of platforms required
36+
static int findPlatform(int arr[], int dep[], int n) {
37+
38+
/**important*/
39+
// Sort arrival and departure arrays
40+
Arrays.sort(arr);
41+
Arrays.sort(dep);
42+
43+
// max_platforms_so_far indicates number of platforms needed at a time
44+
int max_platforms_so_far = 1, needed_platforms = 1;
45+
int i = 1, j = 0;
46+
47+
// Similar to merge in merge sort to process all events in sorted order
48+
while (i < n && j < n) { /** end the loop when any one iterator completes any one array.*/
49+
// If next event in sorted order is arrival,
50+
// increment count of platforms needed
51+
if (arr[i] <= dep[j]) /**means one more train can before any train could depart so increment the max_platfroms */ {
52+
max_platforms_so_far++;
53+
i++;
54+
55+
// Update result if needed
56+
if (max_platforms_so_far > needed_platforms)
57+
needed_platforms = max_platforms_so_far;
58+
}
59+
60+
// Else decrement count of platforms needed
61+
else {
62+
max_platforms_so_far--;
63+
j++;
64+
}
65+
}
66+
67+
return needed_platforms;
68+
}
69+
70+
// Driver program to test methods of graph class
71+
public static void main(String[] args) {
72+
int arr[] = {900, 940, 950, 1100, 1500, 1800};
73+
int dep[] = {910, 1200, 1120, 1130, 1900, 2000};
74+
int n = arr.length;
75+
System.out.println("Minimum Number of Platforms Required = "
76+
+ findPlatform(arr, dep, n));
77+
}
78+
}
79+

src/main/java/com/arrays/WildCardMatching.java

Lines changed: 53 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,72 @@
11
package com.arrays;
22

33
/**
4-
5-
'?' Matches any single character.
6-
'*' Matches any sequence of characters (including the empty sequence).
7-
8-
Input:
9-
s = "cb"
10-
p = "?a"
11-
Output: false
12-
Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'.
13-
14-
Time complexity of above solution is O(m x n). Auxiliary space used is also O(m x n).
15-
where m = length of pattern
16-
n = length of String
17-
4+
* '?' Matches any single character.
5+
* '*' Matches any sequence of characters (including the empty sequence).
6+
* <p>
7+
* Input:
8+
* s = "cb"
9+
* p = "?a"
10+
* Output: false
11+
* Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'.
12+
* <p>
13+
* Time complexity of above solution is O(m x n). Auxiliary space used is also O(m x n).
14+
* where m = length of pattern
15+
* n = length of String
1816
*/
1917

2018
public class WildCardMatching {
2119

22-
public boolean isMatch(String s, String p) {
23-
char[] str = s.toCharArray();
24-
char[] pattern = p.toCharArray();
25-
26-
//replace multiple * with one *
27-
//e.g a**b***c --> a*b*c
28-
int writeIndex = 0;
29-
boolean isFirst = true;
30-
for (int i = 0; i < pattern.length; i++) {
31-
if (pattern[i] == '*') {
32-
if (isFirst) {
33-
pattern[writeIndex++] = pattern[i];
34-
isFirst = false;
35-
}
36-
} else {
20+
public boolean isMatch(String s, String p) {
21+
char[] str = s.toCharArray();
22+
char[] pattern = p.toCharArray();
23+
24+
//replace multiple * with one *
25+
//e.g a**b***c --> a*b*c
26+
int writeIndex = 0;
27+
boolean isFirst = true;
28+
for (int i = 0; i < pattern.length; i++) {
29+
if (pattern[i] == '*') {
30+
if (isFirst) {
3731
pattern[writeIndex++] = pattern[i];
38-
isFirst = true;
32+
isFirst = false;
3933
}
34+
} else {
35+
pattern[writeIndex++] = pattern[i];
36+
isFirst = true;
4037
}
38+
}
4139

42-
boolean T[][] = new boolean[str.length + 1][pattern.length + 1];
43-
44-
if (writeIndex > 0 && pattern[0] == '*') {
45-
T[0][1] = true;
46-
}
47-
48-
// base case
49-
T[0][0] = true;
40+
boolean T[][] = new boolean[str.length + 1][pattern.length + 1];
5041

51-
/**
52-
* Rules
53-
* 1) if the value at boolean_array[i][j] is a matching character or a “?”.
54-
* boolean_array[i][j] = boolean_array[i-1][j-1] (means diagonal)
55-
*
56-
* 2) if the value at at boolean_array[i][j] is “*”.
57-
* boolean_array[i][j] = boolean_array[i][j-1] || boolean_array[i-1][j] (means one one left or one on top whichever is true)
58-
*/
42+
if (writeIndex > 0 && pattern[0] == '*') {
43+
T[0][1] = true;
44+
}
5945

60-
for (int i = 1; i <= str.length; i++)
61-
{
62-
for (int j = 1; j <= pattern.length; j++)
63-
if (pattern[j - 1] == '?' || str[i - 1] == pattern[j - 1]) {
64-
T[i][j] = T[i - 1][j - 1]; //diagonal
65-
} else if (pattern[j - 1] == '*') {
66-
T[i][j] = T[i - 1][j] || T[i][j - 1];
67-
}
46+
// base case
47+
T[0][0] = true;
48+
49+
/**
50+
* Rules
51+
* 1) if the value at boolean_array[i][j] is a matching character or a “?”.
52+
* boolean_array[i][j] = boolean_array[i-1][j-1] (means diagonal)
53+
*
54+
* 2) if the value at at boolean_array[i][j] is “*”.
55+
* boolean_array[i][j] = boolean_array[i][j-1] || boolean_array[i-1][j] (means one one left or one on top whichever is true)
56+
*/
57+
58+
for (int i = 1; i <= str.length; i++) {
59+
for (int j = 1; j <= pattern.length; j++)
60+
if (pattern[j - 1] == '?' || str[i - 1] == pattern[j - 1]) {
61+
T[i][j] = T[i - 1][j - 1]; //diagonal
62+
} else if (pattern[j - 1] == '*') {
63+
T[i][j] = T[i - 1][j] || T[i][j - 1];
6864
}
69-
70-
return T[str.length][pattern.length];
7165
}
7266

67+
return T[str.length][pattern.length];
68+
}
69+
7370

7471
public static void main(String args[]) {
7572
WildCardMatching wcm = new WildCardMatching();
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.linkedlist;
2+
3+
import com.linkedlist.model.Node;
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
/**
9+
* Write a program to find the node at which the intersection of two singly linked lists begins.
10+
*
11+
* For example, the following two linked lists:
12+
*
13+
* Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
14+
* Output: Reference of the node with value = 8
15+
* Input Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect).
16+
* From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,0,1,8,4,5].
17+
* There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
18+
*
19+
* Notes
20+
* If the two linked lists have no intersection at all, return null.
21+
* The linked lists must retain their original structure after the function returns.
22+
* You may assume there are no cycles anywhere in the entire linked structure.
23+
* Your code should preferably run in O(n) time and use only O(1) memory.
24+
*
25+
*/
26+
public class IntersectionOfTwoLinkedLists {
27+
28+
//Approach 2: Hash Table (10ms)
29+
public Node getIntersectionNode(Node headA, Node headB) {
30+
Set<Node> nodes = new HashSet<>();
31+
Node pa = headA;
32+
while (pa != null) {
33+
nodes.add(pa);
34+
pa = pa.next;
35+
}
36+
if (nodes.isEmpty()) {
37+
return null;
38+
}
39+
Node pb = headB;
40+
while (pb != null) {
41+
if (nodes.contains(pb)) {
42+
return pb;
43+
}
44+
pb = pb.next;
45+
}
46+
return null;
47+
}
48+
49+
//Approach 3: Two Pointers (1ms)
50+
51+
public Node getIntersectionNode_1(Node headA, Node headB) {
52+
Node pa = headA, pb = headB;
53+
while (pa != pb) {
54+
pa = (pa != null) ? pa.next : headB;
55+
pb = (pb != null) ? pb.next : headA;
56+
}
57+
return pa;
58+
}
59+
60+
61+
}
62+
63+
64+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
image::../../../images/LRUCache.png[]

0 commit comments

Comments
 (0)