Skip to content

Commit b62441e

Browse files
All code moved and renamed.
1 parent b18c4a5 commit b62441e

33 files changed

Lines changed: 166 additions & 114 deletions

14 Hashing and Maps/TwoSum.java renamed to 1 Leetcode Easy Problems/Leetcode 1 Two Sum using Hashmap.java

File renamed without changes.

17 DFS and BFS/PathSum.java renamed to 1 Leetcode Easy Problems/Leetcode 112 Path Sum.java

File renamed without changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public List<List<Integer>> generate(int numRows) {
5+
List<List<Integer>> result = new ArrayList<>();
6+
for (int i = 0; i < numRows; i++) {
7+
List<Integer> genRow = generateRow(i+1);
8+
result.add(genRow);
9+
}
10+
return result;
11+
}
12+
13+
private List<Integer> generateRow(int rowNumber) {
14+
if (rowNumber == 1) {
15+
List<Integer> firstRow = new ArrayList<>();
16+
firstRow.add(1);
17+
return firstRow;
18+
}
19+
20+
List<Integer> currRow = new ArrayList<>();
21+
List<Integer> prevRow = generateRow(rowNumber - 1);
22+
23+
currRow.add(1);
24+
for (int i = 1; i < rowNumber - 1; i++) {
25+
currRow.add(prevRow.get(i-1) + prevRow.get(i));
26+
}
27+
currRow.add(1);
28+
return currRow;
29+
}
30+
}

1 Leetcode Easy Problems/Leetcode 118 Pascals Triangle using Recursion.java renamed to 1 Leetcode Easy Problems/Leetcode 118 Pascals Triangle using Dynamic Programming.java

File renamed without changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int maxProfit(int[] prices) {
3+
int maxProfit = 0;
4+
int minPrice = Integer.MAX_VALUE;
5+
6+
for (int i = 0; i < prices.length; i++) {
7+
if (minPrice > prices[i]) {
8+
minPrice = prices[i];
9+
} else {
10+
int profit = prices[i] - minPrice;
11+
if (profit > maxProfit) {
12+
maxProfit = profit;
13+
}
14+
}
15+
}
16+
17+
return maxProfit;
18+
}
19+
}
Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
class Solution {
2-
public int maxProfit(int[] prices) {
3-
int maxProfit = 0;
4-
int minPrice = Integer.MAX_VALUE;
5-
6-
for (int i = 0; i < prices.length; i++) {
7-
if (minPrice > prices[i]) {
8-
minPrice = prices[i];
9-
} else {
10-
int profit = prices[i] - minPrice;
11-
if (profit > maxProfit) {
12-
maxProfit = profit;
13-
}
14-
}
15-
}
16-
17-
return maxProfit;
18-
}
2+
public int maxProfit(int[] prices) {
3+
int maxProfit = 0;
4+
for (int i = 0; i < prices.length - 1; i++) {
5+
for (int j = i + 1; j < prices.length; j++) {
6+
int profit = prices[j] - prices[i];
7+
if (profit > maxProfit) {
8+
maxProfit = profit;
9+
}
10+
}
11+
}
12+
return maxProfit;
13+
}
1914
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
private final String[][] SYMBOLS = new String[][]{
3+
{"I", "1"},
4+
{"V", "5"},
5+
{"X", "10"},
6+
{"L", "50"},
7+
{"C", "100"},
8+
{"D", "500"},
9+
{"M", "1000"},
10+
};
11+
12+
private int getSymbolValue(char symbol) {
13+
for (String[] row: SYMBOLS) {
14+
if (row[0].equals(symbol + "")) {
15+
return Integer.parseInt(row[1]);
16+
}
17+
}
18+
return 0;
19+
}
20+
21+
public int romanToInt(String s) {
22+
int result = 0;
23+
for (int i = 0; i < s.length(); i++) {
24+
char current = s.charAt(i);
25+
int currValue = getSymbolValue(current);
26+
27+
if (i + 1 < s.length()) {
28+
char next = s.charAt(i+1);
29+
int nextVal = getSymbolValue(next);
30+
if (nextVal <= currValue) {
31+
result += currValue;
32+
} else {
33+
result += (nextVal - currValue);
34+
i++;
35+
}
36+
} else {
37+
result += currValue;
38+
}
39+
}
40+
return result;
41+
}
42+
}

1 Leetcode Easy Problems/Leetcode 13 Roman to Integer.java renamed to 1 Leetcode Easy Problems/Leetcode 13 Roman to Integer using Hashmap.java

File renamed without changes.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int majorityElement(int[] nums) {
5+
Map<Integer, Integer> cardi = getCardinalityMap(nums);
6+
// Space: O(1), Time: O(n)
7+
for (Map.Entry<Integer, Integer> entry : cardi.entrySet()) {
8+
if (entry.getValue() > nums.length / 2) {
9+
return entry.getKey();
10+
}
11+
}
12+
return -1;
13+
}
14+
15+
// Space: O(n), Time: O(n)
16+
private Map<Integer, Integer> getCardinalityMap(int[] nums) {
17+
Map<Integer, Integer> cardi = new HashMap<>();
18+
for (int num: nums) {
19+
if (!cardi.containsKey(num)) {
20+
cardi.put(num, 1);
21+
} else {
22+
int currCount = cardi.get(num);
23+
cardi.put(num, currCount + 1);
24+
}
25+
}
26+
return cardi;
27+
}
28+
}

1 Leetcode Easy Problems/Leetcode 169 Majority Element.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)