Skip to content

Commit e8dcc45

Browse files
Merge branch 'neetcode-gh:main' into main
2 parents 4f8f824 + f9807f4 commit e8dcc45

20 files changed

+426
-4
lines changed

README.md

Lines changed: 4 additions & 4 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+
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+
}

java/0162-find-peak-element.java

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

java/0455-assign-cookies.java

Lines changed: 17 additions & 0 deletions
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+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*---------------------------
2+
Time Complexity: O(n)
3+
Space Complexity: O(1)
4+
-----------------------------*/
5+
class Solution {
6+
public int maxLengthBetweenEqualCharacters(String s) {
7+
Map<Character, Integer> char_index = new HashMap<>();
8+
int res = -1;
9+
10+
for(int i = 0; i < s.length(); i++){
11+
if(char_index.containsKey(s.charAt(i))){
12+
res = Math.max(res, i - char_index.get(s.charAt(i)) - 1);
13+
}
14+
else
15+
char_index.put(s.charAt(i), i);
16+
}
17+
return res;
18+
}
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public boolean makeEqual(String[] words) {
3+
Map<Character, Integer> map = new HashMap<>();
4+
5+
for(String s: words){
6+
for(char c: s.toCharArray()){
7+
map.put(c, map.getOrDefault(c, 0) + 1);
8+
}
9+
}
10+
int n = words.length;
11+
for(char c: map.keySet()){
12+
if(map.get(c) % n != 0)
13+
return false;
14+
}
15+
return true;
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int[] successfulPairs(
3+
int[] spells, int[] potions, long success) {
4+
int [] res = new int[spells.length];
5+
Arrays.sort(potions);
6+
for(int i=0;i<spells.length;i++){
7+
int spell = spells[i];
8+
int l = 0, r = potions.length-1;
9+
while(l<=r){
10+
int m = l+(r-l)/2;
11+
if((long)spell*potions[m]>=success) r=m-1;
12+
else l=m+1;
13+
}
14+
res[i] = (l<potions.length)?potions.length-l:0;
15+
}
16+
return res;
17+
}
18+
}

kotlin/0455-assign-cookies.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
fun findContentChildren(g: IntArray, s: IntArray): Int {
3+
g.sort()
4+
s.sort()
5+
6+
var i = 0
7+
var j = 0
8+
while (i < g.size) {
9+
while (j < s.size && g[i] > s[j]) j++
10+
if (j == s.size) break
11+
i++
12+
j++
13+
}
14+
15+
return i
16+
}
17+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// dp, space complexity down from O(n * t) down to O(t)
2+
class Solution {
3+
fun numRollsToTarget(n: Int, k: Int, t: Int): Int {
4+
val mod = 1_000_000_007
5+
var dp = LongArray (t + 1)
6+
7+
dp[0] = 1
8+
9+
for (d in 0 until n) {
10+
val nextDp = LongArray (t + 1)
11+
for (m in 1..k) {
12+
for (s in m..t)
13+
nextDp[s] = (nextDp[s] + dp[s - m]) % mod
14+
}
15+
dp = nextDp
16+
}
17+
18+
return dp[t].toInt()
19+
}
20+
}
21+
22+
// recursion + memo
23+
class Solution {
24+
fun numRollsToTarget(n: Int, k: Int, t: Int): Int {
25+
val mod = 1_000_000_007
26+
val dp = Array (n + 1) { LongArray (t + 1) { -1L } }
27+
28+
fun dfs(n: Int, s: Int): Long {
29+
if (n < 0 || s > t) return 0
30+
if (s == t && n == 0) return 1
31+
if (dp[n][s] != -1L) return dp[n][s]
32+
33+
var res = 0L
34+
for (m in 1..k)
35+
res = (res + dfs(n - 1, s + m)) % mod
36+
37+
dp[n][s] = res
38+
return dp[n][s]
39+
}
40+
41+
return (dfs(n, 0) % mod).toInt()
42+
}
43+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
fun minDifficulty(jobDifficulty: IntArray, d: Int): Int {
3+
if (jobDifficulty.size < d) return -1
4+
5+
val cache = HashMap<String, Int>()
6+
7+
fun dfs(i: Int, d: Int, curMax: Int): Int {
8+
if (i == jobDifficulty.size) {
9+
return if (d == 0) 0 else INFINITY
10+
}
11+
if (d == 0) return INFINITY
12+
cache["$i:$d:$curMax"]?.let { return it }
13+
14+
var max = maxOf(curMax, jobDifficulty[i])
15+
var res = minOf(
16+
dfs(i + 1, d, max),
17+
max + dfs(i + 1, d - 1, -1)
18+
)
19+
20+
cache["$i:$d:$max"] = res
21+
return res
22+
}
23+
24+
return dfs(0, d, -1)
25+
}
26+
27+
companion object {
28+
const val INFINITY = 400000
29+
}
30+
}

0 commit comments

Comments
 (0)