Skip to content

Commit

Permalink
2023/11/26
Browse files Browse the repository at this point in the history
  • Loading branch information
baowj-678 committed Nov 26, 2023
1 parent 5eaf6ea commit d469d7b
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 2 deletions.
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions src/main/java/com/leetcode/1457.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.leetcode;

import com.sun.xml.internal.bind.v2.model.core.NonElement;
import sun.reflect.generics.tree.Tree;

import java.net.Inet4Address;
import java.util.HashMap;
import java.util.Map;

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution1457 {
int count;
public int pseudoPalindromicPaths (TreeNode root) {
this.count = 0;
dfs(root, new HashMap<>());
return this.count;
}

private void dfs(TreeNode root, HashMap<Integer, Integer> map) {
int v = 1;
if (map.containsKey(root.val)) {
v += map.get(root.val);
map.replace(root.val, v);
} else {
map.put(root.val, v);
}
if (root.left != null) {
dfs(root.left, map);
}
if (root.right != null) {
dfs(root.right, map);
}
if (root.left == null && root.right == null) {
int oddCnt = 0;
for (Map.Entry<Integer, Integer> entry: map.entrySet()) {
if (entry.getValue() % 2 == 1) {
oddCnt++;
}
if (oddCnt > 1) {
break;
}
}
if (oddCnt <= 1) {
this.count++;
}
}

if (v == 1) {
map.remove(root.val);
} else {
map.replace(root.val, v-1);
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/leetcode/2520.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.leetcode;

class Solution {
public int countDigits(int num) {
int copy = num;
int res = 0;
while (copy > 0) {
int tmp = copy % 10;
copy /= 10;
if (num % tmp == 0) {
res++;
}
}
return res;
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/leetcode/2586.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.leetcode;

import java.util.HashMap;
import java.util.HashSet;

class Solution2586 {
public int vowelStrings(String[] words, int left, int right) {
int res = 0;
HashSet<Character> vowel = new HashSet<>(5);
vowel.add('a');
vowel.add('e');
vowel.add('i');
vowel.add('o');
vowel.add('u');
for (int i = left; i <= right; i++) {
String s = words[i];
if (vowel.contains(s.charAt(0)) && vowel.contains(s.charAt(s.length()-1))) {
res++;
}
}
return res;
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/leetcode/2594.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.leetcode;

import java.util.Arrays;

class Solution2594 {
public long repairCars(int[] ranks, int cars) {
long l = 1, r = (long)Arrays.stream(ranks).min().getAsInt() * cars * cars;
while (l < r) {
long mid = (l + r) / 2;
if (this.check(ranks, cars, mid)) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}

private boolean check(int[] ranks, int cars, long time) {
for (int rank: ranks) {
int car = (int) Math.floor(Math.sqrt((double)time / rank));
cars -= car;
if (cars <= 0) {
return true;
}
}
return cars <= 0;
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/leetcode/2609.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.leetcode;

class Solution2609 {
public int findTheLongestBalancedSubstring(String s) {
int res = 0;
int[] cnt = new int[2];
for (char c: s.toCharArray()) {
switch (c) {
case '0':
if (cnt[1] > 0) {
cnt = new int[2];
}
cnt[0]++;
break;
case '1':
cnt[1]++;
if (cnt[0] >= cnt[1]) {
res = Math.max(res, cnt[1] * 2);
} else {
cnt = new int[2];
}
break;
}
}
return res;
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/leetcode/2760.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.leetcode;


class Solution2760 {
public int longestAlternatingSubarray(int[] nums, int threshold) {
int res = 0;
for (int l = 0; l < nums.length; l++) {
boolean isEven = true;
boolean isInclude = true;
for (int i = l; i < nums.length; i++) {
if (((nums[i] % 2 == 0) == isEven) && nums[i] <= threshold) {
isEven = !isEven;
} else {
res = Math.max(res, i - l);
isInclude = false;
break;
}
}
if (isInclude) {
res = Math.max(res, nums.length - l);
}

}
return res;
}

public static void main(String[] args) {
int[] nums = new int[]{3,2,5,4};
Solution2760 solution2760 = new Solution2760();
System.out.println(solution2760.longestAlternatingSubarray(nums, 5));
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/leetcode/807. 保持城市天际线.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
package com.leetcode;

class Solution {
class Solution807 {
public int maxIncreaseKeepingSkyline(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[] mDirect = new int[m];
Expand Down

0 comments on commit d469d7b

Please sign in to comment.