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

Commit cbdd771

Browse files
committed
added Programs to all categories
1 parent 77df86c commit cbdd771

24 files changed

+790
-2
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.arrays;
2+
3+
/**
4+
* An array is called Bitonic if it is comprises of an increasing sequence of integers followed immediately
5+
* by a decreasing sequence of integers.
6+
* Given a bitonic array A of N distinct integers. Find a element X in it.
7+
*/
8+
public class SearchInRotatedSortedArray_BitonicArray {
9+
10+
public static int search(int[] nums, int target) {
11+
int left = 0;
12+
int right = nums.length - 1;
13+
14+
while (left <= right) {
15+
16+
int mid = left + (right - left) / 2;
17+
if (target == nums[mid])
18+
return mid;
19+
20+
if (nums[left] <= nums[mid]) {
21+
if (nums[left] <= target && target < nums[mid]) {
22+
right = mid - 1;
23+
} else {
24+
left = mid + 1;
25+
}
26+
}
27+
28+
if (nums[mid] <= nums[right]) {
29+
if (nums[mid] < target && target <= nums[right]) {
30+
left = mid + 1;
31+
} else {
32+
right = mid - 1;
33+
}
34+
}
35+
}
36+
37+
return -1;
38+
}
39+
40+
public static void main(String args[]) {
41+
int[] arr = {4, 5, 6, 7, 0, 1, 2};
42+
System.out.println("Element found at index : " + search(arr, 6));
43+
}
44+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//package com.graph;
2+
//
3+
//public class DijkstraShortestPath {
4+
//
5+
//
6+
// public static void main(String args[]){
7+
// Graph<Integer> graph = new Graph<>(false);
8+
// /*graph.addEdge(0, 1, 4);
9+
// graph.addEdge(1, 2, 8);
10+
// graph.addEdge(2, 3, 7);
11+
// graph.addEdge(3, 4, 9);
12+
// graph.addEdge(4, 5, 10);
13+
// graph.addEdge(2, 5, 4);
14+
// graph.addEdge(1, 7, 11);
15+
// graph.addEdge(0, 7, 8);
16+
// graph.addEdge(2, 8, 2);
17+
// graph.addEdge(3, 5, 14);
18+
// graph.addEdge(5, 6, 2);
19+
// graph.addEdge(6, 8, 6);
20+
// graph.addEdge(6, 7, 1);
21+
// graph.addEdge(7, 8, 7);*/
22+
//
23+
// graph.addEdge(1, 2, 5);
24+
// graph.addEdge(2, 3, 2);
25+
// graph.addEdge(1, 4, 9);
26+
// graph.addEdge(1, 5, 3);
27+
// graph.addEdge(5, 6, 2);
28+
// graph.addEdge(6, 4, 2);
29+
// graph.addEdge(3, 4, 3);
30+
//
31+
// DijkstraShortestPath dsp = new DijkstraShortestPath();
32+
// Vertex<Integer> sourceVertex = graph.getVertex(1);
33+
// Map<Vertex<Integer>,Integer> distance = dsp.shortestPath(graph, sourceVertex);
34+
// System.out.print(distance);
35+
// }
36+
//}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.heaps;
2+
3+
import java.util.Collections;
4+
import java.util.Comparator;
5+
import java.util.PriorityQueue;
6+
7+
/**
8+
* Median is the middle value in an ordered integer list.
9+
* If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.
10+
* <p>
11+
* First of all, it seems that the best time complexity we can get for this problem is O(log(n)) of add() and O(1) of getMedian().
12+
* This data structure seems highly likely to be a tree.
13+
* O(log(n)) of add() and O(1) of getMedian()
14+
*/
15+
class FindMedianFromDataStream {
16+
17+
PriorityQueue<Integer> minHeap = null;
18+
PriorityQueue<Integer> maxHeap = null;
19+
20+
/**
21+
* initialize your data structure here.
22+
*/
23+
public FindMedianFromDataStream() {
24+
minHeap = new PriorityQueue<>();
25+
maxHeap = new PriorityQueue<>(Comparator.reverseOrder());
26+
}
27+
28+
public void addNum(int num) {
29+
minHeap.offer(num); /** Step 1- Each element is first added to minHeap First */
30+
maxHeap.offer(minHeap.poll()); /** Step 2- Minimum element is poped put and offered to MaxHeap .
31+
This assures all the elements in minHeap are greater than maxHeap */
32+
33+
if (minHeap.size() < maxHeap.size()) { /** Step 3- The two heaps need to be laod balanced */
34+
minHeap.offer(maxHeap.poll());
35+
}
36+
}
37+
38+
public double findMedian() {
39+
if (minHeap.size() > maxHeap.size()) { /** Odd num of total elements */
40+
return minHeap.peek();
41+
} else {
42+
return (minHeap.peek() + maxHeap.peek()) / 2.0; /** Even num of total elements */
43+
}
44+
}
45+
46+
public static void main(String[] args) {
47+
FindMedianFromDataStream stream = new FindMedianFromDataStream();
48+
49+
stream.addNum(2);
50+
stream.addNum(5);
51+
stream.addNum(8);
52+
stream.addNum(18);
53+
stream.addNum(20);
54+
stream.addNum(28);
55+
56+
System.out.println("Median " + stream.findMedian());
57+
}
58+
}
59+
Loading
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.linkedlist;
2+
3+
import com.linkedlist.model.Node;
4+
5+
/**
6+
* For Reversing a linked list we update
7+
* the pointer of each node to point to previous node instead of next node
8+
*/
9+
public class ReverseLinkedList {
10+
11+
public static void main(String args[]) {
12+
Node targetList = new Node(4);
13+
targetList.setNext(new Node(8));
14+
targetList.getNext().setNext(new Node(10));
15+
targetList.getNext().getNext().setNext(new Node(12));
16+
17+
Node headOfReverseList = reverseLinkedList(targetList);
18+
19+
while (headOfReverseList != null) {
20+
System.out.println(headOfReverseList.getData());
21+
headOfReverseList = headOfReverseList.getNext();
22+
}
23+
24+
}
25+
26+
private static Node reverseLinkedList(Node head) {
27+
/** Initialize 3 pointers */
28+
Node previous = null;
29+
Node current = head;
30+
Node next = null;
31+
32+
while (current != null) {
33+
34+
next = current.getNext(); /**save next */
35+
current.setNext(previous); /** reverse */
36+
37+
/** Advance prev and current */
38+
previous = current;
39+
current = next;
40+
}
41+
42+
return previous; /** return ne head at the end */
43+
44+
}
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.stack;
2+
3+
import java.util.HashMap;
4+
import java.util.Stack;
5+
6+
/**
7+
* Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
8+
* <p>
9+
* The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
10+
* <p>
11+
* Analysis
12+
* <p>
13+
* A typical problem which can be solved by using a stack data structure.
14+
*/
15+
public class ParenthesisChecker {
16+
17+
public static boolean isValid(String s) {
18+
HashMap<Character, Character> map = new HashMap<>();
19+
map.put('(', ')');
20+
map.put('[', ']');
21+
map.put('{', '}');
22+
23+
Stack<Character> stack = new Stack<>();
24+
25+
for (int i = 0; i < s.length(); i++) {
26+
char curr = s.charAt(i);
27+
28+
if (map.keySet().contains(curr)) {
29+
stack.push(curr);
30+
} else if (map.values().contains(curr)) {
31+
if (!stack.empty() && map.get(stack.peek()) == curr) {
32+
stack.pop();
33+
} else {
34+
return false;
35+
}
36+
}
37+
}
38+
return stack.empty();
39+
}
40+
41+
public static void main (String args[]){
42+
String input = "[()]{}{[()()]()}";
43+
System.out.println("isValid : "+isValid(input));
44+
}
45+
}
Loading
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.string;
2+
3+
4+
/**
5+
* https://www.youtube.com/watch?v=b6AGUjqIPsA
6+
*
7+
*/
8+
public class EditDistanceBetween2Strings_DynamicProgramming {
9+
10+
public static void main (String args[]){
11+
String from = "YELLOW";
12+
String to = "HELLO";
13+
System.out.println("MinEditDistance:" +editDist(from,to,from.length(),to.length()));
14+
}
15+
16+
static int editDist(String from, String to, int m, int n)
17+
{
18+
// If first string is empty, the only option is to
19+
// insert all characters of second string into first
20+
if (m == 0)
21+
return n;
22+
23+
// If second string is empty, the only option is to
24+
// remove all characters of first string
25+
if (n == 0)
26+
return m;
27+
28+
// If last characters of two strings are same, nothing
29+
// much to do. Ignore last characters and get count for
30+
// remaining strings.
31+
if (from.charAt(m - 1) == to.charAt(n - 1))
32+
return editDist(from, to, m - 1, n - 1); /** diagonal - blind copy*/
33+
34+
// If last characters are not same, consider all three
35+
// operations on last character of first string, recursively
36+
// compute minimum cost for all three operations and take
37+
// minimum of three values.
38+
39+
return 1 + Integer.min(Integer.min(editDist(from, to, m, n - 1), // Insert --just left
40+
editDist(from, to, m - 1, n)),// Remove --just above
41+
editDist(from, to, m - 1, n - 1)); // Replace --just diagonal
42+
43+
}
44+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.string;
2+
3+
import java.util.LinkedHashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Given a string S consisting of lowercase Latin Letters. Find the first non repeating character in S.
8+
*/
9+
public class FindFirstNonRepeatingCharacter {
10+
11+
public static void main(String args[]) {
12+
String input = "ABCDHJACDHJ";
13+
int c = findFirstNonRepeatingChar(input);
14+
if (c == -1) {
15+
System.out.println("findFirstNonRepeatingChar : " + "Not Found");
16+
} else {
17+
System.out.println("findFirstNonRepeatingChar : " + (char) c);
18+
}
19+
}
20+
21+
private static int findFirstNonRepeatingChar(String input) {
22+
/**insertion order will preserved as we are using linkedHashMap */
23+
Map<Character, Integer> map = new LinkedHashMap<>();
24+
25+
/** store frequency of each character in map*/
26+
for (char c : input.toCharArray()) {
27+
if (map.containsKey(c)) {
28+
map.put(c, map.get(c) + 1);
29+
} else {
30+
map.put(c, 1);
31+
}
32+
}
33+
34+
for (char c : map.keySet()) {
35+
if (map.get(c) == 1) {
36+
return c;
37+
}
38+
}
39+
return -1;
40+
}
41+
}

0 commit comments

Comments
 (0)