Skip to content

Commit 8dc600a

Browse files
author
Prashant Jain
committed
Added various sorting implementations
1 parent 293c322 commit 8dc600a

7 files changed

Lines changed: 329 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package in.knowledgegate.dsa.dfs;
2+
3+
public class Solitare {
4+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package in.knowledgegate.dsa.maps;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* You're creating a game with some amusing mini-games,
8+
* and you've decided to make a simple variant of
9+
* the game Mahjong.
10+
*
11+
* In this variant, players have a number of tiles,
12+
* each marked 0-9. The tiles can be grouped into
13+
* pairs or triples of the same tile. For example,
14+
* if a player has "33344466", the player's hand
15+
* has a triple of 3s, a triple of 4s, and a pair
16+
* of 6s. Similarly, "55555777" has a triple of 5s,
17+
* a pair of 5s, and a triple of 7s.
18+
*
19+
* A "complete hand" is defined as a collection
20+
* of tiles where all the tiles can be grouped
21+
* into any number of triples (zero or more) and
22+
* exactly one pair, and each tile is used in
23+
* exactly one triple or pair.
24+
*
25+
* Write a function that takes a string representation of a collection of tiles in no particular order, and returns true or false depending on whether or not the collection represents a complete hand.
26+
*
27+
* tiles1 = "11133555" # True. 111 33 555
28+
* tiles2 = "111333555" # False. There are three triples, 111 333 555 but no pair.
29+
* tiles3 = "00000111" # True. 000 00 111. Your pair and a triplet can be of the same value
30+
* # There is also no limit to how many of each tile there is.
31+
* tiles4 = "13233121" # True. Tiles are not guaranteed to be in order
32+
* tiles5 = "11223344555" # False. There cannot be more than one pair
33+
* tiles6 = "99999999" # True. You can have many of one tile
34+
* tiles7 = "999999999" # False.
35+
* tiles8 = "9" # False.
36+
* tiles9 = "99" # True. One pair.
37+
* tiles10 = "000022" # False.
38+
* tiles11 = "888889" # False. There cannot be any tiles left over.
39+
* tiles12 = "889" # False. There cannot be any tiles left over.
40+
* tiles13 = "88888844" # True. Two triples and one pair
41+
* tiles14 = "77777777777777" # True. Four triples and one pair
42+
* tiles15 = "1111111" # False.
43+
* tiles16 = "1111122222" # False.
44+
*
45+
* complete(tiles1) => True
46+
* complete(tiles2) => False
47+
* complete(tiles3) => True
48+
* complete(tiles4) => True
49+
* complete(tiles5) => False
50+
* complete(tiles6) => True
51+
* complete(tiles7) => False
52+
* complete(tiles8) => False
53+
* complete(tiles9) => True
54+
* complete(tiles10) => False
55+
* complete(tiles11) => False
56+
* complete(tiles12) => False
57+
* complete(tiles13) => True
58+
* complete(tiles14) => True
59+
* complete(tiles15) => False
60+
* complete(tiles16) => False
61+
*
62+
* Complexity Variable
63+
* N - Number of tiles in the input string
64+
*/
65+
public class MiniMahjongWinningHand {
66+
public boolean isWinningHand(String hand) {
67+
Map<Character, Integer> map = new HashMap<>();
68+
for (int i = 0; i < hand.length(); i++) {
69+
char c = hand.charAt(i);
70+
map.put(c, map.getOrDefault(c, 0) + 1);
71+
}
72+
73+
boolean pairFound = false;
74+
for (Integer count : map.values()) {
75+
if (count % 3 == 2) {
76+
if (!pairFound) {
77+
pairFound = true;
78+
} else {
79+
return false;
80+
}
81+
} else if (count % 3 != 0) {
82+
return false;
83+
}
84+
}
85+
return pairFound;
86+
}
87+
88+
public static void main(String[] args) {
89+
String tiles1 = "11133555";
90+
String tiles2 = "111333555";
91+
String tiles3 = "00000111";
92+
String tiles4 = "13233121";
93+
String tiles5 = "11223344555";
94+
String tiles6 = "99999999";
95+
String tiles7 = "999999999";
96+
String tiles8 = "9";
97+
String tiles9 = "99";
98+
String tiles10 = "000022";
99+
String tiles11 = "888889";
100+
String tiles12 = "889";
101+
String tiles13 = "88888844";
102+
String tiles14 = "77777777777777";
103+
String tiles15 = "1111111";
104+
String tiles16 = "1111122222";
105+
MiniMahjongWinningHand obj =
106+
new MiniMahjongWinningHand();
107+
System.out.println(tiles1 + ":" + obj.isWinningHand(tiles1));
108+
System.out.println(tiles2 + ":" + obj.isWinningHand(tiles2));
109+
System.out.println(tiles3 + ":" + obj.isWinningHand(tiles3));
110+
System.out.println(tiles4 + ":" + obj.isWinningHand(tiles4));
111+
System.out.println(tiles5 + ":" + obj.isWinningHand(tiles5));
112+
System.out.println(tiles6 + ":" + obj.isWinningHand(tiles6));
113+
System.out.println(tiles7 + ":" + obj.isWinningHand(tiles7));
114+
System.out.println(tiles8 + ":" + obj.isWinningHand(tiles8));
115+
System.out.println(tiles9 + ":" + obj.isWinningHand(tiles9));
116+
System.out.println(tiles10 + ":" + obj.isWinningHand(tiles10));
117+
System.out.println(tiles11 + ":" + obj.isWinningHand(tiles11));
118+
System.out.println(tiles12 + ":" + obj.isWinningHand(tiles12));
119+
System.out.println(tiles13 + ":" + obj.isWinningHand(tiles13));
120+
System.out.println(tiles14 + ":" + obj.isWinningHand(tiles14));
121+
System.out.println(tiles15 + ":" + obj.isWinningHand(tiles15));
122+
System.out.println(tiles16 + ":" + obj.isWinningHand(tiles16));
123+
}
124+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package in.knowledgegate.dsa.maps;public class MiniMahjongWinningHand {
2+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package in.knowledgegate.dsa.sorting.algos;
2+
3+
public class BubbleSort implements SortingAlgo {
4+
5+
private void swap(int[] nums, int i, int j) {
6+
int temp = nums[i];
7+
nums[i] = nums[j];
8+
nums[j] = temp;
9+
}
10+
11+
@Override
12+
public void sort(int[] nums) {
13+
for (int i = 0; i < nums.length - 1; i++) {
14+
for (int j = 0; j < nums.length - 1 - i; j++) {
15+
if (nums[j] > nums[j+1]) {
16+
swap(nums, j, j+1);
17+
}
18+
}
19+
// printArray(nums);
20+
}
21+
}
22+
23+
public static void main(String[] args) {
24+
int[] nums = {8, 6, 14, 77, 1, 13};
25+
26+
SortingAlgo sortingAlgo = new BubbleSort();
27+
sortingAlgo.sort(nums);
28+
System.out.print("Sorted Array:");
29+
printArray(nums);
30+
}
31+
32+
private static void printArray(int[] nums) {
33+
for (int num : nums) {
34+
System.out.print(num + " ");
35+
}
36+
System.out.println();
37+
}
38+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package in.knowledgegate.dsa.sorting.algos;
2+
3+
public class MergeSort implements SortingAlgo {
4+
5+
private void mergeSort(int[] nums) {
6+
int n = nums.length;
7+
if (n < 2) {
8+
return;
9+
}
10+
int mid = n / 2;
11+
int[] left = new int[mid];
12+
int[] right = new int[n - mid];
13+
14+
for (int i = 0; i < mid; i++) {
15+
left[i] = nums[i];
16+
}
17+
18+
for (int i = mid; i < n; i++) {
19+
right[i - mid] = nums[i];
20+
}
21+
22+
mergeSort(left);
23+
mergeSort(right);
24+
25+
merge(nums, left, right);
26+
}
27+
28+
private void merge(int[] result, int[] first,
29+
int[] second) {
30+
int i = 0, j = 0, k = 0;
31+
while (i < first.length && j < second.length) {
32+
if (first[i] <= second[j]) {
33+
result[k++] = first[i++];
34+
} else {
35+
result[k++] = second[j++];
36+
}
37+
}
38+
while (i < first.length) {
39+
result[k++] = first[i++];
40+
}
41+
while (j < second.length) {
42+
result[k++] = second[j++];
43+
}
44+
}
45+
46+
@Override
47+
public void sort(int[] nums) {
48+
mergeSort(nums);
49+
}
50+
51+
public static void main(String[] args) {
52+
int[] arr = {8, 6, 14, 77, 1, 13};
53+
int n = arr.length;
54+
55+
SortingAlgo sort = new MergeSort();
56+
sort.sort(arr);
57+
System.out.println("Sorted array: ");
58+
printArray(arr, n);
59+
}
60+
61+
static void printArray(int[] arr, int size) {
62+
for (int i = 0; i < size; i++)
63+
System.out.print(arr[i] + " ");
64+
System.out.println();
65+
}
66+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package in.knowledgegate.dsa.sorting.algos;
2+
3+
public class QuickSort implements SortingAlgo {
4+
5+
private void swap(int[] arr, int i, int j) {
6+
int temp = arr[i];
7+
arr[i] = arr[j];
8+
arr[j] = temp;
9+
}
10+
11+
private int partition(int[] nums, int low,
12+
int high) {
13+
int pivot = nums[high];
14+
int i = low;
15+
16+
for (int j = low; j < high; j++) {
17+
if (nums[j] < pivot) {
18+
swap(nums, i++, j);
19+
}
20+
}
21+
swap(nums, i, high);
22+
return i;
23+
}
24+
25+
private void quickSort(int[] nums, int low,
26+
int high) {
27+
if (low < high) {
28+
int partitionIndex = partition(nums, low,
29+
high);
30+
quickSort(nums, 0, partitionIndex - 1);
31+
quickSort(nums, partitionIndex + 1, high);
32+
}
33+
}
34+
35+
@Override
36+
public void sort(int[] nums) {
37+
quickSort(nums, 0, nums.length - 1);
38+
}
39+
40+
public static void main(String[] args) {
41+
int[] arr = {8, 6, 14, 77, 1, 13};
42+
int n = arr.length;
43+
44+
SortingAlgo sort = new QuickSort();
45+
sort.sort(arr);
46+
System.out.println("Sorted array: ");
47+
printArray(arr, n);
48+
}
49+
50+
static void printArray(int[] arr, int size) {
51+
for (int i = 0; i < size; i++)
52+
System.out.print(arr[i] + " ");
53+
System.out.println();
54+
}
55+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package in.knowledgegate.dsa.sorting.algos;
2+
3+
public class SelectionSort implements SortingAlgo {
4+
5+
private void swap(int[] nums, int i, int j) {
6+
int temp = nums[i];
7+
nums[i] = nums[j];
8+
nums[j] = temp;
9+
}
10+
11+
@Override
12+
public void sort(int[] nums) {
13+
int n = nums.length;
14+
for (int i = 0; i < n - 1; i++) {
15+
int minIndex = i;
16+
for (int j = i + 1; j < n; j++) {
17+
if (nums[j] < nums[minIndex]) {
18+
minIndex = j;
19+
}
20+
}
21+
swap(nums, minIndex, i);
22+
}
23+
}
24+
25+
public static void main(String[] args) {
26+
int[] nums = {8, 6, 14, 77, 1, 13};
27+
28+
SortingAlgo sortingAlgo = new SelectionSort();
29+
sortingAlgo.sort(nums);
30+
System.out.print("Sorted Array:");
31+
printArray(nums);
32+
}
33+
34+
private static void printArray(int[] nums) {
35+
for (int num : nums) {
36+
System.out.print(num + " ");
37+
}
38+
System.out.println();
39+
}
40+
}

0 commit comments

Comments
 (0)