First 12 rows of java
| uuidstring | contentstring | content_formatstring | raw_contentstring | taskstring | analysisstring | solutionstring | teststring | full_contentstring | full_content_formatstring |
|---|---|---|---|---|---|---|---|---|---|
| 8d317d84-23f5-4b8f-a0a8-466309da7865 | Write a Java method that counts the number of fully white (all 0s) and fully blue (all 1s) square subregions in a square binary matrix using a divide-and-conquer quad-tree approach. Given a square matrix of size n (where n is a power of 2) containing only 0s and 1s, the method mu… | no fence | import java.util.*; import java.io.*; public class Main { private static int read() throws Exception { int c, n = System.in.read() & 15; while ((c = System.in.read()) >= 48) n = (n << 3) + (n << 1) + (c & 15); return n; } static int n, white = 0, blue = 0; static… |
| Write a Java method that counts the number of fully white (all 0s) and fully blue (all 1s) square subregions in a square binary matrix using a divide-and-conquer quad-tree approach. Given a square matrix of size n (where n is a power of 2) containing only 0s and 1s, the method mu… |
| The solution uses a recursive divide-and-conquer approach similar to quadtree decomposition. The main algorithm works as follows: For a given submatrix starting at position (row, col) with size `size`, we first sum all elements within that submatrix. If the sum equals `size * siz… |
| import java.util.Arrays; class UniformRegionCounter { /** * Counts the number of uniform white (0) and blue (1) subregions using * divide-and-conquer on a square binary matrix whose size is a power of 2. * * @param matrix a non-empty square matrix contain… |
| class Main { public static void main(String[] args) { // Test 1: 1x1 all white int[] r1 = UniformRegionCounter.countUniformRegions(new int[][]{{0}}); assert r1[0] == 1 && r1[1] == 0 : "Test 1 failed"; // Test 2: 1x1 all blue int[] r2 =… |
| // Write a Java method that counts the number of fully white (all 0s) and fully blue (all 1s) square subregions in a square binary matrix using a divide-and-conquer quad-tree approach. Given a square matrix of size n (where n is a power of 2) containing only 0s and 1s, the method… |
| test-first order |
| 79341f3b-5e11-4567-9104-b94ecfe0ee8b | Write a Java method named `generateNumberPyramid(int n)` that accepts a single positive integer `n` (where `n >= 1`) and returns a `String` representing a triangular pattern of numbers. The pattern must consist of `n` rows. For each row `i` (where `i` goes from `1` to `n`), the r… | markdown | package com.class9; public class Task1 { public static void main(String[] args) { for (int i=5; i>=1; i--) { for (int j=1; j<=i; j++) { System.out.print(i); } System.out.println(); } //task2 for (int i=1; i<=5; i++) {//rows for (int j=1; j<=i; j++)… | Write a Java method named `generateNumberPyramid(int n)` that accepts a single positive integer `n` (where `n >= 1`) and returns a `String` representing a triangular pattern of numbers. The pattern must consist of `n` rows. For each row `i` (where `i` goes from `1` to `n`), the r… | The solution uses nested loops: an outer loop iterating over row numbers `i` from `1` to `n`, and an inner loop that iterates `i` times to append the character representation of `i` (since `i` can be multi-digit for `n >= 10`, we must convert each digit properly). Instead of buil… | class NumberPyramid { /** * Generates a triangular pattern where each row i (1-indexed) contains the digit(s) * of i repeated i times. Each row ends with a newline. * * @param n the number of rows; must be >= 1 * @return the pattern as a string, or em… | class Main { public static void main(String[] args) { assert NumberPyramid.generateNumberPyramid(1).equals("1\n"); assert NumberPyramid.generateNumberPyramid(2).equals("1\n22\n"); assert NumberPyramid.generateNumberPyramid(4).equals("1\n22\n333\n4444\n… | Write a Java method named `generateNumberPyramid(int n)` that accepts a single positive integer `n` (where `n >= 1`) and returns a `String` representing a triangular pattern of numbers. The pattern must consist of `n` rows. For each row `i` (where `i` goes from `1` to `n`), the r… | standard order |
| efdf821b-91d9-48fc-98bf-3058d81a49c7 | /* Write a Java method `mergeSortedArrays` that takes two integer arrays, both already sorted in ascending order, and returns a new integer array containing all elements from both arrays merged into a single ascending sorted order. The method should handle empty arrays, arrays of… | code comment | /** * 병합 알고리즘 Merge Algorithm * 2개의 정수 배열 합치기, 단 2개의 배열은 오름차순으로 정렬되어있음 */ public class MergeAlgorithm { public static void main(String[] args) { //input int[] data1 = {1, 3, 5}; int[] data2 = {2, 4}; int M = data1.length; int N = data2.leng… | Write a Java method `mergeSortedArrays` that takes two integer arrays, both already sorted in ascending order, and returns a new integer array containing all elements from both arrays merged into a single ascending sorted order. The method should handle empty arrays, arrays of un… | The solution uses the classic two-pointer merge technique: initialize an index `i` for the first array, `j` for the second, and `k` for the result array. While both `i` and `j` are within bounds, compare the current elements; place the smaller one into the result at position `k`,… | class MergeArrayTool { /** * Merges two ascending-sorted integer arrays into one ascending-sorted array. * * @param data1 the first sorted array (must be in non-decreasing order) * @param data2 the second sorted array (must be in non-decreasing order) … | class Main { public static void main(String[] args) { // Standard case int[] result1 = MergeArrayTool.mergeSortedArrays(new int[]{1, 3, 5}, new int[]{2, 4}); assert java.util.Arrays.equals(result1, new int[]{1, 2, 3, 4, 5}) : "Standard merge failed"; … | // Write a Java method `mergeSortedArrays` that takes two integer arrays, both already sorted in ascending order, and returns a new integer array containing all elements from both arrays merged into a single ascending sorted order. The method should handle empty arrays, arrays of… | standard order |
| fb915630-426f-402c-94c0-9c9825b9f179 | Given an array of non-negative integers where each element represents the maximum jump length from that position, write a Java method that determines whether you can reach the last index starting from the first index. The method should take an array of integers as input and retur… | no fence | package 精刷400.array; class Solution55 { public boolean canJump(int[] nums) { int n = nums.length; int rightmost = 0; for (int i = 0; i < n; i++) { if (i <= rightmost) { rightmost = Math.max(rightmost, i + nums[i]); … | Given an array of non-negative integers where each element represents the maximum jump length from that position, write a Java method that determines whether you can reach the last index starting from the first index. The method should take an array of integers as input and retur… | The solution uses a greedy approach with a single pass. We maintain a variable `rightmost` that tracks the farthest index reachable so far. We iterate through the array from index 0 to n-1. At each position `i`, if `i` is within the current reachable range (`i <= rightmost`), we … | class JumpGame { /** * Determines whether the last index of the array is reachable from the first index, * where each element represents the maximum jump length from that position. * * @param nums an array of non-negative integers representing maximum jump… | class Main { public static void main(String[] args) { // Basic reachable cases assert JumpGame.canJump(new int[]{2,3,1,1,4}) == true; assert JumpGame.canJump(new int[]{3,2,1,0,4}) == false; // Single element array assert JumpGa… | /* Given an array of non-negative integers where each element represents the maximum jump length from that position, write a Java method that determines whether you can reach the last index starting from the first index. The method should take an array of integers as input and re… | solution-first order |
| 892efee2-0e0d-42f9-bc65-e6767d5c918a | /* Write a Java method that takes a string of decimal digits (0-9) as input and returns a new string where each digit is wrapped in parentheses according to nesting depth. The nesting depth for each digit equals its numeric value: a digit '3' must be surrounded by exactly 3 openi… | code comment | import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.HashSet; import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); // … | Write a Java method that takes a string of decimal digits (0-9) as input and returns a new string where each digit is wrapped in parentheses according to nesting depth. The nesting depth for each digit equals its numeric value: a digit '3' must be surrounded by exactly 3 opening … | The main algorithm is a simple state-machine simulation. We maintain an integer `level` representing the current nesting depth. We iterate through each character of the input string, converting it to an integer `target` (by subtracting '0'). While `level < target`, we append '(' … | class ParenthesisNesting { /** * Returns a string where each digit is nested in parentheses according to its numeric value, * sharing parentheses when depth changes gradually. * * @param digits a non-empty string containing only characters '0' to '9' … | class Main { public static void main(String[] args) { assert ParenthesisNesting.nest("0000").equals("0000"); assert ParenthesisNesting.nest("101").equals("(1)0(1)"); assert ParenthesisNesting.nest("111000").equals("(111)000"); assert Parenthesi… | // Write a Java method that takes a string of decimal digits (0-9) as input and returns a new string where each digit is wrapped in parentheses according to nesting depth. The nesting depth for each digit equals its numeric value: a digit '3' must be surrounded by exactly 3 openi… | test-first order |
| ed2263a8-0657-464d-b0f2-3faa5a0b781f | /* Write a Java method that counts the number of "good nodes" in a binary tree. A node X is considered "good" if, on the path from the root to X, there are no nodes with a value greater than X’s value (i.e., X’s value is the maximum along that root-to-node path). The method shoul… | code comment | /** * 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;… | Write a Java method that counts the number of "good nodes" in a binary tree. A node X is considered "good" if, on the path from the root to X, there are no nodes with a value greater than X’s value (i.e., X’s value is the maximum along that root-to-node path). The method should t… | The algorithm uses a depth-first search (DFS) traversal. Starting from the root, we maintain the maximum value encountered so far along the current path. At each node, we compare the node’s value to the current maximum: if the node’s value is greater than or equal to the current … | class GoodNodesCounter { /** * Counts the number of good nodes in a binary tree. * A node is good if its value is greater than or equal to all values * on the path from the root to that node. * * @param root the root of the binary tree (may be null) … | class Main { public static void main(String[] args) { // Test 1: Empty tree assert GoodNodesCounter.countGoodNodes(null) == 0; // Test 2: Single node GoodNodesCounter.TreeNode single = new GoodNodesCounter.TreeNode(5); assert G… | // Write a Java method that counts the number of "good nodes" in a binary tree. A node X is considered "good" if, on the path from the root to X, there are no nodes with a value greater than X’s value (i.e., X’s value is the maximum along that root-to-node path). The method shoul… | test-first order |
| 727bbaac-8802-4893-873e-f9b815a4fb6a | Write a Java method that takes a string `s` and returns the length of the longest substring that contains no repeated characters. The substring must consist of consecutive characters from the original string. The input string may be empty, contain only one character, consist enti… | no fence | package com.code.basic.algorithm.hash; import java.util.HashMap; /** * 无重复字符的最长子串(滑动窗口) * <p> * 给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串的长度。 * <p> * 示例 1: * 输入: s = "abcabcbb" * 输出: 3 * 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。 * <p> * 示例 2: * 输入: s = "bbbbb" * 输出: 1 * 解释: 因为无重复字符的最… | Write a Java method that takes a string `s` and returns the length of the longest substring that contains no repeated characters. The substring must consist of consecutive characters from the original string. The input string may be empty, contain only one character, consist enti… | The core idea is to maintain a sliding window defined by a left boundary (`start`) and a right boundary (the current index `i`) that ensures the substring from `start` to `i` contains no duplicate characters. As we iterate through the string, we use a `HashMap` to store the most … | package com.task.substring; import java.util.HashMap; import java.util.Map; class LongestSubstring { /** * Returns the length of the longest substring without repeating characters. * * @param s the input string (may be null, if null treated as empty) * @… | class Main { public static void main(String[] args) { // Basic examples from the problem statement assert com.task.substring.LongestSubstring.lengthOfLongestSubstring("abcabcbb") == 3; assert com.task.substring.LongestSubstring.lengthOfLongestSubstring… | Write a Java method that takes a string `s` and returns the length of the longest substring that contains no repeated characters. The substring must consist of consecutive characters from the original string. The input string may be empty, contain only one character, consist enti… | commented standard order |
| b1dafb7c-2c57-41f0-af5c-73ce82596ba0 | /* Write a Java method that determines whether a given string `s` contains only correctly ordered and balanced parentheses, where every opening parenthesis `'('` has a matching closing parenthesis `')'` that appears later in the string, and the parentheses are properly nested. Th… | code comment | package programmers; public class Solution_86 { // 올바른 괄호 boolean solution(String s) { boolean answer = false; int cnt = 0; for(int i = 0; i < s.length(); i++) { if(s.charAt(i) == '(') cnt++; if(s.charAt(i) == ')') cnt--; … | Write a Java method that determines whether a given string `s` contains only correctly ordered and balanced parentheses, where every opening parenthesis `'('` has a matching closing parenthesis `')'` that appears later in the string, and the parentheses are properly nested. The s… | The solution uses a counter that tracks the balance of parentheses. Iterate through each character of the string: increment the counter by 1 for every `'('` and decrement by 1 for every `')'`. If at any point the counter becomes negative, it means there is a closing parenthesis w… | class ParenthesesValidator { /** * Checks whether parentheses in the given string are balanced and correctly ordered. * Non-parenthesis characters are ignored. * * @param s the input string to check * @return true if parentheses are balanced and corre… | class Main { public static void main(String[] args) { assert ParenthesesValidator.isValidParentheses("") == true; assert ParenthesesValidator.isValidParentheses("()") == true; assert ParenthesesValidator.isValidParentheses("(())") == true; asse… | Write a Java method that determines whether a given string `s` contains only correctly ordered and balanced parentheses, where every opening parenthesis `'('` has a matching closing parenthesis `')'` that appears later in the string, and the parentheses are properly nested. The s… | commented standard order |
| e283c2bd-bdda-48a8-8f18-b20378027883 | Implement a Java method that finds the k-th smallest element in a Binary Search Tree (BST). You are given a BST where each node contains an integer value. Write a public static method that takes the root of the BST and an integer k (1-indexed) and returns the k-th smallest value … | no fence | package core.tiktok.trees.bst; import java.util.ArrayList; import java.util.List; //Definition for a binary tree node. class TreeNode { int val; TreeNode_ left; TreeNode_ right; TreeNode() {} TreeNode(int val) { this.val = val; } TreeNode(int val,… | Implement a Java method that finds the k-th smallest element in a Binary Search Tree (BST). You are given a BST where each node contains an integer value. Write a public static method that takes the root of the BST and an integer k (1-indexed) and returns the k-th smallest value … | The most straightforward approach is to exploit the BST property that an in-order traversal visits nodes in ascending order. Perform a recursive in-order traversal (left subtree → current node → right subtree) and collect all node values into a list. Since in-order traversal of a… | import java.util.ArrayList; import java.util.List; class KthSmallestInBST { /** * Finds the k-th smallest element in a Binary Search Tree. * Uses in-order traversal to collect sorted node values. * * @param root the root of the BST (non-null) * @para… | class Main { public static void main(String[] args) { // Test 1: Simple 3-node BST (root = 2, left = 1, right = 3) KthSmallestInBST.TreeNode root1 = new KthSmallestInBST.TreeNode(2); root1.left = new KthSmallestInBST.TreeNode(1); root1.right = … | Implement a Java method that finds the k-th smallest element in a Binary Search Tree (BST). You are given a BST where each node contains an integer value. Write a public static method that takes the root of the BST and an integer k (1-indexed) and returns the k-th smallest value … | standard order |
| e4913b51-8ac4-432c-b2f7-c077ceebee99 | /* You are given an array of positions along a number line from 0 to 100, where at most one cow (either Holstein 'H' or Guernsey 'G') can stand at each integer position. The input starts with an integer `n` (1 ≤ n ≤ 100) representing the number of cows, followed by `n` lines, eac… | code comment | import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] lst = new int[101]; for (int i = 0; i < n; i++){ int place = sc.nextInt(); … | You are given an array of positions along a number line from 0 to 100, where at most one cow (either Holstein 'H' or Guernsey 'G') can stand at each integer position. The input starts with an integer `n` (1 ≤ n ≤ 100) representing the number of cows, followed by `n` lines, each c… | The core idea is to simulate the given logic: first, map each position to a value—1 for Holstein, 2 for Guernsey—in an array of size 101 (indices 0..100). Then, for every starting position `i` that has a cow (array value > 0), we iterate forward to every position `j` from `i` to … | import java.util.Scanner; class CowSegmentLength { /** * Computes the maximum length of a contiguous segment where either the number * of Holsteins equals the number of Guernseys, or only one breed is present. * * @param n the number of cows * @… | class Main { public static void main(String[] args) { // Test 1: Equal counts example with positions 0(H),2(G) -> segment [0,2] length 2 assert CowSegmentLength.maxSegmentLength(2, new int[]{0, 2}, new char[]{'H', 'G'}) == 2; // Test 2: Single breed sc… | // You are given an array of positions along a number line from 0 to 100, where at most one cow (either Holstein 'H' or Guernsey 'G') can stand at each integer position. The input starts with an integer `n` (1 ≤ n ≤ 100) representing the number of cows, followed by `n` lines, eac… | commented standard order |
| 96f5e052-7b72-4b76-b436-6815aa194dfe | /* Write a Java method that encodes a list of strings into a single string by prefixing each string with its length followed by a '#' character, and also provides a method to decode such an encoded string back into the original list of strings. The encoding must be unambiguous: g… | code comment | package Codec; import java.util.ArrayList; import java.util.List; class Solution{ public static void main(String[] args) { List<String> strs = java.util.Arrays.asList("sreeram","charagundla","modda","guduv"); String s = encode(strs); System.out.println(… | Write a Java method that encodes a list of strings into a single string by prefixing each string with its length followed by a '#' character, and also provides a method to decode such an encoded string back into the original list of strings. The encoding must be unambiguous: give… | The core idea is to use a length-prefix encoding: for each string, we append its length (as a decimal integer) followed by a delimiter `#` and then the raw string itself. For example, the list `["abc", "", "x#y"]` encodes to `"3#abc0#3#x#y"`. To decode, we scan the encoded string… | import java.util.ArrayList; import java.util.List; class StringCodec { /** * Encodes a list of strings into a single string. * Format: for each string s, append s.length() + "#" + s consecutively. * * @param strs the list of strings to encode, may contain… | class Main { public static void main(String[] args) { // Basic round-trip test java.util.List<String> list1 = java.util.Arrays.asList("hello", "world"); String encoded1 = StringCodec.encode(list1); assert StringCodec.decode(encoded1).equals(lis… | Write a Java method that encodes a list of strings into a single string by prefixing each string with its length followed by a '#' character, and also provides a method to decode such an encoded string back into the original list of strings. The encoding must be unambiguous: give… | commented standard order |
| 43d70299-6dde-4609-8b32-ae12980b23b6 | Write a Java method `public static String solution(int n, int t, int m, int p)` that simulates a game where players take turns speaking digits (or letters for bases above 10) in sequence. The game starts at 0 and counts upward in base `n` (where `n` is between 2 and 16 inclusive)… | markdown | class Solution { public String solution(int n, int t, int m, int p) { int[] idxs = new int[t]; for (int i = 0; i < t; i++) { idxs[i] = p + m * i - 1; } int start = 0; StringBuilder sb = new StringBuilder(); while (s… | Write a Java method `public static String solution(int n, int t, int m, int p)` that simulates a game where players take turns speaking digits (or letters for bases above 10) in sequence. The game starts at 0 and counts upward in base `n` (where `n` is between 2 and 16 inclusive)… | The solution first computes the exact indices of the concatenated string that correspond to your turns: the i-th turn (0-indexed) is at position `p + m * i - 1`. Since the string is built by converting integers 0, 1, 2, ... into base `n` and concatenating, we generate the string … | class NBaseGame { /** * Returns the characters spoken by player p in an n-base counting game. * * @param n base of the number system (2 to 16 inclusive) * @param t number of turns for the player (positive integer) * @param m total number of players (p… | class Main { public static void main(String[] args) { // Base 2, 1 player, player 1, 3 turns: sequence "011011100..." positions 0,1,2 => "011" assert NBaseGame.solution(2, 3, 1, 1).equals("011"); // Base 16, 2 players, player 1, 2 turns: sequence 0,1,2… | Write a Java method `public static String solution(int n, int t, int m, int p)` that simulates a game where players take turns speaking digits (or letters for bases above 10) in sequence. The game starts at 0 and counts upward in base `n` (where `n` is between 2 and 16 inclusive)… | commented standard order |