-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathProblemSolutions.java
87 lines (76 loc) · 2.5 KB
/
ProblemSolutions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/******************************************************************
*
* Vaughn Hartzell 001
*
* This java file contains the problem solutions of isSubSet, findKthLargest,
* and sort2Arrays methods. You should utilize the Java Collection Framework for
* these methods.
*
********************************************************************/
import java.util.*;
class ProblemSolutions {
/**
* Method: isSubset()
*
* Given two arrays of integers, A and B, return whether
* array B is a subset if array A. Example:
* Input: [1,50,55,80,90], [55,90]
* Output: true
* Input: [1,50,55,80,90], [55,90, 99]
* Output: false
*
* The solution time complexity must NOT be worse than O(n).
* For the solution, use a Hash Table.
*
* @param list1 - Input array A
* @param list2 - input array B
* @return - returns boolean value B is a subset of A.
*/
public boolean isSubset(int list1[], int list2[]) {
HashSet<Integer> set = new HashSet<>();
for(int a=0;a<list1.length;a++){
set.add(list1[a]);
}
for(int b=0;b<list2.length;b++){
if(!set.contains(list2[b])){
return false;
}
}
return true;
}
/**
* Method: findKthLargest
*
* Given an Array A and integer K, return the k-th maximum element in the array.
* Example:
* Input: [1,7,3,10,34,5,8], 4
* Output: 7
*
* @param array - Array of integers
* @param k - the kth maximum element
* @return - the value in the array which is the kth maximum value
*/
public int findKthLargest(int[] array, int k) {
Arrays.sort(array);
return array[array.length-k];
}
/**
* Method: sort2Arrays
*
* Given two arrays A and B with n and m integers respectively, return
* a single array of all the elements in A and B in sorted order. Example:
* Input: [4,1,5], [3,2]
* Output: 1 2 3 4 5
*
* @param array1 - Input array 1
* @param array2 - Input array 2
* @return - Sorted array with all elements in A and B.
*/
public int[] sort2Arrays(int[] array1, int[] array2) {
int[] ans = new int[array1.length+array2.length];
System.arraycopy(array1, 0, ans, 0, array1.length);
System.arraycopy(array2, 0, ans, array1.length, array2.length);
Arrays.sort(ans);
return ans;
}
}