Skip to content

Commit ab1dc19

Browse files
committed
Solution Added by Achhuta Nand Jha (anjha)
1 parent 95e8f21 commit ab1dc19

File tree

24 files changed

+336
-0
lines changed

24 files changed

+336
-0
lines changed

Diff for: 0458.poor-pigs/PoorPigs.java

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class PoorPigs {
2+
public int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
3+
int testBuckets = minutesToTest / minutesToDie;
4+
int pigs = 0;
5+
while (Math.pow(testBuckets + 1, pigs) < buckets) {
6+
pigs++;
7+
}
8+
return pigs;
9+
}
10+
}

Diff for: 0458.poor-pigs/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [458. Poor Pigs](https://leetcode.com/problems/poor-pigs/)
2+

Diff for: 0461.hamming-distance/HammingDistance.java

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class HammingDistance {
2+
public int hammingDistance(int x, int y) {
3+
int z = x ^ y;
4+
int cnt = 0;
5+
while (z != 0) {
6+
if ((z & 1) == 1) cnt++;
7+
z = z >> 1;
8+
}
9+
return cnt;
10+
}
11+
}

Diff for: 0461.hamming-distance/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [461. Hamming Distance](https://leetcode.com/problems/hamming-distance/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import java.util.Arrays;
2+
3+
class MinimumMovesToEqualArrayElementsIi {
4+
public int minMoves2(int[] nums) {
5+
int l = nums.length;
6+
Arrays.sort(nums);
7+
int m = nums.length % 2 == 0 ? (nums[l / 2] + nums[l / 2 - 1]) / 2 : nums[l / 2];
8+
int steps = 0;
9+
for (int num : nums) {
10+
steps += Math.abs(num - m);
11+
}
12+
return steps;
13+
}
14+
}
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [462. Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/)
2+

Diff for: 0472.concatenated-words/ConcatenatedWords.java

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.List;
4+
5+
class ConcatenatedWords {
6+
WordTrie trie = new WordTrie();
7+
8+
public List<String> findAllConcatenatedWordsInADict(String[] words) {
9+
List<String> ans = new ArrayList<>();
10+
Arrays.sort(words, (a, b) -> a.length() - b.length());
11+
for (int i = 0; i < words.length; i++) {
12+
String word = words[i];
13+
if (word.length() == 0)
14+
continue;
15+
boolean[] visited = new boolean[word.length()];
16+
if (dfs(word, 0, visited))
17+
ans.add(word);
18+
else
19+
insert(word);
20+
}
21+
return ans;
22+
}
23+
24+
private boolean dfs(String word, int start, boolean[] visited) {
25+
if (word.length() == start)
26+
return true;
27+
if (visited[start])
28+
return false;
29+
visited[start] = true;
30+
WordTrie node = trie;
31+
for (int i = start; i < word.length(); i++) {
32+
char c = word.charAt(i);
33+
int index = c - 'a';
34+
node = node.children[index];
35+
if (node == null)
36+
return false;
37+
if (node.isEnd)
38+
if (dfs(word, i + 1, visited))
39+
return true;
40+
}
41+
return false;
42+
}
43+
44+
private void insert(String word) {
45+
WordTrie node = trie;
46+
for (int i = 0; i < word.length(); i++) {
47+
char c = word.charAt(i);
48+
int index = c - 'a';
49+
if (node.children[index] == null)
50+
node.children[index] = new WordTrie();
51+
node = node.children[index];
52+
}
53+
node. isEnd = true;
54+
}
55+
}
56+
57+
class WordTrie {
58+
WordTrie[] children;
59+
boolean isEnd;
60+
61+
public WordTrie() {
62+
children = new WordTrie[26];
63+
isEnd = false;
64+
}
65+
}

Diff for: 0472.concatenated-words/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# [472. Concatenated Words](https://leetcode.com/problems/concatenated-words/)
2+
3+
4+
5+
Complexity Analysis:
6+
7+
- Time Complexity: $O(n\log n + m)$. $n$ is the length of `words`. $m$ is the length of the longest string in the array `words`. Sort words costs $O(n\log n)$ and determine a word is concatenated word costs $O(m)$. The total time complexity is $O(n\log n + m)$
8+
- Space Complexity: $O(m\times n)$. We have $n$ words and the worst case is all words with $m$ length. Thus, the total space complexity is $O(m\times n)$.

Diff for: 0473.matchsticks-to-square/MatchsticksToSquare.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.util.Arrays;
2+
3+
class MatchsticksToSquare {
4+
private void reverse(int[] nums) {
5+
int left = 0;
6+
int right = nums.length - 1;
7+
while (left < right) {
8+
int tmp = nums[left];
9+
nums[left] = nums[right];
10+
nums[right] = tmp;
11+
left++;
12+
right--;
13+
}
14+
}
15+
16+
private boolean dfs(int[] nums, int[] sums, int index, int target) {
17+
if (index == nums.length) {
18+
return sums[0] == target && sums[1] == target && sums[2] == target;
19+
}
20+
for (int i = 0; i < 4; i++) {
21+
if (sums[i] + nums[index] > target) continue;
22+
sums[i] += nums[index];
23+
if (dfs(nums, sums, index + 1, target)) return true;
24+
sums[i] -= nums[index];
25+
}
26+
return false;
27+
}
28+
29+
public boolean makesquare(int[] matchsticks) {
30+
if (matchsticks == null || matchsticks.length < 4) return false;
31+
int l = matchsticks.length;
32+
int perimeter = 0;
33+
for (int i = 0; i < l; i++) {
34+
perimeter += matchsticks[i];
35+
}
36+
if (perimeter % 4 != 0) return false;
37+
Arrays.sort(matchsticks);
38+
reverse(matchsticks);
39+
return dfs(matchsticks, new int[4], 0, perimeter / 4);
40+
}
41+
}

Diff for: 0473.matchsticks-to-square/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [473. Matchsticks to Square](https://leetcode.com/problems/matchsticks-to-square/)
2+

Diff for: 0476.number-complement/NumberComplement.java

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class NumberComplement {
2+
public int findComplement(int num) {
3+
if (num == 0) return 1;
4+
int mask = 1 << 30;
5+
while ((mask & num) == 0) mask >>= 1;
6+
mask = (mask << 1) - 1;
7+
return num ^ mask;
8+
}
9+
}

Diff for: 0476.number-complement/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [476. Number Complement](https://leetcode.com/problems/number-complement/)
2+

Diff for: 0485.max-consecutive-ones/MaxConsecutiveOnes.java

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class MaxConsecutiveOnes {
2+
public int findMaxConsecutiveOnes(int[] nums) {
3+
int max = 0, cur = 0;
4+
for (int num : nums) {
5+
cur = num == 0 ? 0 : cur + 1;
6+
max = Math.max(max, cur);
7+
}
8+
return max;
9+
}
10+
}

Diff for: 0485.max-consecutive-ones/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [485. Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* public class TreeNode {
7+
* int val;
8+
* TreeNode left;
9+
* TreeNode right;
10+
* TreeNode() {}
11+
* TreeNode(int val) { this.val = val; }
12+
* TreeNode(int val, TreeNode left, TreeNode right) {
13+
* this.val = val;
14+
* this.left = left;
15+
* this.right = right;
16+
* }
17+
* }
18+
*/
19+
class FindModeInBinarySearchTree {
20+
private int curCnt = 1;
21+
private int maxCnt = 1;
22+
private TreeNode preNode = null;
23+
24+
private void inOrder(TreeNode node, List<Integer> nums) {
25+
if (node == null) return;
26+
inOrder(node.left, nums);
27+
if (preNode != null) {
28+
if (preNode.val == node.val) curCnt++;
29+
else curCnt = 1;
30+
}
31+
if (curCnt > maxCnt) {
32+
maxCnt = curCnt;
33+
nums.clear();
34+
nums.add(node.val);
35+
} else if (curCnt == maxCnt) {
36+
nums.add(node.val);
37+
}
38+
preNode = node;
39+
inOrder(node.right, nums);
40+
}
41+
42+
public int[] findMode(TreeNode root) {
43+
List<Integer> maxCntNums = new ArrayList<>();
44+
inOrder(root, maxCntNums);
45+
int[] ret = new int[maxCntNums.size()];
46+
int idx = 0;
47+
for (int num : maxCntNums) {
48+
ret[idx++] = num;
49+
}
50+
return ret;
51+
}
52+
}

Diff for: 0501.find-mode-in-binary-search-tree/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [501. Find Mode in Binary Search Tree](https://leetcode.com/problems/find-mode-in-binary-search-tree/)
2+

Diff for: 0502.ipo/Ipo.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.Arrays;
2+
import java.util.Comparator;
3+
import java.util.PriorityQueue;
4+
5+
class Ipo {
6+
public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
7+
int n = profits.length;
8+
int curr = 0;
9+
int[][] arr = new int[n][2];
10+
11+
for (int i = 0; i < n; i++) {
12+
arr[i][0] = capital[i];
13+
arr[i][1] = profits[i];
14+
}
15+
Arrays.sort(arr, Comparator.comparingInt(a -> a[0]));
16+
17+
PriorityQueue<Integer> pq = new PriorityQueue<>((x, y) -> y - x);
18+
for (int i = 0; i < k; i++) {
19+
while (curr < n && arr[curr][0] <= w) {
20+
pq.add(arr[curr][1]);
21+
curr++;
22+
}
23+
if (!pq.isEmpty())
24+
w += pq.poll();
25+
else
26+
break;
27+
}
28+
return w;
29+
}
30+
}

Diff for: 0502.ipo/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# [502. IPO](https://leetcode.com/problems/ipo/)
2+
3+
4+
5+
Complexity Analysis:
6+
7+
- Time Complexity: $O(n)$. $n$ is the length of `profits` and `capitals`. Sorting array costs $O(n\log n)$ and adding an element to heap and sorting heap costs $O(k\log n)$. The total time complexity is $O((n+k)\log n)$.
8+
- Space Complexity: $O(n)$.
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.Arrays;
2+
import java.util.Stack;
3+
4+
class NextGreaterElementIi {
5+
public int[] nextGreaterElements(int[] nums) {
6+
int n = nums.length;
7+
int[] next = new int[n];
8+
Arrays.fill(next, -1);
9+
Stack<Integer> pre = new Stack<>();
10+
for (int i = 0; i < 2 * n; i++) {
11+
int num = nums[i % n];
12+
while (!pre.isEmpty() && nums[pre.peek()] < num) {
13+
next[pre.pop()] = num;
14+
}
15+
if (i < n) {
16+
pre.push(i);
17+
}
18+
}
19+
return next;
20+
}
21+
}

Diff for: 0503.next-greater-element-ii/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [503. Next Greater Element II](https://leetcode.com/problems/next-greater-element-ii/)
2+

Diff for: 0509.fibonacci-number/FibonacciNumber.java

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class FibonacciNumber {
2+
public int fib(int n) {
3+
if (n == 0) return 0;
4+
if (n == 1) return 1;
5+
return fib(n - 1) + fib(n - 2);
6+
}
7+
}

Diff for: 0509.fibonacci-number/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [509. Fibonacci Number](https://leetcode.com/problems/fibonacci-number/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* public class TreeNode {
7+
* int val;
8+
* TreeNode left;
9+
* TreeNode right;
10+
* TreeNode() {}
11+
* TreeNode(int val) { this.val = val; }
12+
* TreeNode(int val, TreeNode left, TreeNode right) {
13+
* this.val = val;
14+
* this.left = left;
15+
* this.right = right;
16+
* }
17+
* }
18+
*/
19+
class FindBottomLeftTreeValue {
20+
public int findBottomLeftValue(TreeNode root) {
21+
Queue<TreeNode> queue = new LinkedList<>();
22+
queue.add(root);
23+
while (!queue.isEmpty()) {
24+
root = queue.poll();
25+
if (root.right != null) queue.add(root.right);
26+
if (root.left != null) queue.add(root.left);
27+
}
28+
return root.val;
29+
}
30+
}

Diff for: 0513.find-bottom-left-tree-value/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [513. Find Bottom Left Tree Value](https://leetcode.com/problems/find-bottom-left-tree-value/)
2+

0 commit comments

Comments
 (0)