Skip to content

Commit 4c9a3a9

Browse files
committed
Added 4 solutions
1 parent fb4ed36 commit 4c9a3a9

4 files changed

+150
-0
lines changed

Easy/Day of the Year.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
List<Integer> daysInMonth = Arrays.asList(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
3+
public int dayOfYear(String date) {
4+
int year = Integer.parseInt(date.split("-")[0]);
5+
int month = Integer.parseInt(date.split("-")[1]);
6+
int day = Integer.parseInt(date.split("-")[2]);
7+
8+
int numOfDays = getNumOfDays(month, isLeapYear(year));
9+
10+
return numOfDays + day;
11+
}
12+
13+
private boolean isLeapYear(int year) {
14+
if (year % 4 == 0) {
15+
if (year % 100 == 0) {
16+
return year % 400 == 0;
17+
}
18+
else {
19+
return true;
20+
}
21+
}
22+
return false;
23+
}
24+
25+
private int getNumOfDays(int month, boolean isLeapYear) {
26+
int numOfDays = 0;
27+
for (int i = 1; i < month; i++) {
28+
if (i == 2) {
29+
numOfDays += isLeapYear ? daysInMonth.get(i - 1) + 1 : daysInMonth.get(i - 1);
30+
}
31+
else {
32+
numOfDays += daysInMonth.get(i - 1);
33+
}
34+
}
35+
return numOfDays;
36+
}
37+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public int countCharacters(String[] words, String chars) {
3+
int goodWordsLength = 0;
4+
Map<Character, Integer> charsMap = getMap(chars);
5+
6+
for (String word : words) {
7+
Map<Character, Integer> wordMap = getMap(word);
8+
boolean allFound = true;
9+
for (Character character : wordMap.keySet()) {
10+
if (charsMap.getOrDefault(character, 0) < wordMap.get(character)) {
11+
allFound = false;
12+
break;
13+
}
14+
}
15+
16+
if (allFound) {
17+
goodWordsLength +=word.length();
18+
}
19+
}
20+
21+
22+
return goodWordsLength;
23+
}
24+
25+
private Map<Character, Integer> getMap(String s) {
26+
Map<Character, Integer> map = new HashMap<>();
27+
for (char c : s.toCharArray()) {
28+
map.put(c, map.getOrDefault(c, 0) + 1);
29+
}
30+
31+
return map;
32+
}
33+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public int maxDistance(int[][] grid) {
3+
if (grid.length == 0 || grid[0].length == 0) {
4+
return -1;
5+
}
6+
int[][] dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
7+
boolean[][] visited = new boolean[grid.length][grid[0].length];
8+
Queue<int[]> queue = new LinkedList<>();
9+
for (int i = 0; i < grid.length; i++) {
10+
for (int j = 0; j < grid[0].length; j++) {
11+
if (grid[i][j] == 1) {
12+
queue.add(new int[]{i, j});
13+
visited[i][j] = true;
14+
}
15+
}
16+
}
17+
int maxDist = -1;
18+
while (!queue.isEmpty()) {
19+
int size = queue.size();
20+
while (size-- > 0) {
21+
int[] curr = queue.remove();
22+
for (int[] dir : dirs) {
23+
int x = curr[0] + dir[0];
24+
int y = curr[1] + dir[1];
25+
if (x >= 0 && x < grid.length && y >= 0 && y < grid[0].length && !visited[x][y] && grid[x][y] == 0) {
26+
visited[x][y] = true;
27+
queue.add(new int[]{x, y});
28+
}
29+
}
30+
}
31+
maxDist++;
32+
}
33+
34+
return maxDist == 0 ? -1 : maxDist;
35+
}
36+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public int maxLevelSum(TreeNode root) {
12+
if (root == null) {
13+
return 0;
14+
}
15+
16+
Queue<TreeNode> queue = new LinkedList<>();
17+
queue.add(root);
18+
int maxSum = 0;
19+
int maxLevel = 1;
20+
int currLevel = 1;
21+
22+
while (!queue.isEmpty()) {
23+
int size = queue.size();
24+
int tempSum = 0;
25+
while (size-- > 0) {
26+
TreeNode removed = queue.remove();
27+
tempSum += removed.val;
28+
if (removed.left != null) {
29+
queue.add(removed.left);
30+
}
31+
if (removed.right != null) {
32+
queue.add(removed.right);
33+
}
34+
}
35+
if (maxSum < tempSum) {
36+
maxSum = tempSum;
37+
maxLevel = currLevel;
38+
}
39+
currLevel++;
40+
}
41+
42+
return maxLevel;
43+
}
44+
}

0 commit comments

Comments
 (0)