Skip to content

Commit b986b35

Browse files
authored
Added tasks 1952, 1953, 1954, 1955, 1957.
1 parent 1c018e7 commit b986b35

File tree

15 files changed

+438
-0
lines changed

15 files changed

+438
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g1901_2000.s1952_three_divisors;
2+
3+
// #Easy #Math #2022_05_18_Time_3_ms_(6.76%)_Space_39.9_MB_(72.52%)
4+
5+
public class Solution {
6+
public boolean isThree(int n) {
7+
int divisors = 0;
8+
for (int i = 1; i <= n; i++) {
9+
if (n % i == 0) {
10+
divisors++;
11+
}
12+
}
13+
return divisors == 3;
14+
}
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
1952\. Three Divisors
2+
3+
Easy
4+
5+
Given an integer `n`, return `true` _if_ `n` _has **exactly three positive divisors**. Otherwise, return_ `false`.
6+
7+
An integer `m` is a **divisor** of `n` if there exists an integer `k` such that `n = k * m`.
8+
9+
**Example 1:**
10+
11+
**Input:** n = 2
12+
13+
**Output:** false **Explantion:** 2 has only two divisors: 1 and 2.
14+
15+
**Example 2:**
16+
17+
**Input:** n = 4
18+
19+
**Output:** true **Explantion:** 4 has three divisors: 1, 2, and 4.
20+
21+
**Constraints:**
22+
23+
* <code>1 <= n <= 10<sup>4</sup></code>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g1901_2000.s1953_maximum_number_of_weeks_for_which_you_can_work;
2+
3+
// #Medium #Array #Greedy #2022_05_18_Time_4_ms_(40.93%)_Space_77.6_MB_(67.51%)
4+
5+
public class Solution {
6+
public long numberOfWeeks(int[] milestones) {
7+
long sum = 0;
8+
long max = 0;
9+
for (int m : milestones) {
10+
sum += m;
11+
max = Math.max(max, m);
12+
}
13+
return sum - max + 1 >= max ? sum : 1 + 2 * (sum - max);
14+
}
15+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
1953\. Maximum Number of Weeks for Which You Can Work
2+
3+
Medium
4+
5+
There are `n` projects numbered from `0` to `n - 1`. You are given an integer array `milestones` where each `milestones[i]` denotes the number of milestones the <code>i<sup>th</sup></code> project has.
6+
7+
You can work on the projects following these two rules:
8+
9+
* Every week, you will finish **exactly one** milestone of **one** project. You **must** work every week.
10+
* You **cannot** work on two milestones from the same project for two **consecutive** weeks.
11+
12+
Once all the milestones of all the projects are finished, or if the only milestones that you can work on will cause you to violate the above rules, you will **stop working**. Note that you may not be able to finish every project's milestones due to these constraints.
13+
14+
Return _the **maximum** number of weeks you would be able to work on the projects without violating the rules mentioned above_.
15+
16+
**Example 1:**
17+
18+
**Input:** milestones = [1,2,3]
19+
20+
**Output:** 6
21+
22+
**Explanation:** One possible scenario is:
23+
24+
- During the 1<sup>st</sup> week, you will work on a milestone of project 0.
25+
26+
- During the 2<sup>nd</sup> week, you will work on a milestone of project 2.
27+
28+
- During the 3<sup>rd</sup> week, you will work on a milestone of project 1.
29+
30+
- During the 4<sup>th</sup> week, you will work on a milestone of project 2.
31+
32+
- During the 5<sup>th</sup> week, you will work on a milestone of project 1.
33+
34+
- During the 6<sup>th</sup> week, you will work on a milestone of project 2.
35+
36+
The total number of weeks is 6.
37+
38+
**Example 2:**
39+
40+
**Input:** milestones = [5,2,1]
41+
42+
**Output:** 7
43+
44+
**Explanation:** One possible scenario is:
45+
46+
- During the 1<sup>st</sup> week, you will work on a milestone of project 0.
47+
48+
- During the 2<sup>nd</sup> week, you will work on a milestone of project 1.
49+
50+
- During the 3<sup>rd</sup> week, you will work on a milestone of project 0.
51+
52+
- During the 4<sup>th</sup> week, you will work on a milestone of project 1.
53+
54+
- During the 5<sup>th</sup> week, you will work on a milestone of project 0.
55+
56+
- During the 6<sup>th</sup> week, you will work on a milestone of project 2.
57+
58+
- During the 7<sup>th</sup> week, you will work on a milestone of project 0.
59+
60+
The total number of weeks is 7. Note that you cannot work on the last milestone of project 0 on 8<sup>th</sup> week because it would violate the rules. Thus, one milestone in project 0 will remain unfinished.
61+
62+
**Constraints:**
63+
64+
* `n == milestones.length`
65+
* <code>1 <= n <= 10<sup>5</sup></code>
66+
* <code>1 <= milestones[i] <= 10<sup>9</sup></code>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g1901_2000.s1954_minimum_garden_perimeter_to_collect_enough_apples;
2+
3+
// #Medium #Math #Binary_Search #2022_05_18_Time_2_ms_(59.57%)_Space_40.7_MB_(69.15%)
4+
5+
public class Solution {
6+
public long minimumPerimeter(long neededApples) {
7+
long l = 1;
8+
long r = 1000000;
9+
long res = l;
10+
11+
while (l <= r) {
12+
long m = l + (r - l) / 2;
13+
boolean isPossible = check(m, neededApples);
14+
15+
if (isPossible) {
16+
res = m;
17+
r = m - 1;
18+
} else {
19+
l = m + 1;
20+
}
21+
}
22+
return res * 8;
23+
}
24+
25+
private boolean check(long len, long neededApples) {
26+
long sum = len * (len + 1) / 2;
27+
long applesPerQuadrant = 2 * len * sum;
28+
long totalCount = 4 * sum + 4 * applesPerQuadrant;
29+
return (totalCount >= neededApples);
30+
}
31+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
1954\. Minimum Garden Perimeter to Collect Enough Apples
2+
3+
Medium
4+
5+
In a garden represented as an infinite 2D grid, there is an apple tree planted at **every** integer coordinate. The apple tree planted at an integer coordinate `(i, j)` has `|i| + |j|` apples growing on it.
6+
7+
You will buy an axis-aligned **square plot** of land that is centered at `(0, 0)`.
8+
9+
Given an integer `neededApples`, return _the **minimum perimeter** of a plot such that **at least**_ `neededApples` _apples are **inside or on** the perimeter of that plot_.
10+
11+
The value of `|x|` is defined as:
12+
13+
* `x` if `x >= 0`
14+
* `-x` if `x < 0`
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2019/08/30/1527_example_1_2.png)
19+
20+
**Input:** neededApples = 1
21+
22+
**Output:** 8
23+
24+
**Explanation:** A square plot of side length 1 does not contain any apples. However, a square plot of side length 2 has 12 apples inside (as depicted in the image above). The perimeter is 2 \* 4 = 8.
25+
26+
**Example 2:**
27+
28+
**Input:** neededApples = 13
29+
30+
**Output:** 16
31+
32+
**Example 3:**
33+
34+
**Input:** neededApples = 1000000000
35+
36+
**Output:** 5040
37+
38+
**Constraints:**
39+
40+
* <code>1 <= neededApples <= 10<sup>15</sup></code>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package g1901_2000.s1955_count_number_of_special_subsequences;
2+
3+
// #Hard #Array #Dynamic_Programming #2022_05_18_Time_22_ms_(80.65%)_Space_121_MB_(27.42%)
4+
5+
public class Solution {
6+
public int countSpecialSubsequences(int[] nums) {
7+
int mod = 1000000007;
8+
int[] dp = new int[] {1, 0, 0, 0};
9+
10+
for (int n : nums) {
11+
dp[n + 1] = (dp[n] + 2 * dp[n + 1] % mod) % mod;
12+
}
13+
14+
return dp[3];
15+
}
16+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
1955\. Count Number of Special Subsequences
2+
3+
Hard
4+
5+
A sequence is **special** if it consists of a **positive** number of `0`s, followed by a **positive** number of `1`s, then a **positive** number of `2`s.
6+
7+
* For example, `[0,1,2]` and `[0,0,1,1,1,2]` are special.
8+
* In contrast, `[2,1,0]`, `[1]`, and `[0,1,2,0]` are not special.
9+
10+
Given an array `nums` (consisting of **only** integers `0`, `1`, and `2`), return _the **number of different subsequences** that are special_. Since the answer may be very large, **return it modulo** <code>10<sup>9</sup> + 7</code>.
11+
12+
A **subsequence** of an array is a sequence that can be derived from the array by deleting some or no elements without changing the order of the remaining elements. Two subsequences are **different** if the **set of indices** chosen are different.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [0,1,2,2]
17+
18+
**Output:** 3
19+
20+
**Explanation:** The special subsequences are bolded [**0**,**1**,**2**,2], [**0**,**1**,2,**2**], and [**0**,**1**,**2**,**2**].
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [2,2,0,0]
25+
26+
**Output:** 0
27+
28+
**Explanation:** There are no special subsequences in [2,2,0,0].
29+
30+
**Example 3:**
31+
32+
**Input:** nums = [0,1,2,0,1,2]
33+
34+
**Output:** 7
35+
36+
**Explanation:** The special subsequences are bolded:
37+
38+
- [**0**,**1**,**2**,0,1,2]
39+
40+
- [**0**,**1**,2,0,1,**2**]
41+
42+
- [**0**,**1**,**2**,0,1,**2**]
43+
44+
- [**0**,**1**,2,0,**1**,**2**]
45+
46+
- [**0**,1,2,**0**,**1**,**2**]
47+
48+
- [**0**,1,2,0,**1**,**2**]
49+
50+
- [0,1,2,**0**,**1**,**2**]
51+
52+
**Constraints:**
53+
54+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
55+
* `0 <= nums[i] <= 2`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g1901_2000.s1957_delete_characters_to_make_fancy_string;
2+
3+
// #Easy #String #2022_05_18_Time_54_ms_(52.94%)_Space_67.8_MB_(72.35%)
4+
5+
public class Solution {
6+
public String makeFancyString(String s) {
7+
StringBuilder ans = new StringBuilder();
8+
int c = 1;
9+
ans.append(s.charAt(0));
10+
for (int i = 1; i < s.length(); i++) {
11+
if (s.charAt(i) == s.charAt(i - 1)) {
12+
c++;
13+
} else {
14+
c = 1;
15+
}
16+
if (c < 3) {
17+
ans.append(s.charAt(i));
18+
}
19+
}
20+
21+
return ans.toString();
22+
}
23+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
1957\. Delete Characters to Make Fancy String
2+
3+
Easy
4+
5+
A **fancy string** is a string where no **three** **consecutive** characters are equal.
6+
7+
Given a string `s`, delete the **minimum** possible number of characters from `s` to make it **fancy**.
8+
9+
Return _the final string after the deletion_. It can be shown that the answer will always be **unique**.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "leeetcode"
14+
15+
**Output:** "leetcode"
16+
17+
**Explanation:**
18+
19+
Remove an 'e' from the first group of 'e's to create "leetcode".
20+
21+
No three consecutive characters are equal, so return "leetcode".
22+
23+
**Example 2:**
24+
25+
**Input:** s = "aaabaaaa"
26+
27+
**Output:** "aabaa"
28+
29+
**Explanation:**
30+
31+
Remove an 'a' from the first group of 'a's to create "aabaaaa".
32+
33+
Remove two 'a's from the second group of 'a's to create "aabaa".
34+
35+
No three consecutive characters are equal, so return "aabaa".
36+
37+
**Example 3:**
38+
39+
**Input:** s = "aab"
40+
41+
**Output:** "aab"
42+
43+
**Explanation:** No three consecutive characters are equal, so return "aab".
44+
45+
**Constraints:**
46+
47+
* <code>1 <= s.length <= 10<sup>5</sup></code>
48+
* `s` consists only of lowercase English letters.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g1901_2000.s1952_three_divisors;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void isThree() {
11+
assertThat(new Solution().isThree(2), equalTo(false));
12+
}
13+
14+
@Test
15+
void isThree2() {
16+
assertThat(new Solution().isThree(4), equalTo(true));
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g1901_2000.s1953_maximum_number_of_weeks_for_which_you_can_work;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void numberOfWeeks() {
11+
assertThat(new Solution().numberOfWeeks(new int[] {1, 2, 3}), equalTo(6L));
12+
}
13+
14+
@Test
15+
void numberOfWeeks2() {
16+
assertThat(new Solution().numberOfWeeks(new int[] {5, 2, 1}), equalTo(7L));
17+
}
18+
}

0 commit comments

Comments
 (0)