Skip to content

Commit 6682be2

Browse files
committed
Rotated index: done
1 parent 8923cd6 commit 6682be2

File tree

3 files changed

+95
-35
lines changed

3 files changed

+95
-35
lines changed

src/main/java/com/rampatra/arrays/PivotedBinarySearch.java

+27-21
Original file line numberDiff line numberDiff line change
@@ -7,62 +7,68 @@
77
*
88
* @author rampatra
99
* @since 5/31/15
10-
* @time: 10:44 PM
1110
*/
1211
public class PivotedBinarySearch {
1312

1413
/**
15-
* Search an element in a sorted pivoted array {@param a}.
14+
* Search an element in a sorted pivoted array {@code arr}.
1615
* <p/>
1716
* Example,
1817
* 1) For array [3,4,5,1,2] pivot is 5
1918
* 2) For array [6,7,8,5,4] pivot is 8
2019
*
21-
* @param a
20+
* @param arr
2221
* @param n
2322
* @return
2423
*/
25-
public static int pivotedBinarySearch(int a[], int n) {
26-
int pivot = findPivot(a, 0, a.length - 1);
24+
public static int pivotedBinarySearch(int[] arr, int n) {
25+
int pivot = findPivotIndex(arr, 0, arr.length - 1);
2726

28-
if (pivot == -1 || a[pivot] == n) {
27+
if (pivot == -1 || arr[pivot] == n) {
2928
return pivot;
30-
} else if (n <= a[0]) {
31-
return BinarySearch.binarySearch(a, n, pivot + 1, a.length - 1);
29+
} else if (n < arr[0]) {
30+
return BinarySearch.binarySearch(arr, n, pivot + 1, arr.length - 1);
3231
} else {
33-
return BinarySearch.binarySearch(a, n, 0, pivot - 1);
32+
return BinarySearch.binarySearch(arr, n, 0, pivot - 1);
3433
}
3534
}
3635

3736
/**
38-
* Finds the pivot element in array {@param a}. Pivot element is the only
37+
* Finds the pivot element in array {@code arr}. Pivot element is the only
3938
* element for which next element to it is smaller than it.
4039
*
41-
* @param a
40+
* @param arr
4241
* @param low
4342
* @param high
44-
* @return
43+
* @return the index of the pivot element in the {@code arr}.
4544
*/
46-
public static int findPivot(int a[], int low, int high) {
45+
public static int findPivotIndex(int[] arr, int low, int high) {
4746
if (low > high) return -1;
48-
if (low == high) return low;
4947

5048
int mid = (low + high) / 2;
5149

52-
if (a[mid] > a[mid + 1] && a[mid] > a[mid - 1]) {
50+
if (mid == 0 || mid == arr.length - 1) return -1;
51+
52+
if (arr[mid] > arr[mid + 1] && arr[mid] > arr[mid - 1]) {
5353
return mid;
54-
} else if (a[mid] > a[mid - 1] && a[mid] < a[mid + 1]) {
55-
return findPivot(a, mid + 1, a.length - 1);
54+
} else if (arr[mid] > arr[mid - 1] && arr[mid] < arr[mid + 1]) {
55+
return findPivotIndex(arr, mid + 1, arr.length - 1);
5656
} else {
57-
return findPivot(a, 0, mid - 1);
57+
return findPivotIndex(arr, 0, mid - 1);
5858
}
5959
}
6060

6161
public static void main(String[] args) {
62-
System.out.println("Pivot: " + findPivot(new int[]{1, 2, 3, 4, 5}, 0, 3));
62+
System.out.println("Pivot: " + findPivotIndex(new int[]{3, 4, 5, 1, 2}, 0, 4));
63+
System.out.println("Index: " + pivotedBinarySearch(new int[]{3, 4, 5, 1, 2}, 5));
64+
65+
System.out.println("Pivot: " + findPivotIndex(new int[]{1, 2, 3, 4, 5}, 0, 4));
6366
System.out.println("Index: " + pivotedBinarySearch(new int[]{1, 2, 3, 4, 5}, 4));
6467

65-
System.out.println("Pivot: " + findPivot(new int[]{5}, 0, 0));
66-
System.out.println("Index: " + pivotedBinarySearch(new int[]{5}, 5));
68+
System.out.println("Pivot: " + findPivotIndex(new int[]{5, 4, 3, 2, 1}, 0, 4));
69+
System.out.println("Index: " + pivotedBinarySearch(new int[]{5, 4, 3, 2, 1}, 4));
70+
71+
System.out.println("Pivot: " + findPivotIndex(new int[]{5}, 0, -1));
72+
System.out.println("Index: " + pivotedBinarySearch(new int[]{5}, -1));
6773
}
6874
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.rampatra.arrays;
2+
3+
/**
4+
* @author rampatra
5+
* @since 2019-04-04
6+
*/
7+
public class RotatedIndex {
8+
9+
private static int findIndexOfRotationPoint(String[] words) {
10+
return findIndexOfRotationPoint(words, 0, words.length - 1);
11+
}
12+
13+
private static int findIndexOfRotationPoint(String[] words, int start, int end) {
14+
if (start > end) return -1;
15+
16+
int mid = (start + end) / 2;
17+
18+
if (mid == 0 || mid == words.length - 1) return -1;
19+
20+
if (words[mid].compareTo(words[mid - 1]) < 0 && words[mid].compareTo(words[mid + 1]) < 0) {
21+
return mid;
22+
} else if (words[mid].compareTo(words[mid - 1]) > 0 && words[mid].compareTo(words[mid + 1]) < 0) {
23+
return findIndexOfRotationPoint(words, start, mid - 1);
24+
} else {
25+
return findIndexOfRotationPoint(words, mid + 1, end);
26+
}
27+
}
28+
29+
public static void main(String[] args) {
30+
System.out.println(findIndexOfRotationPoint(new String[]{
31+
"ptolemaic",
32+
"retrograde",
33+
"supplant",
34+
"undulate",
35+
"xenoepist",
36+
"asymptote", // <-- rotates here!
37+
"babka",
38+
"banoffee",
39+
"engender",
40+
"karpatka",
41+
"othellolagkage",
42+
}));
43+
44+
System.out.println(findIndexOfRotationPoint(new String[]{}));
45+
46+
System.out.println(findIndexOfRotationPoint(new String[]{
47+
"asymptote",
48+
"babka",
49+
"banoffee",
50+
"engender",
51+
"karpatka",
52+
"othellolagkage",
53+
}));
54+
}
55+
}

src/main/java/com/rampatra/base/Trie.java

+13-14
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,27 @@
33
import java.util.HashMap;
44

55
/**
6-
* Created by IntelliJ IDEA.
7-
* <p/>
86
* Trie also called digital tree and sometimes radix tree or prefix tree (as they can be
97
* searched by prefixes), is an ordered tree data structure that is used to store a dynamic
108
* set or associative array where the keys are usually strings.
119
* <p/>
1210
* You can think it as HashMap of HashMap of HashMap and so on. Each key in the HashMap is a
1311
* single digit/letter of the data you want to store and {@code data} is the final full word
1412
* you want to save in trie.
13+
* <p>
14+
* Some resources:
15+
* <a href="https://en.wikipedia.org/wiki/Trie">Trie Data Structure</a>
16+
* <a href="https://www.topcoder.com/community/data-science/data-science-tutorials/using-tries">More about Tries</a>
1517
*
1618
* @author rampatra
1719
* @since 9/22/15
18-
* @time: 8:19 PM
19-
* @see: https://en.wikipedia.org/wiki/Trie
20-
* @see: https://www.topcoder.com/community/data-science/data-science-tutorials/using-tries
2120
*/
2221
public class Trie<E> {
2322

2423
TrieNode<E> root;
2524

2625
Trie() {
27-
root = new TrieNode<>(null, new HashMap<String, TrieNode<E>>());
26+
root = new TrieNode<>(null, new HashMap<>());
2827
}
2928

3029
/**
@@ -48,7 +47,7 @@ public void insert(E data) {
4847
}
4948

5049
while (i < str.length()) {
51-
curr.children.put(str.substring(i, i + 1), new TrieNode<>(null, new HashMap<String, TrieNode<E>>()));
50+
curr.children.put((E) str.substring(i, i + 1), new TrieNode<>(null, new HashMap<>()));
5251
curr = curr.children.get(str.substring(i, i + 1));
5352
i++;
5453
}
@@ -57,10 +56,10 @@ public void insert(E data) {
5756
}
5857

5958
/**
60-
* Searches {@param data} in trie.
59+
* Searches {@code data} in trie.
6160
*
62-
* @param data
63-
* @return {@code true} if {@param data} is present, {@code false} otherwise.
61+
* @param data the value to search.
62+
* @return {@code true} if {@code data} is present, {@code false} otherwise.
6463
*/
6564
public boolean search(E data) {
6665

@@ -79,11 +78,11 @@ public boolean search(E data) {
7978
return curr.data != null && curr.data.equals(data);
8079
}
8180

82-
private class TrieNode<E> {
83-
E data; // stores the complete string (required to determine whether the string is in the trie)
84-
HashMap<String, TrieNode<E>> children;
81+
private class TrieNode<T> {
82+
T data; // stores the complete string (required to determine whether the string is in the trie)
83+
HashMap<T, TrieNode<T>> children;
8584

86-
TrieNode(E data, HashMap<String, TrieNode<E>> children) {
85+
TrieNode(T data, HashMap<T, TrieNode<T>> children) {
8786
this.data = data;
8887
this.children = children;
8988
}

0 commit comments

Comments
 (0)