Skip to content

Commit 38ea709

Browse files
Merge branch 'neetcode-gh:main' into ginger
2 parents 5c28166 + c440eb2 commit 38ea709

File tree

75 files changed

+1764
-63
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+1764
-63
lines changed

Diff for: README.md

+20-20
Large diffs are not rendered by default.

Diff for: cpp/2390-removing-stars-from-a-string.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
string removeStars(string s) {
4+
stack<char> stk;
5+
for(int i=0;i<s.size();i++){
6+
if(s[i]=='*'){
7+
stk.pop();
8+
}else stk.push(s[i]);
9+
}
10+
string res = "";
11+
while(!stk.empty()){
12+
res += stk.top();
13+
stk.pop();
14+
}
15+
reverse(res.begin(), res.end());
16+
return res;
17+
}
18+
};

Diff for: csharp/0187-repeated-dna-sequences.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Solution
2+
{
3+
public IList<string> FindRepeatedDnaSequences(string s)
4+
{
5+
var seen = new HashSet<string>();
6+
var result = new HashSet<string>();
7+
8+
for (var i = 0; i < s.Length - 9; i++)
9+
{
10+
var cur = s.Substring(i, 10);
11+
if (seen.Contains(cur))
12+
result.Add(cur);
13+
seen.Add(cur);
14+
}
15+
16+
return result.ToList();
17+
}
18+
}

Diff for: csharp/0344-reverse-string.cs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class Solution
2+
{
3+
public void ReverseString(char[] s)
4+
{
5+
var h = s.Length / 2;
6+
for (int i = 0; i < h; i++)
7+
{
8+
var temp = s[i];
9+
s[i] = s[s.Length - i - 1];
10+
s[s.Length - i - 1] = temp;
11+
}
12+
}
13+
}

Diff for: csharp/0523-continuous-subarray-sum.cs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public class Solution
2+
{
3+
public bool CheckSubarraySum(int[] nums, int k)
4+
{
5+
var remainder = new Dictionary<int, int>();
6+
remainder.Add(0, -1);
7+
var total = 0;
8+
for (var i = 0; i < nums.Length; i++)
9+
{
10+
total += nums[i];
11+
var r = total % k;
12+
if (!remainder.ContainsKey(r))
13+
remainder.Add(r, i);
14+
else if (i - remainder[r] > 1)
15+
return true;
16+
}
17+
return false;
18+
}
19+
}

Diff for: csharp/1768-merge-strings-alternately.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Solution
2+
{
3+
public string MergeAlternately(string word1, string word2)
4+
{
5+
var result = string.Empty;
6+
var firstPointer = 0;
7+
var secondPointer = 0;
8+
while (firstPointer < word1.Length || secondPointer < word2.Length)
9+
{
10+
if (firstPointer < word1.Length)
11+
result += word1[firstPointer++];
12+
13+
if (secondPointer < word2.Length)
14+
result += word2[secondPointer++];
15+
}
16+
return result;
17+
}
18+
}

Diff for: dart/0746-min-cost-climbing-stairs.dart

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
int minCostClimbingStairs(List<int> cost) {
3+
for (int i = cost.length - 3; i >= 0; i--) {
4+
cost[i] += min(cost[i + 1], cost[i + 2]);
5+
}
6+
7+
return min(cost[0], cost[1]);
8+
}
9+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int removeDuplicates(int[] nums) {
3+
int k = 0;
4+
for (int num: nums) {
5+
if (k < 2 || nums[k-2] != num) {
6+
nums[k++] = num;
7+
}
8+
}
9+
return k;
10+
}
11+
}

Diff for: java/0110-balanced-binary-tree.java

+29
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,32 @@ public static boolean isBalanced(TreeNode root) {
2525
return dfs(root).getKey();
2626
}
2727
}
28+
29+
// Solution using the bottom up approach
30+
// TC and SC is On
31+
32+
class Solution {
33+
34+
public int height(TreeNode root){
35+
if(root == null){
36+
return 0;
37+
}
38+
39+
int lh = height(root.left);
40+
int rh = height(root.right);
41+
42+
return 1 + Math.max(lh,rh);
43+
}
44+
45+
public boolean isBalanced(TreeNode root) {
46+
47+
if(root == null){
48+
return true;
49+
}
50+
51+
int lh = height(root.left);
52+
int rh = height(root.right);
53+
54+
return Math.abs(lh - rh) <= 1 && isBalanced(root.left) && isBalanced(root.right);
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public Node connect(Node root) {
3+
Node current = root;
4+
Node next = root == null ? null : root.left;
5+
6+
while (current != null && next != null) {
7+
current.left.next = current.right;
8+
9+
if (current.next != null) {
10+
current.right.next = current.next.left;
11+
}
12+
13+
current = current.next;
14+
15+
if (current == null) {
16+
current = next;
17+
next = current.left;
18+
}
19+
}
20+
21+
return root;
22+
}
23+
}

Diff for: java/0147-insertion-sort-list.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public ListNode insertionSortList(ListNode head) {
3+
ListNode dummy = new ListNode(0, head);
4+
5+
ListNode prev = head;
6+
ListNode cur = head.next;
7+
while (cur != null) {
8+
if (prev.val <= cur.val) {
9+
prev = cur;
10+
cur = cur.next;
11+
} else {
12+
ListNode elem = dummy;
13+
while (elem.next.val < cur.val) {
14+
elem = elem.next;
15+
}
16+
17+
prev.next = cur.next;
18+
cur.next = elem.next;
19+
elem.next = cur;
20+
21+
cur = prev.next;
22+
}
23+
}
24+
25+
return dummy.next;
26+
}
27+
}

Diff for: java/0162-find-peak-element.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int findPeakElement(int[] nums) {
3+
int l = 0, r = nums.length-1;
4+
while(l<=r){
5+
int m = l + (r-l)/2;
6+
int left = (m-1==-1)?Integer.MIN_VALUE:nums[m-1];
7+
int right = (m+1==nums.length)?Integer.MIN_VALUE:nums[m+1];
8+
if(nums[m]>left && nums[m]>right) return m;
9+
else if(nums[m]<left) r=m-1;
10+
else l = m+1;
11+
}
12+
return 0;
13+
}
14+
}

Diff for: java/0226-invert-binary-tree.java

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ public TreeNode invertTree(TreeNode root) {
44
if (root == null) return null;
55
TreeNode node = new TreeNode(root.val);
66
node.right = invertTree(root.left);
7-
node.val = root.val;
87
node.left = invertTree(root.right);
98
return node;
109
}

Diff for: java/0304-range-sum-query-2d-immutable.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class NumMatrix {
2+
int[][] preSum;
3+
4+
public NumMatrix(int[][] matrix) {
5+
int m = matrix.length;
6+
int n = matrix[0].length;
7+
preSum = new int[m + 1][n + 1];
8+
9+
for (int i = 0; i < m; i++) {
10+
for (int j = 0; j < n; j++) {
11+
preSum[i + 1][j + 1] = preSum[i + 1][j] + preSum[i][j + 1] - preSum[i][j] + matrix[i][j];
12+
}
13+
}
14+
}
15+
16+
public int sumRegion(int row1, int col1, int row2, int col2) {
17+
return preSum[row2 + 1][col2 + 1] - preSum[row2 + 1][col1] - preSum[row1][col2 + 1] + preSum[row1][col1];
18+
}
19+
}
20+
21+
/**
22+
* Your NumMatrix object will be instantiated and called as such:
23+
* NumMatrix obj = new NumMatrix(matrix);
24+
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
25+
*/

Diff for: java/0446-arithmetic-slices-ii-subsequence.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class Solution {
2+
public int numberOfArithmeticSlices(int[] nums) {
3+
int res = 0;
4+
int n = nums.length;
5+
6+
Map<Long, Integer>[] dp = new HashMap[n];
7+
for (int i = 0; i < n; i++) {
8+
dp[i] = new HashMap<>();
9+
}
10+
11+
for (int i = 0; i < n; i++) {
12+
for (int j = 0; j < i; j++) {
13+
long diff = (long) nums[i] - (long) nums[j];
14+
dp[i].put(diff, dp[i].getOrDefault(diff, 0) + 1 + dp[j].getOrDefault(diff, 0));
15+
res += dp[j].getOrDefault(diff, 0);
16+
}
17+
}
18+
19+
return res;
20+
}
21+
}

Diff for: java/0455-assign-cookies.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int findContentChildren(int[] g, int[] s) {
3+
Arrays.sort(g);
4+
Arrays.sort(s);
5+
6+
int i = 0, j = 0;
7+
while(i < g.length){
8+
while(j < s.length && g[i] > s[j])
9+
j += 1;
10+
if(j == s.length)
11+
break;
12+
i += 1;
13+
j += 1;
14+
}
15+
return i;
16+
}
17+
}

Diff for: java/0661-image-smoother.java

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class Solution {
2+
public int[][] imageSmoother(int[][] img) {
3+
int ROWS = img.length, COLS = img[0].length;
4+
int[][] res = new int[ROWS][COLS];
5+
6+
for(int r = 0; r < ROWS; r++){
7+
for(int c = 0; c < COLS; c++){
8+
int total = 0, cnt = 0;
9+
for(int i = r - 1; i < r + 2; i++){
10+
for(int j = c - 1; j < c + 2; j++){
11+
if(i < 0 || i == ROWS || j < 0 || j == COLS)
12+
continue;
13+
total += img[i][j];
14+
cnt += 1;
15+
}
16+
}
17+
res[r][c] = total / cnt;
18+
}
19+
}
20+
return res;
21+
}
22+
}
23+
24+
// Optimal Solution
25+
26+
class Solution {
27+
public int[][] imageSmoother(int[][] img) {
28+
int ROWS = img.length, COLS = img[0].length;
29+
30+
for(int r = 0; r < ROWS; r++){
31+
for(int c = 0; c < COLS; c++){
32+
int total = 0, cnt = 0;
33+
34+
for(int i = r - 1; i < r + 2; i++){
35+
for(int j = c - 1; j < c + 2; j++){
36+
if(i < 0 || i == ROWS || j < 0 || j == COLS)
37+
continue;
38+
total += img[i][j] % 256;
39+
cnt += 1;
40+
}
41+
}
42+
img[r][c] = img[r][c] ^ (total / cnt) << 8;
43+
}
44+
}
45+
for(int r = 0; r < ROWS; r++){
46+
for(int c = 0; c < COLS; c++){
47+
img[r][c] = img[r][c] >> 8;
48+
}
49+
}
50+
return img;
51+
}
52+
}

Diff for: java/0704-binary-search.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ public int search(int[] nums, int target) {
55
int j = nums.length - 1;
66

77
while (i <= j) {
8-
int mid = (i + j) / 2;
8+
// mid is calculated this way to prevent integer overflow.
9+
// See: https://blog.research.google/2006/06/extra-extra-read-all-about-it-nearly.html
10+
int mid = i + (j - i) / 2;
911

1012
if (nums[mid] == target) return mid; else if (
1113
nums[mid] < target

Diff for: java/0872-leaf-similar-trees.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
3+
ArrayList<Integer> ls1 = new ArrayList<>();
4+
ArrayList<Integer> ls2 = new ArrayList<>();
5+
tree(root1, ls1);
6+
tree(root2, ls2);
7+
return ls1.equals(ls2);
8+
}
9+
private void tree(TreeNode root, List<Integer> ls){
10+
if(root == null)
11+
return;
12+
if(root.left == null && root.right == null)
13+
ls.add(root.val);
14+
tree(root.left, ls);
15+
tree(root.right, ls);
16+
}
17+
}

0 commit comments

Comments
 (0)