Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package g1501_1600.s1521_find_a_value_of_a_mysterious_function_closest_to_target;

// #Hard #Array #Binary_Search #Bit_Manipulation #Segment_Tree
// #2022_04_09_Time_9_ms_(100.00%)_Space_58.4_MB_(94.74%)

public class Solution {
public int closestToTarget(int[] arr, int target) {
int[] prefix = new int[22];
prefix[0] = -1;
int res = Integer.MAX_VALUE;
int size = 1;
for (int a : arr) {
int ns = 1;
for (int i = 1; i < size; i++) {
if (prefix[ns - 1] != (prefix[i] & a)) {
prefix[ns++] = prefix[i] & a;
res = Math.min(res, Math.abs((prefix[i] & a) - target));
}
}
if (prefix[ns - 1] != a) {
prefix[ns++] = a;
res = Math.min(res, Math.abs(a - target));
}
size = ns;
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
1521\. Find a Value of a Mysterious Function Closest to Target

Hard

![](https://assets.leetcode.com/uploads/2020/07/09/change.png)

Winston was given the above mysterious function `func`. He has an integer array `arr` and an integer `target` and he wants to find the values `l` and `r` that make the value `|func(arr, l, r) - target|` minimum possible.

Return _the minimum possible value_ of `|func(arr, l, r) - target|`.

Notice that `func` should be called with the values `l` and `r` where `0 <= l, r < arr.length`.

**Example 1:**

**Input:** arr = [9,12,3,7,15], target = 5

**Output:** 2

**Explanation:** Calling func with all the pairs of [l,r] = [[0,0],[1,1],[2,2],[3,3],[4,4],[0,1],[1,2],[2,3],[3,4],[0,2],[1,3],[2,4],[0,3],[1,4],[0,4]], Winston got the following results [9,12,3,7,15,8,0,3,7,0,0,3,0,0,0]. The value closest to 5 is 7 and 3, thus the minimum difference is 2.

**Example 2:**

**Input:** arr = [1000000,1000000,1000000], target = 1

**Output:** 999999

**Explanation:** Winston called the func with all possible values of [l,r] and he always got 1000000, thus the min difference is 999999.

**Example 3:**

**Input:** arr = [1,2,4,8,16], target = 0

**Output:** 0

**Constraints:**

* <code>1 <= arr.length <= 10<sup>5</sup></code>
* <code>1 <= arr[i] <= 10<sup>6</sup></code>
* <code>0 <= target <= 10<sup>7</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package g1501_1600.s1523_count_odd_numbers_in_an_interval_range;

// #Easy #Math #Programming_Skills_I_Day_1_Basic_Data_Type
// #2022_04_09_Time_0_ms_(100.00%)_Space_41.5_MB_(10.62%)

public class Solution {
public int countOdds(int low, int high) {
if (low % 2 != 0 || high % 2 != 0) {
return (high - low) / 2 + 1;
}
return (high - low) / 2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
1523\. Count Odd Numbers in an Interval Range

Easy

Given two non-negative integers `low` and `high`. Return the _count of odd numbers between_ `low` _and_ `high`_ (inclusive)_.

**Example 1:**

**Input:** low = 3, high = 7

**Output:** 3

**Explanation:** The odd numbers between 3 and 7 are [3,5,7].

**Example 2:**

**Input:** low = 8, high = 10

**Output:** 1

**Explanation:** The odd numbers between 8 and 10 are [9].

**Constraints:**

* `0 <= low <= high <= 10^9`
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package g1501_1600.s1524_number_of_sub_arrays_with_odd_sum;

// #Medium #Array #Dynamic_Programming #Math #Prefix_Sum
// #2022_04_09_Time_9_ms_(90.24%)_Space_97_MB_(68.09%)

public class Solution {
public int numOfSubarrays(int[] arr) {
int number = arr[0] % 2 == 0 ? 0 : 1;
long res = number;
for (int i = 1; i < arr.length; i++) {
if (arr[i] % 2 != 0) {
number = i - number + 1;
}
res += number;
}
long mod = 1_000_000_007;
return (int) (res % mod);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
1524\. Number of Sub-arrays With Odd Sum

Medium

Given an array of integers `arr`, return _the number of subarrays with an **odd** sum_.

Since the answer can be very large, return it modulo <code>10<sup>9</sup> + 7</code>.

**Example 1:**

**Input:** arr = [1,3,5]

**Output:** 4

**Explanation:** All subarrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]]

All sub-arrays sum are [1,4,9,3,8,5].

Odd sums are [1,9,3,5] so the answer is 4.

**Example 2:**

**Input:** arr = [2,4,6]

**Output:** 0

**Explanation:** All subarrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]]

All sub-arrays sum are [2,6,12,4,10,6].

All sub-arrays have even sum and the answer is 0.

**Example 3:**

**Input:** arr = [1,2,3,4,5,6,7]

**Output:** 16

**Constraints:**

* <code>1 <= arr.length <= 10<sup>5</sup></code>
* `1 <= arr[i] <= 100`
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package g1501_1600.s1525_number_of_good_ways_to_split_a_string;

// #Medium #String #Dynamic_Programming #Bit_Manipulation
// #2022_04_09_Time_19_ms_(69.66%)_Space_48_MB_(46.20%)

import java.util.HashSet;

public class Solution {
public int numSplits(String s) {
HashSet<Character> hs = new HashSet<>();
int[] dp1 = new int[s.length() - 1];
int[] dp2 = new int[s.length() - 1];
for (int i = 0; i < s.length() - 1; i++) {
char ch = s.charAt(i);
hs.add(ch);
dp1[i] = hs.size();
}
HashSet<Character> hm = new HashSet<>();
for (int i = s.length() - 1; i > 0; i--) {
char ch = s.charAt(i);
hm.add(ch);
dp2[i - 1] = hm.size();
}
int count = 0;
for (int i = 0; i < s.length() - 1; i++) {
if (dp1[i] == dp2[i]) {
count++;
}
}
return count;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
1525\. Number of Good Ways to Split a String

Medium

You are given a string `s`.

A split is called **good** if you can split `s` into two non-empty strings <code>s<sub>left</sub></code> and <code>s<sub>right</sub></code> where their concatenation is equal to `s` (i.e., <code>s<sub>left</sub> + s<sub>right</sub> = s</code>) and the number of distinct letters in <code>s<sub>left</sub></code> and <code>s<sub>right</sub></code> is the same.

Return _the number of **good splits** you can make in `s`_.

**Example 1:**

**Input:** s = "aacaba"

**Output:** 2

**Explanation:** There are 5 ways to split `"aacaba"` and 2 of them are good.

("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.

("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.

("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).

("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).

("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.

**Example 2:**

**Input:** s = "abcd"

**Output:** 1

**Explanation:** Split the string as follows ("ab", "cd").

**Constraints:**

* <code>1 <= s.length <= 10<sup>5</sup></code>
* `s` consists of only lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package g1501_1600.s1526_minimum_number_of_increments_on_subarrays_to_form_a_target_array;

// #Hard #Array #Dynamic_Programming #Greedy #Stack #Monotonic_Stack
// #2022_04_09_Time_4_ms_(70.36%)_Space_72.7_MB_(76.68%)

public class Solution {
public int minNumberOperations(int[] target) {
int operations = target[0];
for (int i = 1; i < target.length; i++) {
if (target[i] > target[i - 1]) {
operations += target[i] - target[i - 1];
}
}
return operations;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
1526\. Minimum Number of Increments on Subarrays to Form a Target Array

Hard

You are given an integer array `target`. You have an integer array `initial` of the same size as `target` with all elements initially zeros.

In one operation you can choose **any** subarray from `initial` and increment each value by one.

Return _the minimum number of operations to form a_ `target` _array from_ `initial`.

The test cases are generated so that the answer fits in a 32-bit integer.

**Example 1:**

**Input:** target = [1,2,3,2,1]

**Output:** 3

**Explanation:** We need at least 3 operations to form the target array from the initial array.

[**0,0,0,0,0**] increment 1 from index 0 to 4 (inclusive).

[1,**1,1,1**,1] increment 1 from index 1 to 3 (inclusive).

[1,2,**2**,2,1] increment 1 at index 2.

[1,2,3,2,1] target array is formed.

**Example 2:**

**Input:** target = [3,1,1,2]

**Output:** 4

**Explanation:** [**0,0,0,0**] -> [1,1,1,**1**] -> [**1**,1,1,2] -> [**2**,1,1,2] -> [3,1,1,2]

**Example 3:**

**Input:** target = [3,1,5,4,2]

**Output:** 7

**Explanation:** [**0,0,0,0,0**] -> [**1**,1,1,1,1] -> [**2**,1,1,1,1] -> [3,1,**1,1,1**] -> [3,1,**2,2**,2] -> [3,1,**3,3**,2] -> [3,1,**4**,4,2] -> [3,1,5,4,2].

**Constraints:**

* <code>1 <= target.length <= 10<sup>5</sup></code>
* <code>1 <= target[i] <= 10<sup>5</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package g1501_1600.s1521_find_a_value_of_a_mysterious_function_closest_to_target;

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

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void closestToTarget() {
assertThat(new Solution().closestToTarget(new int[] {9, 12, 3, 7, 15}, 5), equalTo(2));
}

@Test
void closestToTarget2() {
assertThat(
new Solution().closestToTarget(new int[] {1000000, 1000000, 1000000}, 1),
equalTo(999999));
}

@Test
void closestToTarget3() {
assertThat(new Solution().closestToTarget(new int[] {1, 2, 4, 8, 16}, 0), equalTo(0));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g1501_1600.s1523_count_odd_numbers_in_an_interval_range;

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

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void countOdds() {
assertThat(new Solution().countOdds(3, 7), equalTo(3));
}

@Test
void countOdds2() {
assertThat(new Solution().countOdds(8, 10), equalTo(1));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g1501_1600.s1524_number_of_sub_arrays_with_odd_sum;

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

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void numOfSubarrays() {
assertThat(new Solution().numOfSubarrays(new int[] {1, 3, 5}), equalTo(4));
}

@Test
void numOfSubarrays2() {
assertThat(new Solution().numOfSubarrays(new int[] {2, 4, 6}), equalTo(0));
}

@Test
void numOfSubarrays3() {
assertThat(new Solution().numOfSubarrays(new int[] {1, 2, 3, 4, 5, 6, 7}), equalTo(16));
}
}
Loading