Skip to content

Commit c9b53ef

Browse files
authored
Added tasks 728, 729, 730, 731, 732.
1 parent 52c6f61 commit c9b53ef

File tree

15 files changed

+484
-0
lines changed

15 files changed

+484
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g0701_0800.s0728_self_dividing_numbers;
2+
3+
// #Easy #Math
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
public List<Integer> selfDividingNumbers(int left, int right) {
10+
List<Integer> selfDividingNumbers = new ArrayList<>();
11+
for (int i = left; i <= right; i++) {
12+
int num = i;
13+
boolean dividing = false;
14+
if (num % 10 != 0) {
15+
while (num > 0) {
16+
int temp = num % 10;
17+
if (i % temp == 0) {
18+
dividing = true;
19+
} else {
20+
dividing = false;
21+
break;
22+
}
23+
num = num / 10;
24+
}
25+
if (dividing) {
26+
selfDividingNumbers.add(i);
27+
}
28+
}
29+
}
30+
return selfDividingNumbers;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
728\. Self Dividing Numbers
2+
3+
Easy
4+
5+
A **self-dividing number** is a number that is divisible by every digit it contains.
6+
7+
* For example, `128` is **a self-dividing number** because `128 % 1 == 0`, `128 % 2 == 0`, and `128 % 8 == 0`.
8+
9+
A **self-dividing number** is not allowed to contain the digit zero.
10+
11+
Given two integers `left` and `right`, return _a list of all the **self-dividing numbers** in the range_ `[left, right]`.
12+
13+
**Example 1:**
14+
15+
**Input:** left = 1, right = 22
16+
17+
**Output:** [1,2,3,4,5,6,7,8,9,11,12,15,22]
18+
19+
**Example 2:**
20+
21+
**Input:** left = 47, right = 85
22+
23+
**Output:** [48,55,66,77]
24+
25+
**Constraints:**
26+
27+
* <code>1 <= left <= right <= 10<sup>4</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g0701_0800.s0729_my_calendar_i;
2+
3+
import java.util.TreeSet;
4+
5+
// #Medium #Design #Ordered_Set #Segment_Tree
6+
7+
public class MyCalendar {
8+
static class Meeting implements Comparable<Meeting> {
9+
public final int start;
10+
public final int end;
11+
12+
public Meeting(int start, int end) {
13+
this.start = start;
14+
this.end = end;
15+
}
16+
17+
public int compareTo(Meeting anotherMeeting) {
18+
return this.start - anotherMeeting.start;
19+
}
20+
}
21+
22+
private final TreeSet<Meeting> meetings;
23+
24+
public MyCalendar() {
25+
meetings = new TreeSet<>();
26+
}
27+
28+
public boolean book(int start, int end) {
29+
Meeting meetingToBook = new Meeting(start, end);
30+
Meeting prevMeeting = meetings.floor(meetingToBook);
31+
Meeting nextMeeting = meetings.ceiling(meetingToBook);
32+
if ((prevMeeting == null || prevMeeting.end <= meetingToBook.start)
33+
&& (nextMeeting == null || meetingToBook.end <= nextMeeting.start)) {
34+
35+
meetings.add(meetingToBook);
36+
return true;
37+
}
38+
return false;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
729\. My Calendar I
2+
3+
Medium
4+
5+
You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a **double booking**.
6+
7+
A **double booking** happens when two events have some non-empty intersection (i.e., some moment is common to both events.).
8+
9+
The event can be represented as a pair of integers `start` and `end` that represents a booking on the half-open interval `[start, end)`, the range of real numbers `x` such that `start <= x < end`.
10+
11+
Implement the `MyCalendar` class:
12+
13+
* `MyCalendar()` Initializes the calendar object.
14+
* `boolean book(int start, int end)` Returns `true` if the event can be added to the calendar successfully without causing a **double booking**. Otherwise, return `false` and do not add the event to the calendar.
15+
16+
**Example 1:**
17+
18+
**Input**
19+
20+
["MyCalendar", "book", "book", "book"]
21+
22+
[[], [10, 20], [15, 25], [20, 30]]
23+
24+
**Output:** [null, true, false, true]
25+
26+
**Explanation:**
27+
28+
MyCalendar myCalendar = new MyCalendar();
29+
myCalendar.book(10, 20); // return True
30+
myCalendar.book(15, 25); // return False, It can not be booked because time 15 is already booked by another event.
31+
myCalendar.book(20, 30); // return True, The event can be booked, as the first event takes every time less than 20, but not including 20.
32+
33+
**Constraints:**
34+
35+
* <code>0 <= start < end <= 10<sup>9</sup></code>
36+
* At most `1000` calls will be made to `book`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package g0701_0800.s0730_count_different_palindromic_subsequences;
2+
3+
// #Hard #String #Dynamic_Programming
4+
5+
public class Solution {
6+
public int countPalindromicSubsequences(String s) {
7+
int big = 1000000007;
8+
int len = s.length();
9+
if (len < 2) {
10+
return len;
11+
}
12+
int[][] dp = new int[len][len];
13+
for (int i = 0; i < len; i++) {
14+
char c = s.charAt(i);
15+
int deta = 1;
16+
dp[i][i] = 1;
17+
int l2 = -1;
18+
for (int j = i - 1; j >= 0; j--) {
19+
if (s.charAt(j) != c) {
20+
dp[j][i] = deal(dp[j][i - 1] + deta, big);
21+
} else {
22+
if (l2 < 0) {
23+
l2 = j;
24+
deta = dp[j + 1][i - 1] + 1;
25+
} else {
26+
deta = dp[j + 1][i - 1] - dp[j + 1][l2 - 1];
27+
}
28+
deta = deal(deta, big);
29+
dp[j][i] = deal(dp[j][i - 1] + deta, big);
30+
}
31+
}
32+
}
33+
return deal(dp[0][len - 1], big);
34+
}
35+
36+
private int deal(int x, int big) {
37+
x %= big;
38+
if (x < 0) {
39+
x += big;
40+
}
41+
return x;
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
730\. Count Different Palindromic Subsequences
2+
3+
Hard
4+
5+
Given a string s, return _the number of different non-empty palindromic subsequences in_ `s`. Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
6+
7+
A subsequence of a string is obtained by deleting zero or more characters from the string.
8+
9+
A sequence is palindromic if it is equal to the sequence reversed.
10+
11+
Two sequences <code>a<sub>1</sub>, a<sub>2</sub>, ...</code> and <code>b<sub>1</sub>, b<sub>2</sub>, ...</code> are different if there is some `i` for which <code>a<sub>i</sub> != b<sub>i</sub></code>.
12+
13+
**Example 1:**
14+
15+
**Input:** s = "bccb"
16+
17+
**Output:** 6
18+
19+
**Explanation:** The 6 different non-empty palindromic subsequences are 'b', 'c', 'bb', 'cc', 'bcb', 'bccb'. Note that 'bcb' is counted only once, even though it occurs twice.
20+
21+
**Example 2:**
22+
23+
**Input:** s = "abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba"
24+
25+
**Output:** 104860361
26+
27+
**Explanation:** There are 3104860382 different non-empty palindromic subsequences, which is 104860361 modulo 10<sup>9</sup> + 7.
28+
29+
**Constraints:**
30+
31+
* `1 <= s.length <= 1000`
32+
* `s[i]` is either `'a'`, `'b'`, `'c'`, or `'d'`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g0701_0800.s0731_my_calendar_ii;
2+
3+
// #Medium #Design #Ordered_Set #Segment_Tree
4+
5+
import java.util.TreeMap;
6+
7+
public class MyCalendarTwo {
8+
9+
private final TreeMap<Integer, Integer> map;
10+
private final TreeMap<Integer, Integer> overlap;
11+
12+
public MyCalendarTwo() {
13+
map = new TreeMap<>();
14+
overlap = new TreeMap<>();
15+
}
16+
17+
public boolean book(int start, int end) {
18+
Integer ol = overlap.lowerKey(end);
19+
if (ol == null || overlap.get(ol) <= start) {
20+
while (true) {
21+
Integer lower = map.lowerKey(end);
22+
if (lower == null || map.get(lower) <= start) {
23+
break;
24+
}
25+
overlap.put(Math.max(start, lower), Math.min(end, map.get(lower)));
26+
start = Math.min(start, lower);
27+
end = Math.max(end, map.get(lower));
28+
map.remove(lower);
29+
}
30+
map.put(start, end);
31+
return true;
32+
}
33+
return false;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
731\. My Calendar II
2+
3+
Medium
4+
5+
You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a **triple booking**.
6+
7+
A **triple booking** happens when three events have some non-empty intersection (i.e., some moment is common to all the three events.).
8+
9+
The event can be represented as a pair of integers `start` and `end` that represents a booking on the half-open interval `[start, end)`, the range of real numbers `x` such that `start <= x < end`.
10+
11+
Implement the `MyCalendarTwo` class:
12+
13+
* `MyCalendarTwo()` Initializes the calendar object.
14+
* `boolean book(int start, int end)` Returns `true` if the event can be added to the calendar successfully without causing a **triple booking**. Otherwise, return `false` and do not add the event to the calendar.
15+
16+
**Example 1:**
17+
18+
**Input**
19+
20+
["MyCalendarTwo", "book", "book", "book", "book", "book", "book"]
21+
22+
[[], [10, 20], [50, 60], [10, 40], [5, 15], [5, 10], [25, 55]]
23+
24+
**Output:** [null, true, true, true, false, true, true]
25+
26+
**Explanation:**
27+
28+
MyCalendarTwo myCalendarTwo = new MyCalendarTwo();
29+
myCalendarTwo.book(10, 20); // return True, The event can be booked.
30+
myCalendarTwo.book(50, 60); // return True, The event can be booked.
31+
myCalendarTwo.book(10, 40); // return True, The event can be double booked.
32+
myCalendarTwo.book(5, 15); // return False, The event ca not be booked, because it would result in a triple booking.
33+
myCalendarTwo.book(5, 10); // return True, The event can be booked, as it does not use time 10 which is already double booked.
34+
myCalendarTwo.book(25, 55); // return True, The event can be booked, as the time in [25, 40) will be double booked with the third event, the time [40, 50) will be single booked, and the time [50, 55) will be double booked with the second event.
35+
36+
**Constraints:**
37+
38+
* <code>0 <= start < end <= 10<sup>9</sup></code>
39+
* At most `1000` calls will be made to `book`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package g0701_0800.s0732_my_calendar_iii;
2+
3+
// #Hard #Design #Ordered_Set #Segment_Tree
4+
5+
public class MyCalendarThree {
6+
private final Node root;
7+
8+
public MyCalendarThree() {
9+
root = new Node(0, 1000000000, 0);
10+
}
11+
12+
public int book(int start, int end) {
13+
updateTree(root, start, end - 1);
14+
return root.val;
15+
}
16+
17+
private void updateTree(Node root, int start, int end) {
18+
if (root == null) {
19+
return;
20+
}
21+
if (root.low >= start && root.high <= end) {
22+
root.val++;
23+
if (root.left != null) {
24+
updateTree(root.left, start, end);
25+
}
26+
if (root.right != null) {
27+
updateTree(root.right, start, end);
28+
}
29+
return;
30+
}
31+
32+
int mid = (root.low + root.high) / 2;
33+
if (root.left == null) {
34+
root.left = new Node(root.low, mid, root.val);
35+
}
36+
37+
if (root.right == null) {
38+
root.right = new Node(mid + 1, root.high, root.val);
39+
}
40+
41+
if (start <= mid) {
42+
updateTree(root.left, start, end);
43+
}
44+
45+
if (end > mid) {
46+
updateTree(root.right, start, end);
47+
}
48+
49+
root.val = Math.max(root.left.val, root.right.val);
50+
}
51+
52+
static class Node {
53+
int low;
54+
int high;
55+
int val;
56+
Node left;
57+
Node right;
58+
59+
public Node(int low, int high, int val) {
60+
this.low = low;
61+
this.high = high;
62+
this.val = val;
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)