chore(java): format

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2024-03-02 21:01:53 +01:00
parent c6dfb95ec0
commit f0cdb67ac1
Signed by: mfocko
GPG key ID: 7C47D46246790496
21 changed files with 520 additions and 532 deletions

View file

@ -17,72 +17,75 @@ import java.util.ArrayDeque;
*/
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;
}
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 BSTIterator {
private enum Position {
Todo, Done
private enum Position {
Todo,
Done
}
private class Entry {
public TreeNode node;
public Position position;
public Entry(TreeNode node, Position position) {
this.node = node;
this.position = position;
}
private class Entry {
public TreeNode node;
public Position position;
public void expand(ArrayDeque<Entry> stack) {
if (node.right != null) {
stack.addLast(new Entry(node.right, Position.Todo));
}
public Entry(TreeNode node, Position position) {
this.node = node;
this.position = position;
}
this.position = Position.Done;
stack.addLast(this);
public void expand(ArrayDeque<Entry> stack) {
if (node.right != null) {
stack.addLast(new Entry(node.right, Position.Todo));
}
if (node.left != null) {
stack.addLast(new Entry(node.left, Position.Todo));
}
}
}
this.position = Position.Done;
stack.addLast(this);
private ArrayDeque<Entry> stack = new ArrayDeque<>();
if (node.left != null) {
stack.addLast(new Entry(node.left, Position.Todo));
}
}
public BSTIterator(TreeNode root) {
stack.addLast(new Entry(root, Position.Todo));
}
public int next() {
for (Entry last = stack.getLast();
last != null && last.position == Position.Todo;
last = stack.getLast()) {
stack.removeLast().expand(stack);
}
private ArrayDeque<Entry> stack = new ArrayDeque<>();
public BSTIterator(TreeNode root) {
stack.addLast(new Entry(root, Position.Todo));
}
return stack.removeLast().node.val;
}
public int next() {
for (
Entry last = stack.getLast();
last != null && last.position == Position.Todo;
last = stack.getLast()
) {
stack.removeLast().expand(stack);
}
return stack.removeLast().node.val;
}
public boolean hasNext() {
Entry last = stack.peekLast();
return last != null && (last.position != Position.Todo || last.node != null);
}
public boolean hasNext() {
Entry last = stack.peekLast();
return last != null && (last.position != Position.Todo || last.node != null);
}
}
/**
* Your BSTIterator object will be instantiated and called as such:
* BSTIterator obj = new BSTIterator(root);
* int param_1 = obj.next();
* boolean param_2 = obj.hasNext();
* Your BSTIterator object will be instantiated and called as such: BSTIterator obj = new
* BSTIterator(root); int param_1 = obj.next(); boolean param_2 = obj.hasNext();
*/

View file

@ -1,21 +1,21 @@
class Solution {
public int buyChoco(int[] prices, int money) {
int[] mins = new int[2];
mins[0] = mins[1] = Integer.MAX_VALUE;
public int buyChoco(int[] prices, int money) {
int[] mins = new int[2];
mins[0] = mins[1] = Integer.MAX_VALUE;
for (int p : prices) {
if (p < mins[0]) {
mins[1] = mins[0];
mins[0] = p;
} else if (p < mins[1]) {
mins[1] = p;
}
}
int leftover = money - mins[0] - mins[1];
if (leftover < 0) {
return money;
}
return leftover;
for (int p : prices) {
if (p < mins[0]) {
mins[1] = mins[0];
mins[0] = p;
} else if (p < mins[1]) {
mins[1] = p;
}
}
int leftover = money - mins[0] - mins[1];
if (leftover < 0) {
return money;
}
return leftover;
}
}

View file

@ -1,27 +1,27 @@
class Solution {
public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
int i = 0, ii = 0;
int j = 0, jj = 0;
public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
int i = 0, ii = 0;
int j = 0, jj = 0;
while (i < word1.length && j < word2.length) {
if (word1[i].charAt(ii) != word2[j].charAt(jj)) {
return false;
}
while (i < word1.length && j < word2.length) {
if (word1[i].charAt(ii) != word2[j].charAt(jj)) {
return false;
}
++ii;
++jj;
++ii;
++jj;
if (ii >= word1[i].length()) {
++i;
ii = 0;
}
if (ii >= word1[i].length()) {
++i;
ii = 0;
}
if (jj >= word2[j].length()) {
++j;
jj = 0;
}
}
return i == word1.length && j == word2.length && ii == 0 && jj == 0;
if (jj >= word2[j].length()) {
++j;
jj = 0;
}
}
return i == word1.length && j == word2.length && ii == 0 && jj == 0;
}
}

View file

@ -1,35 +1,35 @@
import java.util.TreeMap;
class Solution {
private TreeMap<Integer, Integer> cache;
private TreeMap<Integer, Integer> cache;
private int combinationSumRec(int[] nums, int target) {
if (target < 0) {
// Base: Target is below zero
return 0;
}
if (target == 0) {
// Base: We have found a possible combination
return 1;
}
if (cache.containsKey(target)) {
// Base: Target is already precomputed
return cache.get(target);
}
int combinations = 0;
for (int num : nums) {
combinations += combinationSumRec(nums, target - num);
}
cache.put(target, combinations);
return combinations;
private int combinationSumRec(int[] nums, int target) {
if (target < 0) {
// Base: Target is below zero
return 0;
}
public int combinationSum4(int[] nums, int target) {
cache = new TreeMap<>();
return combinationSumRec(nums, target);
if (target == 0) {
// Base: We have found a possible combination
return 1;
}
if (cache.containsKey(target)) {
// Base: Target is already precomputed
return cache.get(target);
}
int combinations = 0;
for (int num : nums) {
combinations += combinationSumRec(nums, target - num);
}
cache.put(target, combinations);
return combinations;
}
public int combinationSum4(int[] nums, int target) {
cache = new TreeMap<>();
return combinationSumRec(nums, target);
}
}

View file

@ -1,33 +1,20 @@
/**
* 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;
* }
* }
* 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 Solution {
private TreeNode sortedArrayToBST(int[] nums, int min, int max) {
if (min > max) {
return null;
}
int mid = (min + max) / 2;
return new TreeNode(
nums[mid],
sortedArrayToBST(nums, min, mid - 1),
sortedArrayToBST(nums, mid + 1, max)
);
private TreeNode sortedArrayToBST(int[] nums, int min, int max) {
if (min > max) {
return null;
}
public TreeNode sortedArrayToBST(int[] nums) {
return sortedArrayToBST(nums, 0, nums.length - 1);
}
int mid = (min + max) / 2;
return new TreeNode(
nums[mid], sortedArrayToBST(nums, min, mid - 1), sortedArrayToBST(nums, mid + 1, max));
}
public TreeNode sortedArrayToBST(int[] nums) {
return sortedArrayToBST(nums, 0, nums.length - 1);
}
}

View file

@ -1,50 +1,50 @@
class Solution {
private static final int EMPTY = -1;
private static final int EMPTY = -1;
private static int charToDigit(String s, int i) {
return s.charAt(i) - '0';
private static int charToDigit(String s, int i) {
return s.charAt(i) - '0';
}
private int numDecodings(String s, int[] dp, int i) {
// out of bounds
if (i < 0 || i > s.length()) {
return 0;
}
private int numDecodings(String s, int[] dp, int i) {
// out of bounds
if (i < 0 || i > s.length()) {
return 0;
}
// final digit
if (i == s.length()) {
return 1;
}
// starting with 0, not allowed
if (s.charAt(i) == '0') {
return 0;
}
// solution has been already precomputed
if (dp[i] != EMPTY) {
return dp[i];
}
// taking just one digit
int result = numDecodings(s, dp, i + 1);
// taking two digits
if (i + 1 < s.length() && charToDigit(s, i) * 10 + charToDigit(s, i + 1) <= 26) {
result += numDecodings(s, dp, i + 2);
}
// save and return
dp[i] = result;
return result;
// final digit
if (i == s.length()) {
return 1;
}
public int numDecodings(String s) {
int[] dp = new int[s.length()];
for (int i = 0; i < dp.length; ++i) {
dp[i] = EMPTY;
}
return numDecodings(s, dp, 0);
// starting with 0, not allowed
if (s.charAt(i) == '0') {
return 0;
}
// solution has been already precomputed
if (dp[i] != EMPTY) {
return dp[i];
}
// taking just one digit
int result = numDecodings(s, dp, i + 1);
// taking two digits
if (i + 1 < s.length() && charToDigit(s, i) * 10 + charToDigit(s, i + 1) <= 26) {
result += numDecodings(s, dp, i + 2);
}
// save and return
dp[i] = result;
return result;
}
public int numDecodings(String s) {
int[] dp = new int[s.length()];
for (int i = 0; i < dp.length; ++i) {
dp[i] = EMPTY;
}
return numDecodings(s, dp, 0);
}
}

View file

@ -1,27 +1,27 @@
class Solution {
public int equalPairs(int[][] grid) {
int count = 0, n = grid.length;
public int equalPairs(int[][] grid) {
int count = 0, n = grid.length;
// Check each row r against each column c.
for (int r = 0; r < n; ++r) {
for (int c = 0; c < n; ++c) {
boolean match = true;
// Check each row r against each column c.
for (int r = 0; r < n; ++r) {
for (int c = 0; c < n; ++c) {
boolean match = true;
// Iterate over row r and column c.
for (int i = 0; i < n; ++i) {
if (grid[r][i] != grid[i][c]) {
match = false;
break;
}
}
// If row r equals column c, increment count by 1.
if (match) {
count++;
}
}
// Iterate over row r and column c.
for (int i = 0; i < n; ++i) {
if (grid[r][i] != grid[i][c]) {
match = false;
break;
}
}
return count;
// If row r equals column c, increment count by 1.
if (match) {
count++;
}
}
}
return count;
}
}

View file

@ -1,27 +1,27 @@
class Solution {
public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
int i = 0, ii = 0;
int j = 0, jj = 0;
public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
int i = 0, ii = 0;
int j = 0, jj = 0;
while (i < word1.length && j < word2.length) {
if (word1[i].charAt(ii) != word2[j].charAt(jj)) {
return false;
}
while (i < word1.length && j < word2.length) {
if (word1[i].charAt(ii) != word2[j].charAt(jj)) {
return false;
}
++ii;
++jj;
++ii;
++jj;
if (ii >= word1[i].length()) {
++i;
ii = 0;
}
if (ii >= word1[i].length()) {
++i;
ii = 0;
}
if (jj >= word2[j].length()) {
++j;
jj = 0;
}
}
return i == word1.length && j == word2.length && ii == 0 && jj == 0;
if (jj >= word2[j].length()) {
++j;
jj = 0;
}
}
return i == word1.length && j == word2.length && ii == 0 && jj == 0;
}
}

View file

@ -5,99 +5,100 @@ import java.util.List;
// This is the interface that allows for creating nested lists.
// You should not implement it, or speculate about its implementation
interface NestedInteger {
// @return true if this NestedInteger holds a single integer, rather than a nested list.
public boolean isInteger();
// @return the single integer that this NestedInteger holds, if it holds a single integer
// Return null if this NestedInteger holds a nested list
public Integer getInteger();
// @return the nested list that this NestedInteger holds, if it holds a nested list
// Return empty list if this NestedInteger holds a single integer
public List<NestedInteger> getList();
// @return true if this NestedInteger holds a single integer, rather than a nested list.
public boolean isInteger();
// @return the single integer that this NestedInteger holds, if it holds a single integer
// Return null if this NestedInteger holds a nested list
public Integer getInteger();
// @return the nested list that this NestedInteger holds, if it holds a nested list
// Return empty list if this NestedInteger holds a single integer
public List<NestedInteger> getList();
}
class NestedIterator implements Iterator<Integer> {
private class DFSEntry {
private List<NestedInteger> it;
private int i = 0;
private class DFSEntry {
private List<NestedInteger> it;
private int i = 0;
public DFSEntry(List<NestedInteger> it) {
this.it = it;
}
private int size() {
return it.size();
}
public boolean isEmpty() {
return i >= size();
}
public NestedInteger at() {
return it.get(i);
}
public void advance() {
i++;
}
public DFSEntry(List<NestedInteger> it) {
this.it = it;
}
ArrayDeque<DFSEntry> stack = new ArrayDeque<>();
public NestedIterator(List<NestedInteger> nestedList) {
stack.addLast(new DFSEntry(nestedList));
findNext();
private int size() {
return it.size();
}
private DFSEntry top() {
return stack.peekLast();
public boolean isEmpty() {
return i >= size();
}
// Performs one iteration of the DFS algorithm
private void findNext() {
while (!stack.isEmpty()) {
// We ran out of successors
if (top().isEmpty()) {
stack.removeLast();
public NestedInteger at() {
return it.get(i);
}
// Advance the parent of the removed list
if (top() != null) {
top().advance();
}
public void advance() {
i++;
}
}
continue;
}
ArrayDeque<DFSEntry> stack = new ArrayDeque<>();
// Base: we have found an integer
if (top().at().isInteger()) {
return;
}
public NestedIterator(List<NestedInteger> nestedList) {
stack.addLast(new DFSEntry(nestedList));
findNext();
}
stack.addLast(new DFSEntry(top().at().getList()));
private DFSEntry top() {
return stack.peekLast();
}
// Performs one iteration of the DFS algorithm
private void findNext() {
while (!stack.isEmpty()) {
// We ran out of successors
if (top().isEmpty()) {
stack.removeLast();
// Advance the parent of the removed list
if (top() != null) {
top().advance();
}
continue;
}
// Base: we have found an integer
if (top().at().isInteger()) {
return;
}
stack.addLast(new DFSEntry(top().at().getList()));
}
}
@Override
public Integer next() {
DFSEntry top = stack.getLast();
Integer e = top.at().getInteger();
@Override
public Integer next() {
DFSEntry top = stack.getLast();
Integer e = top.at().getInteger();
// Find next integer in the nested list
top.advance();
findNext();
// Find next integer in the nested list
top.advance();
findNext();
// Return the popped element
return e;
}
// Return the popped element
return e;
}
@Override
public boolean hasNext() {
return !stack.isEmpty();
}
@Override
public boolean hasNext() {
return !stack.isEmpty();
}
}
/**
* Your NestedIterator object will be instantiated and called as such:
* NestedIterator i = new NestedIterator(nestedList);
* while (i.hasNext()) v[f()] = i.next();
* Your NestedIterator object will be instantiated and called as such: NestedIterator i = new
* NestedIterator(nestedList); while (i.hasNext()) v[f()] = i.next();
*/

View file

@ -1,26 +1,26 @@
class Solution {
public int maxMoves(int[][] grid) {
int[][] dp = new int[grid.length][grid[0].length];
public int maxMoves(int[][] grid) {
int[][] dp = new int[grid.length][grid[0].length];
for (int x = grid[0].length - 2; x >= 0; --x) {
for (int y = 0; y < dp.length; ++y) {
// check neighbours for moves
for (int dy = -1; dy <= 1; ++dy) {
int ny = y + dy;
if (ny < 0 || ny >= dp.length || grid[y][x] >= grid[ny][x + 1]) {
continue;
}
for (int x = grid[0].length - 2; x >= 0; --x) {
for (int y = 0; y < dp.length; ++y) {
// check neighbours for moves
for (int dy = -1; dy <= 1; ++dy) {
int ny = y + dy;
if (ny < 0 || ny >= dp.length || grid[y][x] >= grid[ny][x + 1]) {
continue;
}
dp[y][x] = Math.max(dp[y][x], 1 + dp[ny][x + 1]);
}
}
dp[y][x] = Math.max(dp[y][x], 1 + dp[ny][x + 1]);
}
int m = 0;
for (int y = 0; y < grid.length; ++y) {
m = Math.max(m, dp[y][0]);
}
return m;
}
}
int m = 0;
for (int y = 0; y < grid.length; ++y) {
m = Math.max(m, dp[y][0]);
}
return m;
}
}

View file

@ -1,28 +1,28 @@
class Solution {
public int maxScore(String s) {
// count the ones
int ones = 0;
for (int i = 1; i < s.length(); ++i) {
if (s.charAt(i) == '1') {
++ones;
}
}
int zeros = s.charAt(0) == '0' ? 1 : 0;
int foundScore = ones + zeros;
for (int i = 1; i < s.length() - 1; ++i) {
switch (s.charAt(i)) {
case '0':
++zeros;
break;
case '1':
--ones;
break;
}
foundScore = Math.max(foundScore, ones + zeros);
}
return foundScore;
public int maxScore(String s) {
// count the ones
int ones = 0;
for (int i = 1; i < s.length(); ++i) {
if (s.charAt(i) == '1') {
++ones;
}
}
int zeros = s.charAt(0) == '0' ? 1 : 0;
int foundScore = ones + zeros;
for (int i = 1; i < s.length() - 1; ++i) {
switch (s.charAt(i)) {
case '0':
++zeros;
break;
case '1':
--ones;
break;
}
foundScore = Math.max(foundScore, ones + zeros);
}
return foundScore;
}
}

View file

@ -1,20 +1,20 @@
class Solution {
public String mergeAlternately(String word1, String word2) {
StringBuilder sb = new StringBuilder(word1.length() + word2.length());
public String mergeAlternately(String word1, String word2) {
StringBuilder sb = new StringBuilder(word1.length() + word2.length());
int i, j;
for (i = 0, j = 0; i < word1.length() && j < word2.length(); ++i, ++j) {
sb.append(word1.charAt(i));
sb.append(word2.charAt(j));
}
if (i < word1.length()) {
sb.append(word1.substring(i));
}
if (j < word2.length()) {
sb.append(word2.substring(j));
}
return sb.toString();
int i, j;
for (i = 0, j = 0; i < word1.length() && j < word2.length(); ++i, ++j) {
sb.append(word1.charAt(i));
sb.append(word2.charAt(j));
}
if (i < word1.length()) {
sb.append(word1.substring(i));
}
if (j < word2.length()) {
sb.append(word2.substring(j));
}
return sb.toString();
}
}

View file

@ -1,20 +1,20 @@
class Solution {
public int minOperations(String s) {
int startingZero = 0;
int startingOne = 0;
public int minOperations(String s) {
int startingZero = 0;
int startingOne = 0;
for (int i = 0; i < s.length(); ++i) {
char c = s.charAt(i);
for (int i = 0; i < s.length(); ++i) {
char c = s.charAt(i);
if (c != '0' + i % 2) {
++startingZero;
}
if (c != '0' + i % 2) {
++startingZero;
}
if (c != '0' + (i + 1) % 2) {
++startingOne;
}
}
return Math.min(startingZero, startingOne);
if (c != '0' + (i + 1) % 2) {
++startingOne;
}
}
return Math.min(startingZero, startingOne);
}
}

View file

@ -1,21 +1,21 @@
class Solution {
public int minCost(String colors, int[] neededTime) {
int[] dp = new int[colors.length() + 1];
Arrays.fill(dp, 0);
char previousColor = 0;
int previousTime = 0;
public int minCost(String colors, int[] neededTime) {
int[] dp = new int[colors.length() + 1];
Arrays.fill(dp, 0);
char previousColor = 0;
int previousTime = 0;
for (int i = 1; i <= colors.length(); ++i) {
if (colors.charAt(i - 1) == previousColor) {
dp[i] = dp[i - 1] + Math.min(previousTime, neededTime[i - 1]);
previousTime = Math.max(previousTime, neededTime[i - 1]);
} else {
dp[i] = dp[i - 1];
previousColor = colors.charAt(i - 1);
previousTime = neededTime[i - 1];
}
}
return dp[colors.length()];
for (int i = 1; i <= colors.length(); ++i) {
if (colors.charAt(i - 1) == previousColor) {
dp[i] = dp[i - 1] + Math.min(previousTime, neededTime[i - 1]);
previousTime = Math.max(previousTime, neededTime[i - 1]);
} else {
dp[i] = dp[i - 1];
previousColor = colors.charAt(i - 1);
previousTime = neededTime[i - 1];
}
}
return dp[colors.length()];
}
}

View file

@ -18,24 +18,24 @@ class Node {
*/
class Solution {
private void levelOrder(ArrayList<List<Integer>> traversal, Node root, int level) {
if (root == null) {
return;
}
if (level >= traversal.size()) {
traversal.add(new ArrayList<Integer>());
}
traversal.get(level).add(root.val);
for (var child : root.children) {
levelOrder(traversal, child, level + 1);
}
private void levelOrder(ArrayList<List<Integer>> traversal, Node root, int level) {
if (root == null) {
return;
}
public List<List<Integer>> levelOrder(Node root) {
ArrayList<List<Integer>> result = new ArrayList<>();
levelOrder(result, root, 0);
return result;
if (level >= traversal.size()) {
traversal.add(new ArrayList<Integer>());
}
traversal.get(level).add(root.val);
for (var child : root.children) {
levelOrder(traversal, child, level + 1);
}
}
public List<List<Integer>> levelOrder(Node root) {
ArrayList<List<Integer>> result = new ArrayList<>();
levelOrder(result, root, 0);
return result;
}
}

View file

@ -1,13 +1,13 @@
public class Solution {
public int hammingWeight(int n) {
int bits = 0;
public int hammingWeight(int n) {
int bits = 0;
for (int i = 0; i < 32; ++i) {
if ((n & (1 << i)) != 0) {
++bits;
}
}
return bits;
for (int i = 0; i < 32; ++i) {
if ((n & (1 << i)) != 0) {
++bits;
}
}
return bits;
}
}

View file

@ -1,27 +1,25 @@
class Solution {
private static final long MOD = 1000000007;
private static final long MOD = 1000000007;
public int numRollsToTarget(int n, int k, int target) {
// out of bounds
if (n > target || target > n * k) {
return 0;
}
long[][] dp = new long[n + 1][target + 1];
dp[0][0] = 1;
for (int toss = 1; toss <= n; ++toss) {
for (
int sumIdx = toss, maxSumIdx = Math.min(target, toss * k);
sumIdx <= maxSumIdx;
++sumIdx
) {
for (int f = 1; f <= Math.min(k, sumIdx); ++f) {
dp[toss][sumIdx] = (dp[toss][sumIdx] + dp[toss - 1][sumIdx - f]) % MOD;
}
}
}
return (int) dp[n][target];
public int numRollsToTarget(int n, int k, int target) {
// out of bounds
if (n > target || target > n * k) {
return 0;
}
long[][] dp = new long[n + 1][target + 1];
dp[0][0] = 1;
for (int toss = 1; toss <= n; ++toss) {
for (int sumIdx = toss, maxSumIdx = Math.min(target, toss * k);
sumIdx <= maxSumIdx;
++sumIdx) {
for (int f = 1; f <= Math.min(k, sumIdx); ++f) {
dp[toss][sumIdx] = (dp[toss][sumIdx] + dp[toss - 1][sumIdx - f]) % MOD;
}
}
}
return (int) dp[n][target];
}
}

View file

@ -1,32 +1,32 @@
class PeekingIterator implements Iterator<Integer> {
private Iterator<Integer> it;
private Iterator<Integer> it;
private Integer pVal;
private boolean pHasNext;
private Integer pVal;
private boolean pHasNext;
public PeekingIterator(Iterator<Integer> iterator) {
it = iterator;
next();
public PeekingIterator(Iterator<Integer> iterator) {
it = iterator;
next();
}
public Integer peek() {
return pVal;
}
@Override
public Integer next() {
pHasNext = it.hasNext();
Integer oldValue = pVal;
if (pHasNext) {
pVal = it.next();
}
public Integer peek() {
return pVal;
}
return oldValue;
}
@Override
public Integer next() {
pHasNext = it.hasNext();
Integer oldValue = pVal;
if (pHasNext) {
pVal = it.next();
}
return oldValue;
}
@Override
public boolean hasNext() {
return pHasNext;
}
@Override
public boolean hasNext() {
return pHasNext;
}
}

View file

@ -1,20 +1,20 @@
class Solution {
public boolean makeEqual(String[] words) {
int[] counters = new int[26];
public boolean makeEqual(String[] words) {
int[] counters = new int[26];
for (String w : words) {
for (int i = 0; i < w.length(); ++i) {
++counters[w.charAt(i) - 'a'];
}
}
int N = words.length;
for (int count : counters) {
if (count % N != 0) {
return false;
}
}
return true;
for (String w : words) {
for (int i = 0; i < w.length(); ++i) {
++counters[w.charAt(i) - 'a'];
}
}
int N = words.length;
for (int count : counters) {
if (count % N != 0) {
return false;
}
}
return true;
}
}

View file

@ -1,15 +1,15 @@
class Solution {
public int reductionOperations(int[] nums) {
Arrays.sort(nums);
public int reductionOperations(int[] nums) {
Arrays.sort(nums);
int counter = 0;
int counter = 0;
for (int i = nums.length - 1; i > 0; --i) {
if (nums[i - 1] != nums[i]) {
counter += nums.length - i;
}
}
return counter;
for (int i = nums.length - 1; i > 0; --i) {
if (nums[i - 1] != nums[i]) {
counter += nums.length - i;
}
}
return counter;
}
}

View file

@ -1,55 +1,54 @@
class RLEIterator {
private int[] encoding;
private int major = 0;
private int minor = 0;
private int[] encoding;
private int major = 0;
private int minor = 0;
public RLEIterator(int[] encoding) {
this.encoding = encoding;
public RLEIterator(int[] encoding) {
this.encoding = encoding;
}
private void nextSegment() {
major += 2;
minor = 0;
}
private int lastConsumed() {
if (major == 0 && minor == 0) {
// Haven't consumed anything
return -1;
} else if (major <= encoding.length && minor == 0) {
// Consumed last element of a previous segment
return encoding[major - 1];
} else if (major >= encoding.length) {
// Everything was consumed
return -1;
}
private void nextSegment() {
major += 2;
minor = 0;
// Last consumed from the current segment
return encoding[major + 1];
}
public int next(int n) {
while (n > 0 && major < encoding.length) {
int consumed = Math.min(n, encoding[major] - minor);
n -= consumed;
minor += consumed;
if (minor == encoding[major]) {
nextSegment();
}
}
private int lastConsumed() {
if (major == 0 && minor == 0) {
// Haven't consumed anything
return -1;
} else if (major <= encoding.length && minor == 0) {
// Consumed last element of a previous segment
return encoding[major - 1];
} else if (major >= encoding.length) {
// Everything was consumed
return -1;
}
// Last consumed from the current segment
return encoding[major + 1];
if (n > 0) {
return -1;
}
public int next(int n) {
while (n > 0 && major < encoding.length) {
int consumed = Math.min(n, encoding[major] - minor);
n -= consumed;
minor += consumed;
if (minor == encoding[major]) {
nextSegment();
}
}
if (n > 0) {
return -1;
}
return lastConsumed();
}
return lastConsumed();
}
}
/**
* Your RLEIterator object will be instantiated and called as such:
* RLEIterator obj = new RLEIterator(encoding);
* int param_1 = obj.next(n);
* Your RLEIterator object will be instantiated and called as such: RLEIterator obj = new
* RLEIterator(encoding); int param_1 = obj.next(n);
*/