Skip to content

Commit 18777f7

Browse files
committed
More problems
1 parent a29028c commit 18777f7

24 files changed

+767
-35
lines changed

src/main/java/array/BSearch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public class BSearch {
66
public static void main(String[] args) {
77
List<Integer> input = Arrays.asList(2, 3, 5, 8, 9, 12,13 ,14);
8-
System.out.println(bsearch(input, 3));
8+
System.out.println(bsearch(input, 3));
99
}
1010

1111
public static int bsearch(List<Integer> list, int key) {

src/main/java/array/BinarySearchFirstK.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import java.util.*;
44

55
public class BinarySearchFirstK {
6+
/*
7+
Find the first index whose array value is same as the input key */
8+
69
public static void main(String[] args) {
710
List<Integer> input = Arrays.asList(2, 3, 3, 5, 8, 9, 12, 13, 14);
811
System.out.println(bsearch(input, 3));

src/main/java/array/BinarySearchFirstLargerThanK.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.*;
44

55
public class BinarySearchFirstLargerThanK {
6+
/* Find the first array value which is greater than the input key */
67
public static void main(String[] args) {
78
List<Integer> input = Arrays.asList(2, 3, 3, 3, 5, 8, 9, 12, 13, 14);
89
System.out.println(bsearch(input, 3));

src/main/java/array/Candy.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,43 @@ public class Candy {
66
giving candies to these children subjected to the following requirements:
77
1. Each child must have at least one candy. 2. Children with a higher rating get
88
more candies than their neighbors.
9-
What is the minimum candies you must give?
9+
What is the minimum candies you must give?
1010
*/
1111
public static void main(String[] args) {
12+
int[] rating = {1,2,3,1,2,4};
13+
int[] dist = findDistribution(rating);
14+
for (int i: dist) {
15+
System.out.print(i + " ");
16+
}
17+
System.out.println();
18+
rating = new int[]{4,6,4,5,6,2};
19+
dist = findDistribution(rating);
20+
for (int i: dist) {
21+
System.out.print(i + " ");
22+
}
23+
System.out.println();
24+
rating = new int[]{2,3,4,4,3,2,1};
25+
dist = findDistribution(rating);
26+
for (int i: dist) {
27+
System.out.print(i + " ");
28+
}
29+
}
1230

31+
public static int[] findDistribution(int[] rating) {
32+
int[] dist = new int[rating.length];
33+
dist[0] = 1;
34+
for (int i=1;i<rating.length;i++) {
35+
if (rating[i] > rating[i-1]) {
36+
dist[i] = dist[i - 1] + 1;
37+
} else {
38+
dist[i] = 1;
39+
}
40+
}
41+
for (int i = rating.length - 2;i >= 0;i--) {
42+
if (rating[i] > rating[i+1]) {
43+
dist[i] = Math.max(dist[i], dist[i + 1] + 1);
44+
}
45+
}
46+
return dist;
1347
}
1448
}

src/main/java/array/ContainerWithMostWater.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class ContainerWithMostWater {
1414
*/
1515
public static void main(String[] args) {
1616
System.out.println(compute(new int[]{1,2,4,2,3,2,5,2}));
17-
System.out.println(compute(new int[]{1,1,3,2,4,1,4,2,2,3}));
17+
System.out.println(compute(new int[]{1,1,3,2,4,1,4,2,2,3}));
1818
}
1919

2020
public static int compute(int[] arr) {
@@ -26,9 +26,12 @@ public static int compute(int[] arr) {
2626
int right = arr.length - 1;
2727
while (left < right) {
2828
max = Math.max(max, (right - left) * Math.min(arr[left], arr[right]));
29-
if (arr[left] <arr[right]) {
29+
if (arr[left] < arr[right]) {
3030
left++;
31+
} else if (arr[left] > arr[right]) {
32+
right--;
3133
} else {
34+
left++;
3235
right--;
3336
}
3437
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package array;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
public class ContainsDuplicate {
8+
/*
9+
* Given an array of integers, find if the array contains any duplicates. Your
10+
* function should return true if any value appears at least twice in the array,
11+
* and it should return false if every element is distinct.
12+
*/
13+
public static void main(String[] args) {
14+
System.out.println(compute(new int[]{1,2,4,2,3,2,5,2}));
15+
System.out.println(compute2(new int[] { 1, 2, 4, 2, 3, 2, 5, 2 }));
16+
System.out.println(compute(new int[] { 1, 1, 3, 2, 4, 1, 4, 2, 2, 3 }));
17+
System.out.println(compute2(new int[]{1,1,3,2,4,1,4,2,2,3}));
18+
}
19+
20+
public static boolean compute(int[] arr) {
21+
Set<Integer> set = new HashSet<>();
22+
for (int i=0;i<arr.length;i++) {
23+
if (!set.contains(arr[i])) {
24+
set.add(arr[i]);
25+
} else {
26+
return true;
27+
}
28+
}
29+
return false;
30+
}
31+
32+
public static boolean compute2(int[] arr) {
33+
Arrays.sort(arr);
34+
for (int i = 1;i<arr.length;i++) {
35+
if (arr[i-1] == arr[i]) {
36+
return true;
37+
}
38+
}
39+
return false;
40+
}
41+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package array;
2+
3+
public class FindDuplicates {
4+
public static void main(String[] args) {
5+
int input[] = {2,1,2,1};
6+
System.out.println(compute(input));
7+
}
8+
9+
public static int compute(int[] arr) {
10+
int result = 0;
11+
for (int i=0;i<arr.length;i++) {
12+
int index = Math.abs(arr[i]);
13+
if (arr[index] < 0) {
14+
System.out.println(index);
15+
result++;
16+
} else {
17+
arr[index] = -arr[index];
18+
}
19+
}
20+
return result;
21+
}
22+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package array;
2+
3+
import java.util.ArrayList;
4+
import java.util.Comparator;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.PriorityQueue;
9+
import java.util.Stack;
10+
11+
public class KFrequentElements {
12+
public static void main(String[] args) {
13+
int[] nums = {1,2,3,2,1,3,2,4,5,6,5};
14+
int k = 3;
15+
System.out.println(String.format("%d frequent elements are %s", k, compute(nums, k)));
16+
}
17+
18+
public static List<Integer> compute(int[] nums, int k) {
19+
Map<Integer, Integer> map = new HashMap<>();
20+
for (int num: nums) {
21+
Integer count = map.getOrDefault(num, null);
22+
if (count == null) {
23+
count = 0;
24+
}
25+
count++;
26+
map.put(num, count);
27+
}
28+
PriorityQueue<Pair> minHeap = new PriorityQueue<>(Comparator.comparing(Pair -> Pair.count));
29+
for (Map.Entry<Integer, Integer> entry: map.entrySet()) {
30+
minHeap.add(new Pair(entry.getKey(), entry.getValue()));
31+
if (minHeap.size() > k) {
32+
minHeap.poll();
33+
}
34+
}
35+
Stack<Integer> stack = new Stack<>();
36+
while (minHeap.size() > 0) {
37+
stack.push(minHeap.poll().num);
38+
}
39+
List<Integer> res = new ArrayList<>();
40+
while (!stack.isEmpty()) {
41+
res.add(stack.pop());
42+
}
43+
return res;
44+
}
45+
}
46+
47+
class Pair{
48+
int num;
49+
int count;
50+
public Pair(int num, int count) {
51+
this.num = num;
52+
this.count = count;
53+
}
54+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package array;
2+
3+
import java.util.Comparator;
4+
import java.util.List;
5+
import java.util.PriorityQueue;
6+
import java.util.stream.Collectors;
7+
import java.util.stream.IntStream;
8+
9+
public class KLargestElements {
10+
public static void main(String[] args) {
11+
int k = 3;
12+
int[] input = { 1, 23, 12, 9, 30, 2, 50};
13+
System.out.println(compute(input, k));
14+
}
15+
16+
private static List<Integer> compute(int[] nums, int k) {
17+
PriorityQueue<Integer> pQueue = new PriorityQueue<>(new Comparator<Integer>() {
18+
19+
@Override
20+
public int compare(Integer o1, Integer o2) {
21+
if (o1 > o2) {
22+
return -1;
23+
} else if (o1 < o2) {
24+
return 1;
25+
}
26+
return 0;
27+
}
28+
});
29+
for (int i: nums) {
30+
pQueue.add(i);
31+
}
32+
return IntStream.range(1, k+1).map(i -> pQueue.poll()).boxed().collect(Collectors.toList());
33+
}
34+
}

src/main/java/array/KthMostFrequentString.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55

66
public class KthMostFrequentString {
77
public static void main(String[] args) {
8-
Map<String, Integer> countMap = new HashMap<>();
9-
List<String> input = Arrays.asList("a", "b", "c", "a", "b", "a");
108
int k = 0;
9+
List<String> input = Arrays.asList("a", "b", "c", "a", "b", "a");
10+
11+
System.out.println(String.format("%dth frequent string: %s", k, getResult(input, k)));
12+
}
13+
14+
public static String getResult(List<String> input, int k) {
15+
Map<String, Integer> countMap = new HashMap<>();
1116
for (String value : input) {
1217
Integer count = countMap.getOrDefault(value, null);
1318
if (count == null) {
@@ -17,16 +22,16 @@ public static void main(String[] args) {
1722
}
1823
List<Map.Entry<String, Integer>> countList = countMap.entrySet().stream().collect(Collectors.toList());
1924
Collections.sort(countList, new Comparator<Map.Entry<String, Integer>>() {
20-
@Override
21-
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
22-
if (o1.getValue() > o2.getValue()) {
23-
return -1;
24-
} else if (o1.getValue() < o2.getValue()) {
25-
return 1;
25+
@Override
26+
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
27+
if (o1.getValue() > o2.getValue()) {
28+
return -1;
29+
} else if (o1.getValue() < o2.getValue()) {
30+
return 1;
31+
}
32+
return 0;
2633
}
27-
return 0;
28-
}
29-
});
30-
System.out.println(countList.get(k).getKey());
34+
});
35+
return countList.get(k).getKey();
3136
}
3237
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package array;
2+
3+
import java.util.Arrays;
4+
5+
public class LongestConsecutiveSequence {
6+
public static void main(String[] args) {
7+
int[] input = {4,2,1,6,5};
8+
System.out.println(compute(input));
9+
input=new int[] {5,5,3,1};
10+
System.out.println(compute(input));
11+
}
12+
13+
public static int compute(int[] arr) {
14+
Arrays.sort(arr);
15+
int result = 1;
16+
for (int i = 1;i<arr.length;i++) {
17+
System.out.println("next element: " + arr[i]);
18+
if (arr[i] - 1 == arr[i - 1]) {
19+
result++;
20+
} else {
21+
result = 1;
22+
}
23+
}
24+
return result;
25+
}
26+
}

src/main/java/array/MatrixSearch.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package array;
2+
3+
public class MatrixSearch {
4+
/*
5+
* Given an n x m array where all rows and columns are in sorted order, write a
6+
* function to determine whether the array contains an element x.
7+
8+
contains([[1,2,3,4],
9+
[5,6,7,8],
10+
[9,10,11,12]],
11+
11) = true
12+
*/
13+
public static void main(String[] args) {
14+
int key = 13;
15+
int[][] arr = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
16+
System.out.println(compute(arr, 0, 0, arr.length - 1, arr[0].length - 1, key));
17+
}
18+
19+
public static boolean compute(int[][] arr, int topx, int topy, int bottomx, int bottomy, int key) {
20+
int maxX = arr.length - 1;
21+
int maxY = arr[0].length - 1;
22+
if (topx < 0 || topx > maxX) return false;
23+
if (topy < 0 || topy > maxY) return false;
24+
if (bottomx < 0 || bottomx > maxX) return false;
25+
if (bottomy < 0 || bottomy > maxY) return false;
26+
27+
int midx = topx + (bottomx - topx) / 2;
28+
int midy = topy + (bottomy - topy) / 2;
29+
if (arr[midx][midy] == key) {
30+
return true;
31+
} else if (arr[midx][midy] < key) {
32+
if (key <= arr[midx][maxY]) {
33+
for (int i = midy; i <= maxY; i++) {
34+
if (key == arr[midx][i])
35+
return true;
36+
}
37+
return false;
38+
} else {
39+
return compute(arr, midx + 1, 0, bottomx, bottomy, key);
40+
}
41+
} else {
42+
if (key >= arr[midx][0]) {
43+
for (int i = 0; i < midy; i++) {
44+
if (key == arr[midx][i])
45+
return true;
46+
}
47+
return false;
48+
} else {
49+
return compute(arr, topx, topy, midx - 1, maxY, key);
50+
}
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)