Skip to content

Commit 7c38e1d

Browse files
authored
Added tasks 2038, 2040, 2042, 2043, 2044.
1 parent b564dd4 commit 7c38e1d

File tree

15 files changed

+581
-0
lines changed

15 files changed

+581
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g2001_2100.s2038_remove_colored_pieces_if_both_neighbors_are_the_same_color;
2+
3+
// #Medium #String #Math #Greedy #Game_Theory #2022_05_26_Time_22_ms_(47.78%)_Space_53.9_MB_(61.32%)
4+
5+
public class Solution {
6+
public boolean winnerOfGame(String colors) {
7+
int ans = 0;
8+
for (int i = 1; i < colors.length() - 1; i++) {
9+
if (colors.charAt(i) == colors.charAt(i - 1)
10+
&& colors.charAt(i) == colors.charAt(i + 1)) {
11+
if (colors.charAt(i) == 'A') {
12+
ans++;
13+
} else {
14+
ans--;
15+
}
16+
}
17+
}
18+
return ans > 0;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2038\. Remove Colored Pieces if Both Neighbors are the Same Color
2+
3+
Medium
4+
5+
There are `n` pieces arranged in a line, and each piece is colored either by `'A'` or by `'B'`. You are given a string `colors` of length `n` where `colors[i]` is the color of the <code>i<sup>th</sup></code> piece.
6+
7+
Alice and Bob are playing a game where they take **alternating turns** removing pieces from the line. In this game, Alice moves **first**.
8+
9+
* Alice is only allowed to remove a piece colored `'A'` if **both its neighbors** are also colored `'A'`. She is **not allowed** to remove pieces that are colored `'B'`.
10+
* Bob is only allowed to remove a piece colored `'B'` if **both its neighbors** are also colored `'B'`. He is **not allowed** to remove pieces that are colored `'A'`.
11+
* Alice and Bob **cannot** remove pieces from the edge of the line.
12+
* If a player cannot make a move on their turn, that player **loses** and the other player **wins**.
13+
14+
Assuming Alice and Bob play optimally, return `true` _if Alice wins, or return_ `false` _if Bob wins_.
15+
16+
**Example 1:**
17+
18+
**Input:** colors = "AAABABB"
19+
20+
**Output:** true
21+
22+
**Explanation:**
23+
24+
AAABABB -> AABABB
25+
26+
Alice moves first.
27+
28+
She removes the second 'A' from the left since that is the only 'A' whose neighbors are both 'A'.
29+
30+
Now it's Bob's turn.
31+
32+
Bob cannot make a move on his turn since there are no 'B's whose neighbors are both 'B'.
33+
34+
Thus, Alice wins, so return true.
35+
36+
**Example 2:**
37+
38+
**Input:** colors = "AA"
39+
40+
**Output:** false
41+
42+
**Explanation:**
43+
44+
Alice has her turn first.
45+
46+
There are only two 'A's and both are on the edge of the line, so she cannot move on her turn.
47+
48+
Thus, Bob wins, so return false.
49+
50+
**Example 3:**
51+
52+
**Input:** colors = "ABBBBBBBAAA"
53+
54+
**Output:** false
55+
56+
**Explanation:**
57+
58+
ABBBBBBBAAA -> ABBBBBBBAA
59+
60+
Alice moves first.
61+
62+
Her only option is to remove the second to last 'A' from the right.
63+
64+
ABBBBBBBAA -> ABBBBBBAA
65+
66+
Next is Bob's turn.
67+
68+
He has many options for which 'B' piece to remove.
69+
70+
He can pick any. On Alice's second turn, she has no more pieces that she can remove.
71+
72+
Thus, Bob wins, so return false.
73+
74+
**Constraints:**
75+
76+
* <code>1 <= colors.length <= 10<sup>5</sup></code>
77+
* `colors` consists of only the letters `'A'` and `'B'`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package g2001_2100.s2040_kth_smallest_product_of_two_sorted_arrays;
2+
3+
// #Hard #Array #Binary_Search #2022_05_26_Time_635_ms_(75.24%)_Space_50.6_MB_(92.26%)
4+
5+
public class Solution {
6+
static long inf = (long) 1e10;
7+
8+
public long kthSmallestProduct(int[] nums1, int[] nums2, long k) {
9+
int n = nums2.length;
10+
long lo = -inf - 1;
11+
long hi = inf + 1;
12+
while (lo < hi) {
13+
long mid = lo + ((hi - lo) >> 1);
14+
long cnt = 0;
15+
for (int i : nums1) {
16+
int l = 0;
17+
int r = n - 1;
18+
int p = 0;
19+
if (0 <= i) {
20+
while (l <= r) {
21+
int c = l + ((r - l) >> 1);
22+
long mul = i * (long) nums2[c];
23+
if (mul <= mid) {
24+
p = c + 1;
25+
l = c + 1;
26+
} else {
27+
r = c - 1;
28+
}
29+
}
30+
} else {
31+
while (l <= r) {
32+
int c = l + ((r - l) >> 1);
33+
long mul = i * (long) nums2[c];
34+
if (mul <= mid) {
35+
p = n - c;
36+
r = c - 1;
37+
} else {
38+
l = c + 1;
39+
}
40+
}
41+
}
42+
cnt += p;
43+
}
44+
if (cnt >= k) {
45+
hi = mid;
46+
} else {
47+
lo = mid + 1L;
48+
}
49+
}
50+
return lo;
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2040\. Kth Smallest Product of Two Sorted Arrays
2+
3+
Hard
4+
5+
Given two **sorted 0-indexed** integer arrays `nums1` and `nums2` as well as an integer `k`, return _the_ <code>k<sup>th</sup></code> _(**1-based**) smallest product of_ `nums1[i] * nums2[j]` _where_ `0 <= i < nums1.length` _and_ `0 <= j < nums2.length`.
6+
7+
**Example 1:**
8+
9+
**Input:** nums1 = [2,5], nums2 = [3,4], k = 2
10+
11+
**Output:** 8
12+
13+
**Explanation:** The 2 smallest products are:
14+
15+
- nums1[0] \* nums2[0] = 2 \* 3 = 6
16+
17+
- nums1[0] \* nums2[1] = 2 \* 4 = 8
18+
19+
The 2<sup>nd</sup> smallest product is 8.
20+
21+
**Example 2:**
22+
23+
**Input:** nums1 = [-4,-2,0,3], nums2 = [2,4], k = 6
24+
25+
**Output:** 0
26+
27+
**Explanation:** The 6 smallest products are:
28+
29+
- nums1[0] \* nums2[1] = (-4) \* 4 = -16
30+
31+
- nums1[0] \* nums2[0] = (-4) \* 2 = -8
32+
33+
- nums1[1] \* nums2[1] = (-2) \* 4 = -8
34+
35+
- nums1[1] \* nums2[0] = (-2) \* 2 = -4
36+
37+
- nums1[2] \* nums2[0] = 0 \* 2 = 0
38+
39+
- nums1[2] \* nums2[1] = 0 \* 4 = 0
40+
41+
The 6<sup>th</sup> smallest product is 0.
42+
43+
**Example 3:**
44+
45+
**Input:** nums1 = [-2,-1,0,1,2], nums2 = [-3,-1,2,4,5], k = 3
46+
47+
**Output:** -6
48+
49+
**Explanation:** The 3 smallest products are:
50+
51+
- nums1[0] \* nums2[4] = (-2) \* 5 = -10
52+
53+
- nums1[0] \* nums2[3] = (-2) \* 4 = -8
54+
55+
- nums1[4] \* nums2[0] = 2 \* (-3) = -6
56+
57+
The 3<sup>rd</sup> smallest product is -6.
58+
59+
**Constraints:**
60+
61+
* <code>1 <= nums1.length, nums2.length <= 5 * 10<sup>4</sup></code>
62+
* <code>-10<sup>5</sup> <= nums1[i], nums2[j] <= 10<sup>5</sup></code>
63+
* `1 <= k <= nums1.length * nums2.length`
64+
* `nums1` and `nums2` are sorted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g2001_2100.s2042_check_if_numbers_are_ascending_in_a_sentence;
2+
3+
// #Easy #String #2022_05_26_Time_2_ms_(75.46%)_Space_42.7_MB_(29.81%)
4+
5+
public class Solution {
6+
public boolean areNumbersAscending(String s) {
7+
String[] words = s.split("\\ ");
8+
int prev = 0;
9+
for (String word : words) {
10+
if (Character.isDigit(word.charAt(0))) {
11+
if (Integer.parseInt(word) <= prev) {
12+
return false;
13+
} else {
14+
prev = Integer.parseInt(word);
15+
}
16+
}
17+
}
18+
return true;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2042\. Check if Numbers Are Ascending in a Sentence
2+
3+
Easy
4+
5+
A sentence is a list of **tokens** separated by a **single** space with no leading or trailing spaces. Every token is either a **positive number** consisting of digits `0-9` with no leading zeros, or a **word** consisting of lowercase English letters.
6+
7+
* For example, `"a puppy has 2 eyes 4 legs"` is a sentence with seven tokens: `"2"` and `"4"` are numbers and the other tokens such as `"puppy"` are words.
8+
9+
Given a string `s` representing a sentence, you need to check if **all** the numbers in `s` are **strictly increasing** from left to right (i.e., other than the last number, **each** number is **strictly smaller** than the number on its **right** in `s`).
10+
11+
Return `true` _if so, or_ `false` _otherwise_.
12+
13+
**Example 1:**
14+
15+
![example-1](https://assets.leetcode.com/uploads/2021/09/30/example1.png)
16+
17+
**Input:** s = "1 box has 3 blue 4 red 6 green and 12 yellow marbles"
18+
19+
**Output:** true
20+
21+
**Explanation:** The numbers in s are: 1, 3, 4, 6, 12. They are strictly increasing from left to right: 1 < 3 < 4 < 6 < 12.
22+
23+
**Example 2:**
24+
25+
**Input:** s = "hello world 5 x 5"
26+
27+
**Output:** false
28+
29+
**Explanation:** The numbers in s are: **5**, **5**. They are not strictly increasing.
30+
31+
**Example 3:**
32+
33+
![example-3](https://assets.leetcode.com/uploads/2021/09/30/example3.png)
34+
35+
**Input:** s = "sunset is at 7 51 pm overnight lows will be in the low 50 and 60 s"
36+
37+
**Output:** false
38+
39+
**Explanation:** The numbers in s are: 7, **51**, **50**, 60. They are not strictly increasing.
40+
41+
**Constraints:**
42+
43+
* `3 <= s.length <= 200`
44+
* `s` consists of lowercase English letters, spaces, and digits from `0` to `9`, inclusive.
45+
* The number of tokens in `s` is between `2` and `100`, inclusive.
46+
* The tokens in `s` are separated by a single space.
47+
* There are at least **two** numbers in `s`.
48+
* Each number in `s` is a **positive** number **less** than `100`, with no leading zeros.
49+
* `s` contains no leading or trailing spaces.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g2001_2100.s2043_simple_bank_system;
2+
3+
// #Medium #Array #Hash_Table #Design #Simulation
4+
// #2022_05_26_Time_159_ms_(48.01%)_Space_133.1_MB_(20.58%)
5+
6+
public class Bank {
7+
private final long[] accounts;
8+
9+
public Bank(long[] balance) {
10+
accounts = balance;
11+
}
12+
13+
private boolean validate(int account, long money, boolean withdraw) {
14+
return account < accounts.length && (!withdraw || accounts[account] >= money);
15+
}
16+
17+
public boolean transfer(int account1, int account2, long money) {
18+
if (validate(account1 - 1, money, true) && validate(account2 - 1, 0, false)) {
19+
accounts[account1 - 1] -= money;
20+
accounts[account2 - 1] += money;
21+
return true;
22+
}
23+
return false;
24+
}
25+
26+
public boolean deposit(int account, long money) {
27+
if (validate(account - 1, money, false)) {
28+
accounts[account - 1] += money;
29+
return true;
30+
}
31+
return false;
32+
}
33+
34+
public boolean withdraw(int account, long money) {
35+
if (validate(account - 1, money, true)) {
36+
accounts[account - 1] -= money;
37+
return true;
38+
}
39+
return false;
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2043\. Simple Bank System
2+
3+
Medium
4+
5+
You have been tasked with writing a program for a popular bank that will automate all its incoming transactions (transfer, deposit, and withdraw). The bank has `n` accounts numbered from `1` to `n`. The initial balance of each account is stored in a **0-indexed** integer array `balance`, with the <code>(i + 1)<sup>th</sup></code> account having an initial balance of `balance[i]`.
6+
7+
Execute all the **valid** transactions. A transaction is **valid** if:
8+
9+
* The given account number(s) are between `1` and `n`, and
10+
* The amount of money withdrawn or transferred from is **less than or equal** to the balance of the account.
11+
12+
Implement the `Bank` class:
13+
14+
* `Bank(long[] balance)` Initializes the object with the **0-indexed** integer array `balance`.
15+
* `boolean transfer(int account1, int account2, long money)` Transfers `money` dollars from the account numbered `account1` to the account numbered `account2`. Return `true` if the transaction was successful, `false` otherwise.
16+
* `boolean deposit(int account, long money)` Deposit `money` dollars into the account numbered `account`. Return `true` if the transaction was successful, `false` otherwise.
17+
* `boolean withdraw(int account, long money)` Withdraw `money` dollars from the account numbered `account`. Return `true` if the transaction was successful, `false` otherwise.
18+
19+
**Example 1:**
20+
21+
**Input** ["Bank", "withdraw", "transfer", "deposit", "transfer", "withdraw"] [[[10, 100, 20, 50, 30]], [3, 10], [5, 1, 20], [5, 20], [3, 4, 15], [10, 50]]
22+
23+
**Output:** [null, true, true, true, false, false]
24+
25+
**Explanation:**
26+
27+
Bank bank = new Bank([10, 100, 20, 50, 30]);
28+
29+
bank.withdraw(3, 10); // return true, account 3 has a balance of $20, so it is valid to withdraw $10.
30+
// Account 3 has $20 - $10 = $10.
31+
32+
bank.transfer(5, 1, 20); // return true, account 5 has a balance of $30, so it is valid to transfer $20.
33+
// Account 5 has $30 - $20 = $10, and account 1 has $10 + $20 = $30.
34+
35+
bank.deposit(5, 20); // return true, it is valid to deposit $20 to account 5.
36+
// Account 5 has $10 + $20 = $30.
37+
38+
bank.transfer(3, 4, 15); // return false, the current balance of account 3 is $10,
39+
// so it is invalid to transfer $15 from it.
40+
41+
bank.withdraw(10, 50); // return false, it is invalid because account 10 does not exist.
42+
43+
**Constraints:**
44+
45+
* `n == balance.length`
46+
* <code>1 <= n, account, account1, account2 <= 10<sup>5</sup></code>
47+
* <code>0 <= balance[i], money <= 10<sup>12</sup></code>
48+
* At most <code>10<sup>4</sup></code> calls will be made to **each** function `transfer`, `deposit`, `withdraw`.

0 commit comments

Comments
 (0)