Skip to content

Commit 8e78001

Browse files
committed
More problems
1 parent ec120ae commit 8e78001

8 files changed

+467
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package array;
2+
3+
import java.util.Comparator;
4+
import java.util.*;
5+
6+
public class ConvertToBiggestNum {
7+
/* Write a function that takes a number as input and outputs the biggest number with the same set of digits. */
8+
9+
public static void main(String[] args) {
10+
int input = 423865;
11+
System.out.println(convert(input));
12+
}
13+
14+
public static int convert(int n) {
15+
String strN = String.valueOf(n);
16+
char[] arrN = strN.toCharArray();
17+
Arrays.sort(arrN);
18+
StringBuilder strBuilder = new StringBuilder();
19+
for (int i = arrN.length - 1; i >= 0; i--) {
20+
strBuilder.append(arrN[i]);
21+
}
22+
return Integer.parseInt(strBuilder.toString());
23+
}
24+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package array;
2+
3+
import java.util.List;
4+
import java.util.ArrayList;
5+
6+
public class InsertInterval {
7+
/*
8+
LeetCode 57. Insert Interval
9+
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
10+
11+
You may assume that the intervals were initially sorted according to their start times.
12+
13+
Example 1:
14+
15+
Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
16+
Output: [[1,5],[6,9]]
17+
18+
Example 2:
19+
20+
Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
21+
Output: [[1,2],[3,10],[12,16]]
22+
Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].
23+
*/
24+
public static class Tuple {
25+
int x, y;
26+
public Tuple(int x, int y) {
27+
this.x = x;
28+
this.y = y;
29+
}
30+
public String toString() {
31+
return "[" + x + "," + y + "]";
32+
}
33+
}
34+
public static void main(String[] args) {
35+
int[][] intervals = { { 1, 2 }, { 3, 5 }, { 6, 7 }, { 8, 10 }, { 12, 16 } };
36+
int[] newInterval = {4,8};
37+
38+
InsertInterval instance = new InsertInterval();
39+
instance.insert(intervals, newInterval);
40+
}
41+
42+
public int[][] insert(int[][] intervals, int[] newInterval) {
43+
List<Tuple> result = new ArrayList<>();
44+
boolean started = false;
45+
boolean ended = false;
46+
int startVal = 0;
47+
for (int i = 0;i<intervals.length;i++) {
48+
int[] next = intervals[i];
49+
if (newInterval[0] >= next[0] && newInterval[0] <= next[1]) {
50+
if (newInterval[1] <= next[1]) {
51+
result.add(new Tuple(next[0], next[1]));
52+
} else {
53+
//net interval starts at this interval and continues
54+
started = true;
55+
startVal = next[0];
56+
}
57+
} else if (newInterval[0] < next[0]) {
58+
if (started && newInterval[1] <= next[1]) {
59+
result.add(new Tuple(startVal, next[1]));
60+
ended = true;
61+
}
62+
} else if (ended) {
63+
result.add(new Tuple(next[0], next[1]));
64+
}
65+
}
66+
System.out.println(result);
67+
return null;
68+
}
69+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package array;
2+
3+
import java.util.*;
4+
5+
public class MergeInterval {
6+
public static void main(String[] args) {
7+
/*
8+
Given a collection of intervals, merge all overlapping intervals.
9+
10+
For example,
11+
Given [1,3],[2,6],[8,10],[15,18],
12+
return [1,6],[8,10],[15,18].
13+
*/
14+
int[][] input = { { 1, 3 }, { 2, 6 }, { 8, 10 }, { 15, 18 } };
15+
int[][] output = mergeInterval(input);
16+
System.out.print("\n[");
17+
for (int i = 0; i< output.length; i++) {
18+
if (i > 0) {
19+
System.out.print(", ");
20+
}
21+
System.out.print("[" + output[i][0] + "," + output[i][1] + "]");
22+
}
23+
System.out.print("]\n");
24+
}
25+
26+
static class Tuple {
27+
int x;
28+
int y;
29+
public Tuple(int x, int y) {
30+
this.x = x;
31+
this.y = y;
32+
}
33+
public String toString() {
34+
return "[" + x + "," + y + "]";
35+
}
36+
}
37+
public static int[][] mergeInterval(int[][] input) {
38+
List<Tuple> collect = new ArrayList<>();
39+
int l = input[0][0], h = input[0][1];
40+
for (int i=1;i<input.length;i++) {
41+
boolean l_mod = false, h_mod = false;
42+
if (input[i][0] > l && input[i][1] < h) {
43+
continue;
44+
} else if (input[i][0] < l && input[i][1] > h) {
45+
l = input[i][0];
46+
h = input[i][1];
47+
} else if (input[i][0] > l && input[i][0] < h && input[i][1] > h) {
48+
h = input[i][1];
49+
} else if (input[i][0] < l && input[i][1] > l && input[i][1] < h) {
50+
l = input[i][0];
51+
} else {
52+
collect.add(new Tuple(l, h));
53+
l = input[i][0];
54+
h = input[i][1];
55+
}
56+
}
57+
collect.add(new Tuple(l, h));
58+
final int[][] result = new int[collect.size()][2];
59+
for (int i = 0; i<collect.size(); i++) {
60+
result[i][0] = collect.get(i).x;
61+
result[i][1] = collect.get(i).y;
62+
}
63+
return result;
64+
}
65+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package array;
2+
3+
import java.util.Arrays;
4+
5+
public class SearchInRotatedSortedArray {
6+
/*
7+
LeetCode 33. Search in Rotated Sorted Array
8+
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
9+
10+
(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
11+
12+
You are given a target value to search. If found in the array return its index, otherwise return -1.
13+
14+
You may assume no duplicate exists in the array.
15+
16+
Your algorithm's runtime complexity must be in the order of O(log n).
17+
18+
Example 1:
19+
20+
Input: nums = [4,5,6,7,0,1,2], target = 0
21+
Output: 4
22+
23+
Example 2:
24+
25+
Input: nums = [4,5,6,7,0,1,2], target = 3
26+
Output: -1
27+
*/
28+
29+
public static void main(String[] args) {
30+
SearchInRotatedSortedArray instance = new SearchInRotatedSortedArray();
31+
int[] input = {4, 5, 6, 7, 0, 1, 2};
32+
System.out.println(instance.search(input, 0));
33+
System.out.println(instance.search(input, 3));
34+
System.out.println(instance.search(input, 4));
35+
System.out.println(instance.search(input, 2));
36+
System.out.println(instance.search(input, 7));
37+
int[] input2 = {};
38+
System.out.println(instance.search(input2, 7));
39+
int[] input3 = {1};
40+
System.out.println(instance.search(input3, 2));
41+
int[] input4 = {4,5,6,7,8,9,1,2,3};
42+
System.out.println(instance.search(input4, 1));
43+
44+
System.out.println(instance.search2(input4, 0, 8, 1));
45+
}
46+
47+
public int search(int[] nums, int target) {
48+
if (nums.length == 0) {
49+
return -1;
50+
}
51+
52+
int l = 0, h = nums.length - 1;
53+
int mid = 0;
54+
while (l <= h) {
55+
mid = l + (h - l) / 2;
56+
if (nums[l] == target) {
57+
return l;
58+
} else if (nums[h] == target) {
59+
return h;
60+
}
61+
if (nums[mid] == target) {
62+
return mid;
63+
} else if (nums[mid] > nums[h]) {
64+
l = mid + 1;
65+
} else {
66+
h = mid - 1;
67+
}
68+
}
69+
System.out.println("mid: " + mid + ", value: " + nums[mid]);
70+
if (target < nums[nums.length -1]) {
71+
l = mid + 1;
72+
h = nums.length - 1;
73+
} else {
74+
l = 0;
75+
h = mid - 1;
76+
}
77+
while (l<=h) {
78+
mid = l + (h - l) / 2;
79+
if (nums[mid] < target) {
80+
l = mid + 1;
81+
} else if (nums[mid] > target) {
82+
h = mid - 1;
83+
} else {
84+
return mid;
85+
}
86+
}
87+
return -1;
88+
}
89+
90+
//Alternate solution
91+
public int search2(int[] nums, int left, int right, int target) {
92+
if (left == right) {
93+
return (nums[left] == target)? left: -1;
94+
}
95+
int mid = left + (right - left) / 2;
96+
if (nums[mid] == target)
97+
return mid;
98+
99+
//pivot is on the right of the mid element
100+
if (nums[left] <= nums[mid]) {
101+
if (target >= nums[left] && target <= nums[mid]) {
102+
return search2(nums, left, mid - 1, target);
103+
} else {
104+
return search2(nums, mid + 1, right, target);
105+
}
106+
} else {
107+
//pivot is on the left of the mid element
108+
if (target >= nums[mid] && target <= nums[right]) {
109+
return search2(nums, mid + 1, right, target);
110+
} else {
111+
return search2(nums, left, mid - 1, target);
112+
}
113+
}
114+
}
115+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package dynamicprogramming;
2+
3+
import java.util.*;
4+
5+
public class ParenthesisPermutation {
6+
7+
/* Print all possible n pairs of balanced parentheses.
8+
9+
For example, when n is 2, the function should print “(())” and “()()”. When n is 3, we should get “((()))”, “(()())”, “(())()”, “()(())”, “()()()”.
10+
*/
11+
public static void main(String[] args) {
12+
int n = 4;
13+
System.out.println(getPermutation(n));
14+
}
15+
16+
private static List<String> getPermutation(int n) {
17+
List<String> result = new ArrayList<>();
18+
getPermAux(n, n, "", result);
19+
return result;
20+
}
21+
private static void getPermAux(int left, int right, String combination, List<String> collection) {
22+
if (left == 0 && right == 0) {
23+
collection.add(combination);
24+
}
25+
if (left > 0) {
26+
getPermAux(left - 1, right, combination + "(", collection);
27+
}
28+
if (right > left) {
29+
getPermAux(left, right - 1, combination + ")", collection);
30+
}
31+
}
32+
}

src/main/java/java8/StreamTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package java8;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
import java.util.stream.IntStream;
7+
8+
public class StreamTest {
9+
public static void main(String[] args) {
10+
List<Integer> list = Arrays.asList(3, 4, 5);
11+
System.out.println(list.stream().flatMap(i -> Arrays.asList(i, i).stream()).collect(Collectors.toList()));
12+
}
13+
}

0 commit comments

Comments
 (0)