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

README.md

Lines changed: 20 additions & 20 deletions
Large diffs are not rendered by default.
Lines changed: 18 additions & 0 deletions
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+
};

csharp/0187-repeated-dna-sequences.cs

Lines changed: 18 additions & 0 deletions
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+
}

csharp/0344-reverse-string.cs

Lines changed: 13 additions & 0 deletions
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+
}
Lines changed: 19 additions & 0 deletions
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+
}
Lines changed: 18 additions & 0 deletions
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+
}
Lines changed: 9 additions & 0 deletions
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+
}
Lines changed: 11 additions & 0 deletions
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+
}

java/0110-balanced-binary-tree.java

Lines changed: 29 additions & 0 deletions
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+
}
Lines changed: 23 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)