Skip to content

Commit 00a2244

Browse files
committed
Added 2 solutions in Weekly Contest 113
1 parent c958385 commit 00a2244

2 files changed

Lines changed: 120 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 boolean flipEquiv(TreeNode root1, TreeNode root2) {
12+
if (root1 == null && root2 == null) {
13+
return true;
14+
}
15+
16+
if ((root1 != null && root2 == null) || (root1 == null && root2 != null)) {
17+
return false;
18+
}
19+
20+
if (root1.left == null && root1.right == null && (root2.left != null || root2.right != null)) {
21+
System.out.println("HERE1");
22+
return false;
23+
}
24+
25+
if ((root1.left != null || root1.right != null) && root2.left == null && root2.right == null) {
26+
System.out.println("HERE2");
27+
return false;
28+
}
29+
30+
if (root1.left == null && root1.right != null && root2.left != null && root2.right == null) {
31+
if (root1.right.val != root2.left.val) {
32+
return false;
33+
}
34+
35+
TreeNode temp = root1.right;
36+
root1.right = root1.left;
37+
root1.left = temp;
38+
}
39+
else if (root1.left != null && root1.right == null && root2.left == null && root2.right != null) {
40+
if (root1.left.val != root2.right.val) {
41+
return false;
42+
}
43+
44+
TreeNode temp = root1.right;
45+
root1.right = root1.left;
46+
root1.left = temp;
47+
}
48+
else if (root1.left != null && root1.right != null && root2.left != null && root2.right != null &&
49+
(root1.left.val == root2.right.val && root1.right.val == root2.left.val)) {
50+
TreeNode temp = root1.left;
51+
root1.left = root1.right;
52+
root1.right = temp;
53+
}
54+
else if (root1.left != null && root1.right != null && root2.left != null && root2.right != null &&
55+
((root1.left.val != root2.right.val && root1.right.val != root2.right.val) ||
56+
(root1.right.val != root2.left.val && root1.left.val != root2.left.val))) {
57+
System.out.println("HERE3");
58+
return false;
59+
}
60+
61+
return flipEquiv(root1.left, root2.left) && flipEquiv(root1.right, root2.right);
62+
}
63+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
class Solution {
2+
public String largestTimeFromDigits(int[] arr) {
3+
List<String> permutations = permuteUnique(arr);
4+
String maxStr = "";
5+
6+
for (String str : permutations) {
7+
if (isValid(str)) {
8+
if (maxStr.length() == 0) {
9+
maxStr = str;
10+
}
11+
else if (maxStr.compareTo(str) < 0) {
12+
maxStr = str;
13+
}
14+
}
15+
}
16+
17+
return maxStr.length() > 0 ? maxStr.substring(0, 2) + ":" + maxStr.substring(2) : maxStr;
18+
}
19+
20+
private boolean isValid(String str) {
21+
int left = Integer.parseInt(str.substring(0, 2));
22+
int right = Integer.parseInt(str.substring(2));
23+
24+
return left >= 0 && left <= 23 && right >= 0 && right <= 59;
25+
}
26+
27+
private List<String> permuteUnique(int[] nums) {
28+
List<String> ans = new ArrayList<>();
29+
Arrays.sort(nums);
30+
permuteHelper(nums, new ArrayList<>(), ans, new boolean[nums.length]);
31+
return new ArrayList<>(ans);
32+
}
33+
34+
private void permuteHelper(int[] nums, List<Integer> list, List<String> ans, boolean[] used) {
35+
if (list.size() == nums.length) {
36+
ans.add(list.stream().map(n -> String.valueOf(n)).collect(Collectors.joining("")));
37+
}
38+
else {
39+
for (int i=0; i<nums.length; i++) {
40+
// Choose
41+
if (used[i] || (i > 0 && nums[i] == nums[i-1] && !used[i-1])) {
42+
continue;
43+
}
44+
45+
used[i] = true;
46+
list.add(nums[i]);
47+
48+
// Explore
49+
permuteHelper(nums, list, ans, used);
50+
51+
// Un-choose
52+
used[i] = false;
53+
list.remove(list.size()-1);
54+
}
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)