Skip to content

Added tasks 3151-3154 #1759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/main/java/g3101_3200/s3151_special_array_i/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package g3101_3200.s3151_special_array_i;

// #Easy #Array #2024_05_22_Time_0_ms_(100.00%)_Space_43.2_MB_(51.16%)

public class Solution {
public boolean isArraySpecial(int[] nums) {
for (int i = 1; i < nums.length; i++) {
if (nums[i - 1] % 2 == 1 && nums[i] % 2 == 1) {
return false;
}
if (nums[i - 1] % 2 == 0 && nums[i] % 2 == 0) {
return false;
}
}
return true;
}
}
42 changes: 42 additions & 0 deletions src/main/java/g3101_3200/s3151_special_array_i/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
3151\. Special Array I

Easy

An array is considered **special** if every pair of its adjacent elements contains two numbers with different parity.

You are given an array of integers `nums`. Return `true` if `nums` is a **special** array, otherwise, return `false`.

**Example 1:**

**Input:** nums = [1]

**Output:** true

**Explanation:**

There is only one element. So the answer is `true`.

**Example 2:**

**Input:** nums = [2,1,4]

**Output:** true

**Explanation:**

There is only two pairs: `(2,1)` and `(1,4)`, and both of them contain numbers with different parity. So the answer is `true`.

**Example 3:**

**Input:** nums = [4,3,1,6]

**Output:** false

**Explanation:**

`nums[1]` and `nums[2]` are both odd. So the answer is `false`.

**Constraints:**

* `1 <= nums.length <= 100`
* `1 <= nums[i] <= 100`
24 changes: 24 additions & 0 deletions src/main/java/g3101_3200/s3152_special_array_ii/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package g3101_3200.s3152_special_array_ii;

// #Medium #Array #Binary_Search #Prefix_Sum #2024_05_22_Time_2_ms_(99.93%)_Space_97.9_MB_(79.71%)

public class Solution {
public boolean[] isArraySpecial(int[] nums, int[][] queries) {
int n = nums.length;
int[] bad = new int[n];
for (int i = 1; i < n; i++) {
bad[i] = bad[i - 1] + (((nums[i - 1] ^ nums[i]) & 1) ^ 1);
}
int nq = queries.length;
boolean[] res = new boolean[nq];
for (int i = 0; i < nq; i++) {
int[] q = queries[i];
res[i] = calc(bad, q[0], q[1]) == 0;
}
return res;
}

private int calc(int[] arr, int st, int end) {
return arr[end] - arr[st];
}
}
38 changes: 38 additions & 0 deletions src/main/java/g3101_3200/s3152_special_array_ii/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
3152\. Special Array II

Medium

An array is considered **special** if every pair of its adjacent elements contains two numbers with different parity.

You are given an array of integer `nums` and a 2D integer matrix `queries`, where for <code>queries[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> your task is to check that subarray <code>nums[from<sub>i</sub>..to<sub>i</sub>]</code> is **special** or not.

Return an array of booleans `answer` such that `answer[i]` is `true` if <code>nums[from<sub>i</sub>..to<sub>i</sub>]</code> is special.

**Example 1:**

**Input:** nums = [3,4,1,2,6], queries = [[0,4]]

**Output:** [false]

**Explanation:**

The subarray is `[3,4,1,2,6]`. 2 and 6 are both even.

**Example 2:**

**Input:** nums = [4,3,1,6], queries = [[0,2],[2,3]]

**Output:** [false,true]

**Explanation:**

1. The subarray is `[4,3,1]`. 3 and 1 are both odd. So the answer to this query is `false`.
2. The subarray is `[1,6]`. There is only one pair: `(1,6)` and it contains numbers with different parity. So the answer to this query is `true`.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
* <code>1 <= queries.length <= 10<sup>5</sup></code>
* `queries[i].length == 2`
* `0 <= queries[i][0] <= queries[i][1] <= nums.length - 1`
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package g3101_3200.s3153_sum_of_digit_differences_of_all_pairs;

// #Medium #Array #Hash_Table #Math #Counting #2024_05_22_Time_12_ms_(100.00%)_Space_62.8_MB_(6.25%)

public class Solution {
public long sumDigitDifferences(int[] nums) {
long result = 0;
while (nums[0] > 0) {
int[] counts = new int[10];
for (int i = 0; i < nums.length; i++) {
int digit = nums[i] % 10;
nums[i] = nums[i] / 10;
result += i - counts[digit];
counts[digit]++;
}
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
3153\. Sum of Digit Differences of All Pairs

Medium

You are given an array `nums` consisting of **positive** integers where all integers have the **same** number of digits.

The **digit difference** between two integers is the _count_ of different digits that are in the **same** position in the two integers.

Return the **sum** of the **digit differences** between **all** pairs of integers in `nums`.

**Example 1:**

**Input:** nums = [13,23,12]

**Output:** 4

**Explanation:**
We have the following:
- The digit difference between **1**3 and **2**3 is 1.
- The digit difference between 1**3** and 1**2** is 1.
- The digit difference between **23** and **12** is 2.
So the total sum of digit differences between all pairs of integers is `1 + 1 + 2 = 4`.

**Example 2:**

**Input:** nums = [10,10,10,10]

**Output:** 0

**Explanation:**
All the integers in the array are the same. So the total sum of digit differences between all pairs of integers will be 0.

**Constraints:**

* <code>2 <= nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] < 10<sup>9</sup></code>
* All integers in `nums` have the same number of digits.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package g3101_3200.s3154_find_number_of_ways_to_reach_the_k_th_stair;

// #Hard #Dynamic_Programming #Math #Bit_Manipulation #Memoization #Combinatorics
// #2024_05_22_Time_0_ms_(100.00%)_Space_40.3_MB_(98.50%)

public class Solution {
public int waysToReachStair(int k) {
int x = 1;
int y = 1;
int a = 0;
while (x > 0 && x - y <= k) {
if (x >= k) {
a += combi(y, x - k);
}
x <<= 1;
y++;
}
return a;
}

private int combi(int a, int b) {
if (b > a - b) {
b = a - b;
}
long r = 1;
for (int i = 0; i < b; i++) {
r *= (a - i);
r /= (i + 1);
}
return (int) r;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
3154\. Find Number of Ways to Reach the K-th Stair

Hard

You are given a **non-negative** integer `k`. There exists a staircase with an infinite number of stairs, with the **lowest** stair numbered 0.

Alice has an integer `jump`, with an initial value of 0. She starts on stair 1 and wants to reach stair `k` using **any** number of **operations**. If she is on stair `i`, in one **operation** she can:

* Go down to stair `i - 1`. This operation **cannot** be used consecutively or on stair 0.
* Go up to stair <code>i + 2<sup>jump</sup></code>. And then, `jump` becomes `jump + 1`.

Return the _total_ number of ways Alice can reach stair `k`.

**Note** that it is possible that Alice reaches the stair `k`, and performs some operations to reach the stair `k` again.

**Example 1:**

**Input:** k = 0

**Output:** 2

**Explanation:**

The 2 possible ways of reaching stair 0 are:

* Alice starts at stair 1.
* Using an operation of the first type, she goes down 1 stair to reach stair 0.
* Alice starts at stair 1.
* Using an operation of the first type, she goes down 1 stair to reach stair 0.
* Using an operation of the second type, she goes up 2<sup>0</sup> stairs to reach stair 1.
* Using an operation of the first type, she goes down 1 stair to reach stair 0.

**Example 2:**

**Input:** k = 1

**Output:** 4

**Explanation:**

The 4 possible ways of reaching stair 1 are:

* Alice starts at stair 1. Alice is at stair 1.
* Alice starts at stair 1.
* Using an operation of the first type, she goes down 1 stair to reach stair 0.
* Using an operation of the second type, she goes up 2<sup>0</sup> stairs to reach stair 1.
* Alice starts at stair 1.
* Using an operation of the second type, she goes up 2<sup>0</sup> stairs to reach stair 2.
* Using an operation of the first type, she goes down 1 stair to reach stair 1.
* Alice starts at stair 1.
* Using an operation of the first type, she goes down 1 stair to reach stair 0.
* Using an operation of the second type, she goes up 2<sup>0</sup> stairs to reach stair 1.
* Using an operation of the first type, she goes down 1 stair to reach stair 0.
* Using an operation of the second type, she goes up 2<sup>1</sup> stairs to reach stair 2.
* Using an operation of the first type, she goes down 1 stair to reach stair 1.

**Constraints:**

* <code>0 <= k <= 10<sup>9</sup></code>
28 changes: 28 additions & 0 deletions src/test/java/g3101_3200/s3151_special_array_i/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package g3101_3200.s3151_special_array_i;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void isArraySpecial() {
assertThat(new Solution().isArraySpecial(new int[] {1}), equalTo(true));
}

@Test
void isArraySpecial2() {
assertThat(new Solution().isArraySpecial(new int[] {2, 1, 4}), equalTo(true));
}

@Test
void isArraySpecial3() {
assertThat(new Solution().isArraySpecial(new int[] {4, 3, 1, 6}), equalTo(false));
}

@Test
void isArraySpecial4() {
assertThat(new Solution().isArraySpecial(new int[] {2, 10}), equalTo(false));
}
}
22 changes: 22 additions & 0 deletions src/test/java/g3101_3200/s3152_special_array_ii/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package g3101_3200.s3152_special_array_ii;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void isArraySpecial() {
assertThat(
new Solution().isArraySpecial(new int[] {3, 4, 1, 2, 6}, new int[][] {{0, 4}}),
equalTo(new boolean[] {false}));
}

@Test
void isArraySpecial2() {
assertThat(
new Solution().isArraySpecial(new int[] {4, 3, 1, 6}, new int[][] {{0, 2}, {2, 3}}),
equalTo(new boolean[] {false, true}));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g3101_3200.s3153_sum_of_digit_differences_of_all_pairs;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void sumDigitDifferences() {
assertThat(new Solution().sumDigitDifferences(new int[] {13, 23, 12}), equalTo(4L));
}

@Test
void sumDigitDifferences2() {
assertThat(new Solution().sumDigitDifferences(new int[] {10, 10, 10, 10}), equalTo(0L));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g3101_3200.s3154_find_number_of_ways_to_reach_the_k_th_stair;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void waysToReachStair() {
assertThat(new Solution().waysToReachStair(0), equalTo(2));
}

@Test
void waysToReachStair2() {
assertThat(new Solution().waysToReachStair(1), equalTo(4));
}
}
Loading