forked from compasrc/HW5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblemSolutions.java
More file actions
119 lines (91 loc) · 3.1 KB
/
Copy pathProblemSolutions.java
File metadata and controls
119 lines (91 loc) · 3.1 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/******************************************************************
*
* Malec Basel Tarabein / 002
*
* 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[]) {
// ADD YOU CODE HERE -- DON'T FORGET TO ADD YOR NAME AT TOP OF FILE
Set<Integer> set = new HashSet<Integer>();
// part 1: put list1 in set
for(int num : list1) {
set.add(num);
}
// part 2: check if set contains all integers
for(int num : list2) {
if (!set.contains(num))
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) {
// ADD YOUR CODE HERE
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
for(int num : array) {
pq.add(-1 * num);
}
for(int i = 1; i < k; i++) {
pq.poll();
}
Integer result = pq.poll();
return (result == null ? null : -1 * (int)result);
}
/**
* 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) {
// ADD YOU CODE HERE
int[] result = new int[array1.length + array2.length];
for(int i = 0; i < array1.length; i++) {
result[i] = array1[i];
}
for(int i = 0; i < array2.length; i++) {
result[i + array1.length] = array2[i];
}
Arrays.sort(result);
return result;
}
}