Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit20ff7b7

Browse files
authored
Added tasks 3216-3220
1 parentef5af54 commit20ff7b7

File tree

14 files changed

+481
-0
lines changed

14 files changed

+481
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
packageg3201_3300.s3216_lexicographically_smallest_string_after_a_swap;
2+
3+
// #Easy #String #Greedy #2024_07_18_Time_1_ms_(100.00%)_Space_43.3_MB_(20.48%)
4+
5+
publicclassSolution {
6+
publicStringgetSmallestString(Strings) {
7+
finalchar[]arr =s.toCharArray();
8+
for (inti =1;i <arr.length;i++) {
9+
if (arr[i -1] %2 ==arr[i] %2 &&arr[i -1] >arr[i]) {
10+
finalchartemp =arr[i];
11+
arr[i] =arr[i -1];
12+
arr[i -1] =temp;
13+
break;
14+
}
15+
}
16+
returnString.valueOf(arr);
17+
}
18+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
3216\. Lexicographically Smallest String After a Swap
2+
3+
Easy
4+
5+
Given a string`s` containing only digits, return the lexicographically smallest string that can be obtained after swapping**adjacent** digits in`s` with the same**parity** at most**once**.
6+
7+
Digits have the same parity if both are odd or both are even. For example, 5 and 9, as well as 2 and 4, have the same parity, while 6 and 9 do not.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "45320"
12+
13+
**Output:** "43520"
14+
15+
**Explanation:**
16+
17+
`s[1] == '5'` and`s[2] == '3'` both have the same parity, and swapping them results in the lexicographically smallest string.
18+
19+
**Example 2:**
20+
21+
**Input:** s = "001"
22+
23+
**Output:** "001"
24+
25+
**Explanation:**
26+
27+
There is no need to perform a swap because`s` is already the lexicographically smallest.
28+
29+
**Constraints:**
30+
31+
*`2 <= s.length <= 100`
32+
*`s` consists only of digits.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
packageg3201_3300.s3217_delete_nodes_from_linked_list_present_in_array;
2+
3+
importcom_github_leetcode.ListNode;
4+
5+
// #Medium #Array #Hash_Table #Linked_List #2024_07_18_Time_3_ms_(100.00%)_Space_63.9_MB_(93.81%)
6+
7+
/**
8+
* Definition for singly-linked list. public class ListNode { int val; ListNode next; ListNode() {}
9+
* ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val;
10+
* this.next = next; } }
11+
*/
12+
publicclassSolution {
13+
publicListNodemodifiedList(int[]nums,ListNodehead) {
14+
intmaxv =0;
15+
for (intv :nums) {
16+
maxv =Math.max(maxv,v);
17+
}
18+
boolean[]rem =newboolean[maxv +1];
19+
for (intv :nums) {
20+
rem[v] =true;
21+
}
22+
ListNodeh =newListNode(0);
23+
ListNodet =h;
24+
ListNodep =head;
25+
while (p !=null) {
26+
if (p.val >maxv || !rem[p.val]) {
27+
t.next =p;
28+
t =p;
29+
}
30+
p =p.next;
31+
}
32+
t.next =null;
33+
returnh.next;
34+
}
35+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
3217\. Delete Nodes From Linked List Present in Array
2+
3+
Medium
4+
5+
You are given an array of integers`nums` and the`head` of a linked list. Return the`head` of the modified linked list after**removing** all nodes from the linked list that have a value that exists in`nums`.
6+
7+
**Example 1:**
8+
9+
**Input:** nums =[1,2,3], head =[1,2,3,4,5]
10+
11+
**Output:**[4,5]
12+
13+
**Explanation:**
14+
15+
**![](https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample0.png)**
16+
17+
Remove the nodes with values 1, 2, and 3.
18+
19+
**Example 2:**
20+
21+
**Input:** nums =[1], head =[1,2,1,2,1,2]
22+
23+
**Output:**[2,2,2]
24+
25+
**Explanation:**
26+
27+
![](https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample1.png)
28+
29+
Remove the nodes with value 1.
30+
31+
**Example 3:**
32+
33+
**Input:** nums =[5], head =[1,2,3,4]
34+
35+
**Output:**[1,2,3,4]
36+
37+
**Explanation:**
38+
39+
**![](https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample2.png)**
40+
41+
No node has value 5.
42+
43+
**Constraints:**
44+
45+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
46+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
47+
* All elements in`nums` are unique.
48+
* The number of nodes in the given list is in the range <code>[1, 10<sup>5</sup>]</code>.
49+
* <code>1 <= Node.val <= 10<sup>5</sup></code>
50+
* The input is generated such that there is at least one node in the linked list that has a value not present in`nums`.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
packageg3201_3300.s3218_minimum_cost_for_cutting_cake_i;
2+
3+
// #Medium #Array #Dynamic_Programming #Sorting #Greedy
4+
// #2024_07_18_Time_0_ms_(100.00%)_Space_42.4_MB_(32.85%)
5+
6+
publicclassSolution {
7+
publicintminimumCost(intignoredM,intignoredN,int[]horizontalCut,int[]verticalCut) {
8+
intsum =0;
9+
for (inthc :horizontalCut) {
10+
sum +=hc;
11+
}
12+
for (intvc :verticalCut) {
13+
sum +=vc;
14+
}
15+
for (inthc :horizontalCut) {
16+
for (intvc :verticalCut) {
17+
sum +=Math.min(hc,vc);
18+
}
19+
}
20+
returnsum;
21+
}
22+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
3218\. Minimum Cost for Cutting Cake I
2+
3+
Medium
4+
5+
There is an`m x n` cake that needs to be cut into`1 x 1` pieces.
6+
7+
You are given integers`m`,`n`, and two arrays:
8+
9+
*`horizontalCut` of size`m - 1`, where`horizontalCut[i]` represents the cost to cut along the horizontal line`i`.
10+
*`verticalCut` of size`n - 1`, where`verticalCut[j]` represents the cost to cut along the vertical line`j`.
11+
12+
In one operation, you can choose any piece of cake that is not yet a`1 x 1` square and perform one of the following cuts:
13+
14+
1. Cut along a horizontal line`i` at a cost of`horizontalCut[i]`.
15+
2. Cut along a vertical line`j` at a cost of`verticalCut[j]`.
16+
17+
After the cut, the piece of cake is divided into two distinct pieces.
18+
19+
The cost of a cut depends only on the initial cost of the line and does not change.
20+
21+
Return the**minimum** total cost to cut the entire cake into`1 x 1` pieces.
22+
23+
**Example 1:**
24+
25+
**Input:** m = 3, n = 2, horizontalCut =[1,3], verticalCut =[5]
26+
27+
**Output:** 13
28+
29+
**Explanation:**
30+
31+
![](https://assets.leetcode.com/uploads/2024/06/04/ezgifcom-animated-gif-maker-1.gif)
32+
33+
* Perform a cut on the vertical line 0 with cost 5, current total cost is 5.
34+
* Perform a cut on the horizontal line 0 on`3 x 1` subgrid with cost 1.
35+
* Perform a cut on the horizontal line 0 on`3 x 1` subgrid with cost 1.
36+
* Perform a cut on the horizontal line 1 on`2 x 1` subgrid with cost 3.
37+
* Perform a cut on the horizontal line 1 on`2 x 1` subgrid with cost 3.
38+
39+
The total cost is`5 + 1 + 1 + 3 + 3 = 13`.
40+
41+
**Example 2:**
42+
43+
**Input:** m = 2, n = 2, horizontalCut =[7], verticalCut =[4]
44+
45+
**Output:** 15
46+
47+
**Explanation:**
48+
49+
* Perform a cut on the horizontal line 0 with cost 7.
50+
* Perform a cut on the vertical line 0 on`1 x 2` subgrid with cost 4.
51+
* Perform a cut on the vertical line 0 on`1 x 2` subgrid with cost 4.
52+
53+
The total cost is`7 + 4 + 4 = 15`.
54+
55+
**Constraints:**
56+
57+
*`1 <= m, n <= 20`
58+
*`horizontalCut.length == m - 1`
59+
*`verticalCut.length == n - 1`
60+
* <code>1 <= horizontalCut[i], verticalCut[i] <= 10<sup>3</sup></code>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
packageg3201_3300.s3219_minimum_cost_for_cutting_cake_ii;
2+
3+
// #Hard #Array #Sorting #Greedy #2024_07_18_Time_3_ms_(100.00%)_Space_62.6_MB_(25.82%)
4+
5+
publicclassSolution {
6+
privatestaticfinalintN =1001;
7+
privatestaticfinalint[]HORIZONTAL_COUNTS =newint[N];
8+
privatestaticfinalint[]VERTICAL_COUNTS =newint[N];
9+
10+
publiclongminimumCost(intm,intn,int[]horizontalCut,int[]verticalCut) {
11+
intmax =0;
12+
for (intx :horizontalCut) {
13+
if (x >max) {
14+
max =x;
15+
}
16+
HORIZONTAL_COUNTS[x]++;
17+
}
18+
for (intx :verticalCut) {
19+
if (x >max) {
20+
max =x;
21+
}
22+
VERTICAL_COUNTS[x]++;
23+
}
24+
longans =0;
25+
inthorizontalCount =1;
26+
intverticalCount =1;
27+
for (intx =max;x >0;x--) {
28+
ans += (long)HORIZONTAL_COUNTS[x] *x *horizontalCount;
29+
verticalCount +=HORIZONTAL_COUNTS[x];
30+
HORIZONTAL_COUNTS[x] =0;
31+
ans += (long)VERTICAL_COUNTS[x] *x *verticalCount;
32+
horizontalCount +=VERTICAL_COUNTS[x];
33+
VERTICAL_COUNTS[x] =0;
34+
}
35+
returnans;
36+
}
37+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
3219\. Minimum Cost for Cutting Cake II
2+
3+
Hard
4+
5+
There is an`m x n` cake that needs to be cut into`1 x 1` pieces.
6+
7+
You are given integers`m`,`n`, and two arrays:
8+
9+
*`horizontalCut` of size`m - 1`, where`horizontalCut[i]` represents the cost to cut along the horizontal line`i`.
10+
*`verticalCut` of size`n - 1`, where`verticalCut[j]` represents the cost to cut along the vertical line`j`.
11+
12+
In one operation, you can choose any piece of cake that is not yet a`1 x 1` square and perform one of the following cuts:
13+
14+
1. Cut along a horizontal line`i` at a cost of`horizontalCut[i]`.
15+
2. Cut along a vertical line`j` at a cost of`verticalCut[j]`.
16+
17+
After the cut, the piece of cake is divided into two distinct pieces.
18+
19+
The cost of a cut depends only on the initial cost of the line and does not change.
20+
21+
Return the**minimum** total cost to cut the entire cake into`1 x 1` pieces.
22+
23+
**Example 1:**
24+
25+
**Input:** m = 3, n = 2, horizontalCut =[1,3], verticalCut =[5]
26+
27+
**Output:** 13
28+
29+
**Explanation:**
30+
31+
![](https://assets.leetcode.com/uploads/2024/06/04/ezgifcom-animated-gif-maker-1.gif)
32+
33+
* Perform a cut on the vertical line 0 with cost 5, current total cost is 5.
34+
* Perform a cut on the horizontal line 0 on`3 x 1` subgrid with cost 1.
35+
* Perform a cut on the horizontal line 0 on`3 x 1` subgrid with cost 1.
36+
* Perform a cut on the horizontal line 1 on`2 x 1` subgrid with cost 3.
37+
* Perform a cut on the horizontal line 1 on`2 x 1` subgrid with cost 3.
38+
39+
The total cost is`5 + 1 + 1 + 3 + 3 = 13`.
40+
41+
**Example 2:**
42+
43+
**Input:** m = 2, n = 2, horizontalCut =[7], verticalCut =[4]
44+
45+
**Output:** 15
46+
47+
**Explanation:**
48+
49+
* Perform a cut on the horizontal line 0 with cost 7.
50+
* Perform a cut on the vertical line 0 on`1 x 2` subgrid with cost 4.
51+
* Perform a cut on the vertical line 0 on`1 x 2` subgrid with cost 4.
52+
53+
The total cost is`7 + 4 + 4 = 15`.
54+
55+
**Constraints:**
56+
57+
* <code>1 <= m, n <= 10<sup>5</sup></code>
58+
*`horizontalCut.length == m - 1`
59+
*`verticalCut.length == n - 1`
60+
* <code>1 <= horizontalCut[i], verticalCut[i] <= 10<sup>3</sup></code>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
3220\. Odd and Even Transactions
2+
3+
Medium
4+
5+
SQL Schema
6+
7+
Table:`transactions`
8+
9+
+------------------+------+
10+
| Column Name | Type |
11+
+------------------+------+
12+
| transaction_id | int |
13+
| amount | int |
14+
| transaction_date | date |
15+
+------------------+------+
16+
The transactions_id column uniquely identifies each row in this table.
17+
Each row of this table contains the transaction id, amount and transaction date.
18+
19+
Write a solution to find the**sum of amounts** for**odd** and**even** transactions for each day. If there are no odd or even transactions for a specific date, display as`0`.
20+
21+
Return_the result table ordered by_`transaction_date`_in**ascending** order_.
22+
23+
The result format is in the following example.
24+
25+
**Example:**
26+
27+
**Input:**
28+
29+
`transactions` table:
30+
31+
+----------------+--------+------------------+
32+
| transaction_id | amount | transaction_date |
33+
+----------------+--------+------------------+
34+
| 1 | 150 | 2024-07-01 |
35+
| 2 | 200 | 2024-07-01 |
36+
| 3 | 75 | 2024-07-01 |
37+
| 4 | 300 | 2024-07-02 |
38+
| 5 | 50 | 2024-07-02 |
39+
| 6 | 120 | 2024-07-03 |
40+
+----------------+--------+------------------+
41+
42+
**Output:**
43+
44+
+------------------+---------+----------+
45+
| transaction_date | odd_sum | even_sum |
46+
+------------------+---------+----------+
47+
| 2024-07-01 | 75 | 350 |
48+
| 2024-07-02 | 0 | 350 |
49+
| 2024-07-03 | 0 | 120 |
50+
+------------------+---------+----------+
51+
52+
**Explanation:**
53+
54+
* For transaction dates:
55+
* 2024-07-01:
56+
* Sum of amounts for odd transactions: 75
57+
* Sum of amounts for even transactions: 150 + 200 = 350
58+
* 2024-07-02:
59+
* Sum of amounts for odd transactions: 0
60+
* Sum of amounts for even transactions: 300 + 50 = 350
61+
* 2024-07-03:
62+
* Sum of amounts for odd transactions: 0
63+
* Sum of amounts for even transactions: 120
64+
65+
**Note:** The output table is ordered by`transaction_date` in ascending order.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Write your MySQL query statement below
2+
# #Medium #2024_07_18_Time_272_ms_(100.00%)_Space_0B_(100.00%)
3+
select transaction_date,
4+
sum(case when amount%2<>0 then amount else0 end)as odd_sum,
5+
sum(case when amount%2=0 then amount else0 end)as even_sumfrom transactions
6+
group by transaction_dateorder by transaction_date;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp