Skip to content

Commit 0a08cad

Browse files
committed
Initial Commit
1 parent 914b8c4 commit 0a08cad

Some content is hidden

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

48 files changed

+2707
-0
lines changed

.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Eclipse
2+
.classpath
3+
.project
4+
.settings/
5+
6+
# Intellij
7+
.idea/
8+
*.iml
9+
*.iws
10+
11+
# Mac
12+
.DS_Store
13+
14+
# Maven
15+
log/
16+
target/

pom.xml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>leetcode</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<executions>
16+
<execution>
17+
<id>compile</id>
18+
<phase>compile</phase>
19+
<goals>
20+
<goal>compile</goal>
21+
</goals>
22+
</execution>
23+
<execution>
24+
<id>testCompile</id>
25+
<phase>test-compile</phase>
26+
<goals>
27+
<goal>testCompile</goal>
28+
</goals>
29+
</execution>
30+
</executions>
31+
<configuration>
32+
<source>8</source>
33+
<target>8</target>
34+
</configuration>
35+
</plugin>
36+
</plugins>
37+
</build>
38+
39+
<dependencies>
40+
<dependency>
41+
<groupId>org.openjdk.jmh</groupId>
42+
<artifactId>jmh-core</artifactId>
43+
<version>1.28</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.openjdk.jmh</groupId>
47+
<artifactId>jmh-generator-annprocess</artifactId>
48+
<version>1.28</version>
49+
</dependency>
50+
</dependencies>
51+
52+
53+
</project>

src/main/java/easy/BinaryTreeSum.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package easy;
2+
3+
// Binary Tree Represented As Array.
4+
public class BinaryTreeSum {
5+
public static String solution(long[] arr) {
6+
if (arr.length == 0 || arr.length == 1)
7+
return "";
8+
long left = sum2(arr, 1);
9+
long right = sum2(arr, 2);
10+
if (left == right)
11+
return "";
12+
if (left > right)
13+
return "Left";
14+
else
15+
return "Right";
16+
}
17+
18+
public static long sum2(long[] arr, int index) {
19+
long sum = 0;
20+
if (index > arr.length-1)
21+
return 0;
22+
else if (arr[index] == -1)
23+
return 0;
24+
else if (index < arr.length)
25+
sum += arr[index];
26+
sum += sum2(arr, 2 * index + 1) + sum2(arr, 2 * index + 2);
27+
return sum;
28+
29+
}
30+
31+
public static void main(String[] args) {
32+
long arr[] = new long[]{1, 10, 5, 1, 0, 6};
33+
System.out.println(solution(arr));
34+
}
35+
36+
}

src/main/java/easy/BuySellDP.java

Lines changed: 50 additions & 0 deletions
Large diffs are not rendered by default.

src/main/java/easy/CountPrimes.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package easy;
2+
3+
public class CountPrimes {
4+
5+
public static int countPrimes(int n) {
6+
boolean A[] = new boolean[n + 1];
7+
for (int i = 2; i < n; i++) {
8+
A[i] = true;
9+
}
10+
int p = 0;
11+
for (int i = 2; i < Math.sqrt(n); i++) {
12+
if (A[i]) {
13+
for (int j = i * i; j < n; j = (i * i + p * i)) {
14+
A[j] = false;
15+
p++;
16+
}
17+
}
18+
}
19+
int cnt = 0;
20+
for (int i = 2; i < n; i++) {
21+
if (A[i])
22+
cnt++;
23+
}
24+
return cnt;
25+
}
26+
27+
public static void main(String[] args) {
28+
System.out.println(countPrimes(10));
29+
}
30+
}

src/main/java/easy/HouseRobberDp.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package easy;
2+
3+
public class HouseRobberDp {
4+
5+
public int rob(int[] nums) {
6+
return robr(nums, 0);
7+
}
8+
9+
public int robr(int[] nums, int p) {
10+
if (p == nums.length - 1 || p == nums.length - 2)
11+
return nums[p];
12+
return Integer.max(nums[p] + robr(nums, p + 2), nums[p + 1] + robr(nums, p + 3));
13+
}
14+
15+
// Wrong Approach
16+
public int robrDp(int[] nums) {
17+
int dp[] = new int[nums.length];
18+
dp[0] = nums[0];
19+
dp[1] = nums[1];
20+
for (int i = 2; i < nums.length; i += 2) {
21+
dp[i] = nums[i] + dp[i - 2];
22+
}
23+
for (int j = 3; j < nums.length; j += 2) {
24+
dp[j] = nums[j] + dp[j - 2];
25+
}
26+
int max = dp[0];
27+
for (int i = 1; i < dp.length; i++) {
28+
if (dp[i] > max)
29+
max = dp[i];
30+
}
31+
return max;
32+
}
33+
34+
public int robrDp2(int[] nums) {
35+
if (nums.length == 0)
36+
return 0;
37+
else if (nums.length == 1)
38+
return nums[0];
39+
int dp[] = new int[nums.length];
40+
dp[0] = nums[0];
41+
dp[1] = nums[1];
42+
int max = Math.max(dp[0], dp[1]);
43+
for (int i = 2; i < nums.length; i++) {
44+
if ((i - 3) >= 0 && (i - 2) >= 0)
45+
dp[i] = nums[i] + ((dp[i - 2] > 0 && dp[i - 3] > 0) ? Integer.max(dp[i - 2], dp[i - 3]) : 0);
46+
else if ((i - 2) >= 0)
47+
dp[i] = nums[i] + ((dp[i - 2] > 0) ? dp[i - 2] : 0);
48+
max = Math.max(dp[i], max);
49+
}
50+
return max;
51+
}
52+
53+
public static void main(String[] args) {
54+
HouseRobberDp dp = new HouseRobberDp();
55+
System.out.println(dp.robrDp2(new int[]{1, 2, 3, 1}));
56+
System.out.println(dp.robrDp2(new int[]{2, 7, 9, 3, 1}));
57+
System.out.println(dp.robrDp2(new int[]{5, 2, 3, 8}));
58+
System.out.println(dp.robrDp2(new int[]{1,2}));
59+
}
60+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package easy;
2+
3+
public class IsSubsequenceDP {
4+
public static boolean isSubsequenceRecursive(String s, String t, int i) {
5+
if(s.isEmpty() || t.isEmpty())
6+
return false;
7+
else if(s.charAt(i) == t.charAt(i))
8+
return true;
9+
return isSubsequenceRecursive(s.substring(i+1, s.length()), t.substring(i+1, t.length()), i+1);
10+
}
11+
12+
public static void main(String[] args) {
13+
System.out.println(isSubsequenceRecursive("abc", "ahbgdc", 0));
14+
System.out.println(isSubsequenceRecursive("aec", "abcde", 0));
15+
}
16+
}

src/main/java/easy/Knapsack01.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package easy;
2+
3+
import java.util.List;
4+
5+
public class Knapsack01 {
6+
7+
8+
public static void main(String[] args) {
9+
10+
}
11+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package easy;
2+
3+
public class KthSymbolGrammerRecursive {
4+
5+
public static int kthGrammar(int N, int K) {
6+
if (N == 1) return 0;
7+
if (K % 2 == 0) {
8+
if (kthGrammar(N - 1, K / 2) == 0) return 1;
9+
else return 0;
10+
} else {
11+
if (kthGrammar(N - 1, (K + 1) / 2) == 0) return 0;
12+
else return 1;
13+
}
14+
}
15+
16+
public static int kthGrammarR(int n, int k) {
17+
StringBuilder sb = new StringBuilder();
18+
return kthGrammarRecursive(n).charAt(k - 1) - '0';
19+
}
20+
21+
// Got MLE Issue With below code
22+
// 30
23+
//434991989
24+
// 15/55 cases passed only
25+
public static StringBuilder kthGrammarRecursive(int n) {
26+
if (n == 1)
27+
return new StringBuilder("0");
28+
StringBuilder sbp = kthGrammarRecursive(n - 1);
29+
StringBuilder sbnew = new StringBuilder();
30+
for (int i = 0; i < sbp.length(); i++) {
31+
if (sbp.charAt(i) == '0')
32+
sbnew.append("01");
33+
else
34+
sbnew.append("10");
35+
}
36+
return sbnew;
37+
}
38+
39+
public static void main(String[] args) {
40+
System.out.println(kthGrammar(3, 3));
41+
System.out.println(kthGrammar(30, 434991989));
42+
}
43+
}

0 commit comments

Comments
 (0)