Skip to content

Commit 27f96ed

Browse files
author
Prashant Jain
committed
Added sorting and heap problems
1 parent 1a59684 commit 27f96ed

3 files changed

Lines changed: 165 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package in.knowledgegate.dsa.heap;
2+
3+
import java.util.PriorityQueue;
4+
5+
/**
6+
* Given an integer array nums and an integer k,
7+
* return the kth largest element in the array.
8+
*
9+
* Note that it is the kth largest element in the
10+
* sorted order, not the kth distinct element.
11+
*
12+
* Example 1:
13+
* Input: nums = [3,2,1,5,6,4], k = 2
14+
* Output: 5
15+
*
16+
* Example 2:
17+
* Input: nums = [3,2,3,1,2,4,5,5,6], k = 4
18+
* Output: 4
19+
*
20+
* Constraints:
21+
* 1 <= k <= nums.length <= 104
22+
* -104 <= nums[i] <= 104
23+
*/
24+
public class KthLargestElement {
25+
public static void main(String[] args) {
26+
KthLargestElement kthLargest =
27+
new KthLargestElement();
28+
int[] nums = new int[]{3,2,3,1,2,4,5,5,6};
29+
System.out.println("Kth largest is:" +
30+
kthLargest.findKthLargest(nums, 6));
31+
}
32+
33+
public int findKthLargest(int[] nums, int k) {
34+
PriorityQueue<Integer> kLargest =
35+
new PriorityQueue<>();
36+
for (int num : nums) {
37+
kLargest.add(num);
38+
if (kLargest.size() > k) {
39+
kLargest.poll();
40+
}
41+
}
42+
43+
return kLargest.peek();
44+
}
45+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package in.knowledgegate.dsa.sorting.problems;
2+
3+
/**
4+
* Given an integer array nums and an integer k,
5+
* return the kth largest element in the array.
6+
*
7+
* Note that it is the kth largest element in the
8+
* sorted order, not the kth distinct element.
9+
*
10+
* Example 1:
11+
* Input: nums = [3,2,1,5,6,4], k = 2
12+
* Output: 5
13+
*
14+
* Example 2:
15+
* Input: nums = [3,2,3,1,2,4,5,5,6], k = 4
16+
* Output: 4
17+
*
18+
* Constraints:
19+
* 1 <= k <= nums.length <= 104
20+
* -104 <= nums[i] <= 104
21+
*/
22+
public class KthLargestElement {
23+
public static void main(String[] args) {
24+
KthLargestElement kthLargest =
25+
new KthLargestElement();
26+
int[] nums = new int[]{3,2,3,1,2,4,5,5,6};
27+
System.out.println("Kth largest is:" +
28+
kthLargest.findKthLargest(nums, 4));
29+
}
30+
31+
public int findKthLargest(int[] nums, int k) {
32+
return quickSelect(nums, 0, nums.length-1, k);
33+
}
34+
35+
private int quickSelect(int[] nums, int beg,
36+
int end, int k) {
37+
int pivot = beg;
38+
for (int i = pivot; i < end; i++) {
39+
if (nums[i] <= nums[end]) {
40+
swap(nums, i, pivot);
41+
pivot++;
42+
}
43+
}
44+
swap(nums, pivot, end);
45+
int count = end - pivot + 1;
46+
if (count == k) return nums[pivot];
47+
if (count > k) {
48+
return quickSelect(nums, pivot + 1, end, k);
49+
} else {
50+
return quickSelect(nums, beg, pivot - 1,
51+
k - count);
52+
}
53+
}
54+
55+
private void swap(int[] nums, int i, int j) {
56+
int temp = nums[i];
57+
nums[i] = nums[j];
58+
nums[j] = temp;
59+
}
60+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package in.knowledgegate.dsa.sorting.problems;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Given an integer array nums, return all the
7+
* triplets [nums[i], nums[j], nums[k]] such that
8+
* i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
9+
*
10+
* Notice that the solution set must not contain
11+
* duplicate triplets.
12+
*
13+
* Example 1:
14+
* Input: nums = [-1,0,1,2,-1,-4]
15+
* Output: [[-1,-1,2],[-1,0,1]]
16+
*
17+
* Example 2:
18+
* Input: nums = []
19+
* Output: []
20+
*
21+
* Example 3:
22+
* Input: nums = [0]
23+
* Output: []
24+
*
25+
* Constraints:
26+
* 0 <= nums.length <= 3000
27+
* -105 <= nums[i] <= 105
28+
*/
29+
public class ThreeSum {
30+
public static void main(String[] args) {
31+
ThreeSum threeSum = new ThreeSum();
32+
int[] nums = new int[]{-1,0,1,2,-1,-4};
33+
System.out.println("Possible Triplets:" +
34+
threeSum.threeSum(nums));
35+
}
36+
37+
public Set<List<Integer>> threeSum(int[] nums) {
38+
Arrays.sort(nums);
39+
Set<List<Integer>> res = new HashSet<>();
40+
for (int i = 0; i < nums.length - 2; i++) {
41+
int beg = i + 1;
42+
int end = nums.length - 1;
43+
while (beg < end) {
44+
int sum = nums[i] + nums[beg] + nums[end];
45+
if (sum == 0) {
46+
res.add(Arrays.asList(nums[i],
47+
nums[beg], nums[end]));
48+
beg++;
49+
end--;
50+
} else if (sum < 0) {
51+
beg++;
52+
} else {
53+
end--;
54+
}
55+
}
56+
}
57+
58+
return res;
59+
}
60+
}

0 commit comments

Comments
 (0)