Skip to content

Commit fc90108

Browse files
Merge branch 'neetcode-gh:main' into main
2 parents 02ebbb1 + b3d096b commit fc90108

11 files changed

+278
-20
lines changed

README.md

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

java/0938-range-sum-of-bst.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*--------------------------
2+
Time Complexity: O(n)
3+
Space Complexity: O(n)
4+
---------------------------*/
5+
class Solution {
6+
public int rangeSumBST(TreeNode root, int low, int high) {
7+
if(root == null)
8+
return 0;
9+
if(root.val > high)
10+
return rangeSumBST(root.left, low, high);
11+
if(root.val < low)
12+
return rangeSumBST(root.right, low, high);
13+
return root.val + rangeSumBST(root.left, low, high) + rangeSumBST(root.right, low, high);
14+
}
15+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public String removeDuplicates(String s, int k) {
3+
Stack<Pair<Character, Integer>> stack = new Stack<>();
4+
char[] ss = s.toCharArray();
5+
6+
for (char current : ss) {
7+
if (!stack.isEmpty() && stack.peek().getKey() == current) {
8+
int topCharCount = stack.pop().getValue();
9+
stack.push(new Pair<>(current, topCharCount + 1));
10+
} else {
11+
stack.push(new Pair<>(current, 1));
12+
}
13+
14+
if (stack.peek().getValue() == k) {
15+
stack.pop();
16+
}
17+
}
18+
19+
StringBuilder result = new StringBuilder();
20+
21+
while (!stack.isEmpty()) {
22+
Pair<Character, Integer> poppedPair = stack.pop();
23+
for (int i = 0; i < poppedPair.getValue(); i++) {
24+
result.append(poppedPair.getKey());
25+
}
26+
}
27+
28+
return result.reverse().toString();
29+
}
30+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class Solution {
2+
int[][] intervals;
3+
Integer[] cache;
4+
5+
public int jobScheduling(int[] startTime, int[] endTime, int[] profit) {
6+
int n = startTime.length;
7+
intervals = new int[n][3];
8+
cache = new Integer[n];
9+
10+
for (int i = 0; i < n; i++) {
11+
intervals[i] = new int[]{startTime[i], endTime[i], profit[i]};
12+
}
13+
14+
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
15+
16+
return dfs(0);
17+
}
18+
19+
private int dfs(int i) {
20+
if (i == intervals.length) {
21+
return 0;
22+
}
23+
24+
if (cache[i] != null) {
25+
return cache[i];
26+
}
27+
28+
// don't include
29+
int res = dfs(i + 1);
30+
31+
// include
32+
int j = binarySearch(intervals, i, intervals[i][1]);
33+
cache[i] = res = Math.max(res, intervals[i][2] + dfs(j));
34+
35+
return res;
36+
}
37+
38+
private int binarySearch(int[][] intervals, int start, int target) {
39+
int left = start + 1;
40+
int right = intervals.length - 1;
41+
42+
while (left <= right) {
43+
int mid = left + (right - left) / 2;
44+
45+
if (intervals[mid][0] < target) {
46+
left = mid + 1;
47+
} else {
48+
right = mid - 1;
49+
}
50+
}
51+
52+
return left;
53+
}
54+
}

kotlin/0155-min-stack.kt

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
import kotlin.math.min
2-
31
class MinStack() {
4-
private class Node(var `val`: Int, var min: Int, var next: Node?)
5-
6-
private var head: Node? = null
7-
8-
fun push(x: Int) {
9-
if(head==null) head = Node(x,x,null) else head = Node(x, min(x,head?.min!!),head)
2+
private val stack = Stack<Int>()
3+
private val minStack = Stack<Int>()
4+
5+
fun push(`val`: Int) {
6+
val currentMin = if (minStack.isNotEmpty()) minStack.peek() else Integer.MAX_VALUE
7+
val newMin = minOf(currentMin, `val`)
8+
stack.push(`val`)
9+
minStack.push(newMin)
1010
}
1111

1212
fun pop() {
13-
head = head?.next
14-
13+
if (stack.isNotEmpty()) {
14+
stack.pop()
15+
minStack.pop()
16+
}
1517
}
1618

17-
fun top(): Int? {
18-
return head?.`val`
19-
19+
fun top(): Int {
20+
return stack.peek()
2021
}
2122

22-
fun getMin(): Int? {
23-
return head?.min
24-
23+
fun getMin(): Int {
24+
return minStack.peek()
2525
}
26-
2726
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// dp
2+
class Solution {
3+
fun numberOfArithmeticSlices(nums: IntArray): Int {
4+
var res = 0
5+
val dp = HashMap<Pair<Int, Long>, Int>()
6+
7+
for (i in 0 until nums.size) {
8+
for (j in 0 until i) {
9+
val d = nums[i].toLong() - nums[j]
10+
dp[i to d] = (dp[i to d] ?: 0) + 1 + (dp[j to d] ?: 0)
11+
res += (dp[j to d] ?: 0)
12+
}
13+
}
14+
15+
return res
16+
}
17+
}
18+
19+
// recursion + memoization
20+
class Solution {
21+
fun numberOfArithmeticSlices(nums: IntArray): Int {
22+
val dp = HashMap<String, Int>()
23+
val count = HashMap<Long, MutableList<Int>>()
24+
25+
nums.forEachIndexed { i, n ->
26+
count.getOrPut(n.toLong()) { mutableListOf() }.apply { add(i) }
27+
}
28+
29+
fun dfs(i: Int, d: Long, s: Int): Int {
30+
dp["$i:$d:$s"]?.let { return it }
31+
32+
var res = if (s > 2) 1 else 0
33+
count[nums[i] + d]?.forEach { j ->
34+
if (j > i) res += dfs(j, d, s + 1)
35+
}
36+
37+
dp["$i:$d:$s"] = res
38+
return res
39+
}
40+
41+
var res = 0
42+
for (i in 0 until nums.size) {
43+
for (j in i + 1 until nums.size)
44+
res += dfs(j, nums[j].toLong() - nums[i], 2)
45+
}
46+
47+
return res
48+
}
49+
}

kotlin/0938-range-sum-of-bst.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
fun rangeSumBST(root: TreeNode?, low: Int, high: Int): Int {
3+
root?: return 0
4+
5+
return if (root.`val` > high) rangeSumBST(root.left, low, high)
6+
else if (root.`val` < low) rangeSumBST(root.right, low, high)
7+
else root.`val` +
8+
rangeSumBST(root.left, low, high) +
9+
rangeSumBST(root.right, low, high)
10+
}
11+
}

0 commit comments

Comments
 (0)