Skip to content

Commit 96835f1

Browse files
committed
update questions
1 parent a573d32 commit 96835f1

File tree

5 files changed

+232
-0
lines changed

5 files changed

+232
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ LeetCode Solution in Java
1616
- [Graph](#graph)
1717
- [Greedy](#greedy)
1818
- [Backtracking](#backtracking)
19+
- [Divide and Conquer](#divide-and-conquer)
1920
- [Dynamic Programming](#dynamic-programming)
2021
- [Trie](#trie)
2122
- [Design](#design)
@@ -39,6 +40,7 @@ LeetCode Solution in Java
3940

4041
| # | Title | Solution | Difficulty |
4142
|:-:|-|-|-|
43+
|136|[Single Number](https://leetcode.com/problems/single-number/)|[Java](bit/single-number/README.md)|Easy|
4244
|191|[Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/)|[Java](bit/number-of-1-bits/README.md)|Easy|
4345
|231|[Power of Two](https://leetcode.com/problems/power-of-two/)|[Java](bit/power-of-two/README.md)|Easy|
4446
|371|[Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/)|[Java](bit/sum-of-two-integers/README.md)|Medium|
@@ -86,13 +88,15 @@ LeetCode Solution in Java
8688
|345|[Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/)|[Java](array-string/reverse-vowels-of-a-string/README.md)|Easy|
8789
|349|[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/)|[Java](array-string/intersection-of-two-arrays/README.md)|Easy|
8890
|350|[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/)|[Java](array-string/intersection-of-two-arrays-ii/README.md)|Easy|
91+
|414|[Third Maximum Number (Classic)](https://leetcode.com/problems/third-maximum-number/)|[Java](array-string/third-maximum-number/README.md)|Easy|
8992
|442|[Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/)|[Java](array-string/find-all-duplicates-in-an-array/README.md)|Medium|
9093
|448|[Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/)|[Java](array-string/find-all-numbers-disappeared-in-an-array/README.md)|Easy|
9194
|459|[Repeated Substring Pattern](https://leetcode.com/problems/repeated-substring-pattern/)|[Java](array-string/repeated-substring-pattern/README.md)|Easy|
9295
|509|[Fibonacci Number](https://leetcode.com/problems/fibonacci-number/)|[Java](array-string/fibonacci-number/README.md)|Easy|
9396
|541|[Reverse String II](https://leetcode.com/problems/reverse-string-ii/)|[Java](array-string/reverse-string-ii/README.md)|Easy|
9497
|557|[Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/)|[Java](array-string/reverse-words-in-a-string-iii/README.md)|Easy|
9598
|605|[Can Place Flowers](https://leetcode.com/problems/can-place-flowers/)|[Java](array-string/can-place-flowers/README.md)|Easy|
99+
|628|[Maximum Product of Three Numbers](https://leetcode.com/problems/maximum-product-of-three-numbers/)|[Java](array-string/maximum-product-of-three-numbers/README.md)|Easy|
96100
|665|[Non-decreasing Array](https://leetcode.com/problems/non-decreasing-array/)|[Java](array-string/non-decreasing-array/README.md)|Medium|
97101
|852|[Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/)|[Java](array-string/peak-index-in-a-mountain-array/README.md)|Easy|
98102
|860|[Lemonade Change](https://leetcode.com/problems/lemonade-change/)|[Java](array-string/lemonade-change/README.md)|Easy|
@@ -237,6 +241,12 @@ LeetCode Solution in Java
237241
|216|[Combination Sum III](https://leetcode.com/problems/combination-sum-iii/)|[Java](backtracking/combination-sum-iii/README.md)|Medium|
238242
|437|[Path Sum III](https://leetcode.com/problems/path-sum-iii/)|[Java](backtracking/path-sum-iii/README.md)|Medium|
239243

244+
## Divide and Conquer
245+
246+
| # | Title | Solution | Difficulty |
247+
|:-:|-|-|-|
248+
|493|[Reverse Pairs (Classic)](https://leetcode.com/problems/reverse-pairs/)|[Java](divide-and-conquer/reverse-pairs/README.md)|Hard|
249+
240250
## Dynamic Programming
241251

242252
*Important*
@@ -264,6 +274,7 @@ LeetCode Solution in Java
264274
|1155|[Number of Dice Rolls With Target Sum](https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/)|[Java](dynamic-programming/number-of-dice-rolls-with-target-sum/README.md)|Medium|
265275

266276
## Trie
277+
267278
| # | Title | Solution | Difficulty |
268279
|:-:|-|-|-|
269280
|208|[Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree/)|[Java](trie/implement-trie-prefix-tree/README.md)|Medium|
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Maximum Product of Three Numbers
2+
3+
## Solution 1
4+
5+
```java
6+
/**
7+
* Question : 628. Maximum Product of Three Numbers
8+
* Complexity : Time: O(n) ; Space: O(1)
9+
* Topics : Array
10+
*/
11+
class Solution {
12+
public int maximumProduct(int[] nums) {
13+
int min1 = Integer.MAX_VALUE;
14+
int min2 = Integer.MAX_VALUE;
15+
int max1 = Integer.MIN_VALUE;
16+
int max2 = Integer.MIN_VALUE;
17+
int max3 = Integer.MIN_VALUE;
18+
19+
for (int i = 0; i < nums.length; i++) {
20+
int num = nums[i];
21+
22+
if (min1 > num) {
23+
min2 = min1;
24+
min1 = num;
25+
} else if (min2 > num) {
26+
min2 = num;
27+
}
28+
29+
if (num > max1) {
30+
max3 = max2;
31+
max2 = max1;
32+
max1 = num;
33+
} else if (num > max2) {
34+
max3 = max2;
35+
max2 = num;
36+
} else if (num > max3) {
37+
max3 = num;
38+
}
39+
}
40+
41+
return Math.max(max1 * max2 * max3, max1 * min1 * min2);
42+
}
43+
}
44+
```
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Third Maximum Number
2+
3+
## Solution 1
4+
5+
```java
6+
/**
7+
* Question : Find Three Largest Number
8+
* Complexity : Time: O(n) ; Space: O(1)
9+
*/
10+
public class Solution {
11+
public int thirdMax(int[] array) {
12+
PriorityQueue<Integer> pq = new PriorityQueue<>();
13+
14+
for (int i = 0; i < array.length; i++) {
15+
int curr = array[i];
16+
17+
if (pq.size() < 3 && !pq.contains(curr)) {
18+
pq.add(curr);
19+
continue;
20+
}
21+
22+
if (!pq.contains(curr) && curr > pq.peek()) {
23+
pq.remove();
24+
pq.add(curr);
25+
}
26+
}
27+
28+
if (pq.size() < 3) {
29+
while (pq.size() != 1) pq.remove();
30+
}
31+
32+
return pq.remove();
33+
}
34+
}
35+
```
36+
37+
## Solution 2
38+
39+
```java
40+
/**
41+
* Question : 414. Third Maximum Number
42+
* Complexity : Time: O(n) ; Space: O(1)
43+
* Topics : Array
44+
*/
45+
class Solution {
46+
public int thirdMax(int[] nums) {
47+
Integer first = null;
48+
Integer second = null;
49+
Integer third = null;
50+
51+
for (int i = 0; i < nums.length; i++) {
52+
int num = nums[i];
53+
54+
if ((first == null) || (num > first)) {
55+
third = second;
56+
second = first;
57+
first = num;
58+
} else if (num != first && (second == null || num > second)) {
59+
third = second;
60+
second = num;
61+
} else if ((num != first && num != second) && (third == null || num > third)) {
62+
third = num;
63+
}
64+
}
65+
66+
if (third != null) {
67+
return third;
68+
}
69+
return first;
70+
}
71+
}
72+
```

bit/single-number/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Single Number
2+
3+
## Solution 1
4+
5+
```java
6+
/**
7+
* Question : 136. Single Number
8+
* Complexity : Time: O(n) ; Space: O(1)
9+
* Topics : Bit
10+
*/
11+
class Solution {
12+
public int singleNumber(int[] nums) {
13+
int res = nums[0];
14+
for (int i = 1; i < nums.length; i++) {
15+
res ^= nums[i];
16+
}
17+
return res;
18+
}
19+
}
20+
```
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Count Inversions in an array
2+
3+
## Solution 1
4+
5+
Brute Force
6+
7+
```java
8+
public class Solution {
9+
public int reversePairs(int[] nums) {
10+
int count = 0;
11+
int len = nums.length;
12+
for (int i = 0; i < len - 1; i++) {
13+
for (int j = i + 1; j < len; j++) {
14+
if (nums[i] > nums[j]) {
15+
count++;
16+
}
17+
}
18+
}
19+
return count;
20+
}
21+
}
22+
```
23+
24+
## Solution 2
25+
26+
Merge Sort
27+
28+
```java
29+
/**
30+
* Question : Count Inversions in an array
31+
* Complexity : Time: O(nlog(n)) ; Space: O(1)
32+
* Topics : Divide and Conquer
33+
*/
34+
public class Solution {
35+
public int reversePairs(int[] arr) {
36+
if (arr == null || arr.length == 0) {
37+
return 0;
38+
}
39+
return mergeSort(arr, 0, arr.length - 1);
40+
}
41+
42+
public int mergeSort(int[] arr, int low, int high) {
43+
if (low == high) {
44+
return 0;
45+
}
46+
47+
int mid = low + (high - low) / 2;
48+
49+
int leftCount = mergeSort(arr, low, mid);
50+
int rightCount = mergeSort(arr, mid + 1, high);
51+
52+
return leftCount + rightCount + merge(arr, low, mid, high);
53+
}
54+
55+
public int merge(int[] arr, int low, int mid, int high) {
56+
int[] merged = new int[high - low + 1];
57+
58+
int i = low, j = mid + 1, k = 0;
59+
60+
int inversion = 0;
61+
62+
while (i <= mid && j <= high) {
63+
if (arr[i] <= arr[j]) {
64+
merged[k++] = arr[i++];
65+
} else {
66+
merged[k++] = arr[j++];
67+
inversion += mid - i + 1;
68+
}
69+
}
70+
while (i <= mid) {
71+
merged[k++] = arr[i++];
72+
}
73+
while (j <= high) {
74+
merged[k++] = arr[j++];
75+
}
76+
77+
k = 0;
78+
for (; k < merged.length; k++) {
79+
arr[low + k] = merged[k];
80+
}
81+
82+
return inversion;
83+
}
84+
}
85+
```

0 commit comments

Comments
 (0)