Skip to content

Commit fab52a1

Browse files
add 2689
1 parent 7088197 commit fab52a1

File tree

3 files changed

+109
-0
lines changed
  • paginated_contents/algorithms/3rd_thousand
  • src

3 files changed

+109
-0
lines changed

Diff for: paginated_contents/algorithms/3rd_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
| 2706 | [Buy Two Chocolates](https://leetcode.com/problems/buy-two-chocolates/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2706.java) | | Easy |
2626
| 2697 | [Lexicographically Smallest Palindrome](https://leetcode.com/problems/lexicographically-smallest-palindrome/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2697.java) | | Easy |
2727
| 2696 | [Minimum String Length After Removing Substrings](https://leetcode.com/problems/minimum-string-length-after-removing-substrings/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2696.java) | | Easy |
28+
| 2689 | [Extract Kth Character From The Rope Tree](https://leetcode.com/problems/extract-kth-character-from-the-rope-tree/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2689.java) | | Easy |
2829
| 2682 | [Find the Losers of the Circular Game](https://leetcode.com/problems/find-the-losers-of-the-circular-game/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2682.java) | | Easy |
2930
| 2673 | [Make Costs of Paths Equal in a Binary Tree](https://leetcode.com/problems/make-costs-of-paths-equal-in-a-binary-tree/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2673.java) | | Medium |Tree, DFS, Greedy
3031
| 2678 | [Number of Senior Citizens](https://leetcode.com/problems/number-of-senior-citizens/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2678.java) | | Easy |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.fishercoder.solutions.thirdthousand;
2+
3+
public class _2689 {
4+
// Definition for a rope tree node.
5+
public static class RopeTreeNode {
6+
public int len;
7+
public String val;
8+
public RopeTreeNode left;
9+
public RopeTreeNode right;
10+
11+
public RopeTreeNode() {
12+
}
13+
14+
public RopeTreeNode(String val) {
15+
this.len = 0;
16+
this.val = val;
17+
}
18+
19+
public RopeTreeNode(int len) {
20+
this.len = len;
21+
this.val = "";
22+
}
23+
24+
public RopeTreeNode(int len, RopeTreeNode left, RopeTreeNode right) {
25+
this.len = len;
26+
this.val = "";
27+
this.left = left;
28+
this.right = right;
29+
}
30+
}
31+
32+
public static class Solution1 {
33+
/**
34+
* My completely original solution.
35+
*/
36+
public char getKthCharacter(RopeTreeNode root, int k) {
37+
StringBuilder sb = new StringBuilder();
38+
postOrderToConcatenate(root, k, sb);
39+
return sb.charAt(k - 1);
40+
}
41+
42+
private void postOrderToConcatenate(RopeTreeNode root, int k, StringBuilder sb) {
43+
if (sb.length() >= k || root == null) {
44+
return;
45+
}
46+
postOrderToConcatenate(root.left, k, sb);
47+
postOrderToConcatenate(root.right, k, sb);
48+
sb.append(root.val);
49+
}
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.fishercoder.thirdthousand;
2+
3+
import com.fishercoder.solutions.thirdthousand._2689;
4+
import com.fishercoder.solutions.thirdthousand._2976;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
public class _2689Test {
11+
private static _2689.Solution1 solution1;
12+
13+
@BeforeEach
14+
public void setup() {
15+
solution1 = new _2689.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
_2689.RopeTreeNode root = new _2689.RopeTreeNode();
21+
root.len = 12;
22+
root.val = "";
23+
24+
_2689.RopeTreeNode rootLeft = new _2689.RopeTreeNode();
25+
rootLeft.len = 6;
26+
rootLeft.val = "";
27+
root.left = rootLeft;
28+
29+
_2689.RopeTreeNode rootLeftLeft = new _2689.RopeTreeNode();
30+
rootLeftLeft.len = 3;
31+
rootLeftLeft.val = "abc";
32+
rootLeft.left = rootLeftLeft;
33+
34+
_2689.RopeTreeNode rootLeftRight = new _2689.RopeTreeNode();
35+
rootLeftRight.len = 3;
36+
rootLeftRight.val = "efg";
37+
rootLeft.right = rootLeftRight;
38+
39+
_2689.RopeTreeNode rootRight = new _2689.RopeTreeNode();
40+
rootRight.len = 6;
41+
rootRight.val = "";
42+
root.right = rootRight;
43+
44+
_2689.RopeTreeNode rootRightLeft = new _2689.RopeTreeNode();
45+
rootRightLeft.len = 3;
46+
rootRightLeft.val = "hij";
47+
rootRight.left = rootRightLeft;
48+
49+
_2689.RopeTreeNode rootRightRight = new _2689.RopeTreeNode();
50+
rootRightRight.len = 3;
51+
rootRightRight.val = "klm";
52+
rootRight.right = rootRightRight;
53+
54+
55+
assertEquals('c', solution1.getKthCharacter(root, 3));
56+
}
57+
}

0 commit comments

Comments
 (0)