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

Commit9ad106b

Browse files
authored
Added tasks 1061, 1068
1 parentf1c9ed3 commit9ad106b

File tree

7 files changed

+278
-1
lines changed

7 files changed

+278
-1
lines changed

‎settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
rootProject.name='leetcode-in-java'
1+
rootProject.name='LeetCode-in-Java'
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
packageg1001_1100.s1061_lexicographically_smallest_equivalent_string;
2+
3+
// #Medium #String #Union_Find #2023_06_09_Time_2_ms_(93.75%)_Space_41.7_MB_(41.30%)
4+
5+
publicclassSolution {
6+
privateint[]parent;
7+
8+
publicStringsmallestEquivalentString(Strings1,Strings2,StringbaseStr) {
9+
parent =newint[26];
10+
intn =s1.length();
11+
StringBuilderresult =newStringBuilder();
12+
for (inti =0;i <26;i++) {
13+
parent[i] =i;
14+
}
15+
for (inti =0;i <n;i++) {
16+
union(s1.charAt(i) -'a',s2.charAt(i) -'a');
17+
}
18+
charbase ='a';
19+
for (charelement :baseStr.toCharArray()) {
20+
result.append(Character.toString(base +find(element -'a')));
21+
}
22+
returnresult.toString();
23+
}
24+
25+
privatevoidunion(inta,intb) {
26+
intparentA =find(a);
27+
intparentB =find(b);
28+
if (parentA !=parentB) {
29+
if (parentA <parentB) {
30+
parent[parentB] =parentA;
31+
}else {
32+
parent[parentA] =parentB;
33+
}
34+
}
35+
}
36+
37+
privateintfind(intx) {
38+
while (parent[x] !=x) {
39+
x =parent[x];
40+
}
41+
returnx;
42+
}
43+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
1061\. Lexicographically Smallest Equivalent String
2+
3+
Medium
4+
5+
You are given two strings of the same length`s1` and`s2` and a string`baseStr`.
6+
7+
We say`s1[i]` and`s2[i]` are equivalent characters.
8+
9+
* For example, if`s1 = "abc"` and`s2 = "cde"`, then we have`'a' == 'c'`,`'b' == 'd'`, and`'c' == 'e'`.
10+
11+
Equivalent characters follow the usual rules of any equivalence relation:
12+
13+
***Reflexivity:**`'a' == 'a'`.
14+
***Symmetry:**`'a' == 'b'` implies`'b' == 'a'`.
15+
***Transitivity:**`'a' == 'b'` and`'b' == 'c'` implies`'a' == 'c'`.
16+
17+
For example, given the equivalency information from`s1 = "abc"` and`s2 = "cde"`,`"acd"` and`"aab"` are equivalent strings of`baseStr = "eed"`, and`"aab"` is the lexicographically smallest equivalent string of`baseStr`.
18+
19+
Return_the lexicographically smallest equivalent string of_`baseStr`_by using the equivalency information from_`s1`_and_`s2`.
20+
21+
**Example 1:**
22+
23+
**Input:** s1 = "parker", s2 = "morris", baseStr = "parser"
24+
25+
**Output:** "makkek"
26+
27+
**Explanation:** Based on the equivalency information in s1 and s2, we can group their characters as[m,p],[a,o],[k,r,s],[e,i].
28+
29+
The characters in each group are equivalent and sorted in lexicographical order.
30+
31+
So the answer is "makkek".
32+
33+
**Example 2:**
34+
35+
**Input:** s1 = "hello", s2 = "world", baseStr = "hold"
36+
37+
**Output:** "hdld"
38+
39+
**Explanation:** Based on the equivalency information in s1 and s2, we can group their characters as[h,w],[d,e,o],[l,r].
40+
41+
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
42+
43+
**Example 3:**
44+
45+
**Input:** s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
46+
47+
**Output:** "aauaaaaada"
48+
49+
**Explanation:** We group the equivalent characters in s1 and s2 as[a,o,e,r,s,c],[l,p],[g,t] and[d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
50+
51+
**Constraints:**
52+
53+
*`1 <= s1.length, s2.length, baseStr <= 1000`
54+
*`s1.length == s2.length`
55+
*`s1`,`s2`, and`baseStr` consist of lowercase English letters.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
1068\. Product Sales Analysis I
2+
3+
Easy
4+
5+
SQL Schema
6+
7+
Table:`Sales`
8+
9+
+-------------+-------+
10+
| Column Name | Type |
11+
+-------------+-------+
12+
| sale_id | int |
13+
| product_id | int |
14+
| year | int |
15+
| quantity | int |
16+
| price | int |
17+
+-------------+-------+
18+
19+
(sale_id, year) is the primary key of this table.
20+
21+
product_id is a foreign key to`Product` table.
22+
23+
Each row of this table shows a sale on the product product_id in a certain year.
24+
25+
Note that the price is per unit.
26+
27+
Table:`Product`
28+
29+
+--------------+---------+
30+
| Column Name | Type |
31+
+--------------+---------+
32+
| product_id | int |
33+
| product_name | varchar |
34+
+--------------+---------+
35+
36+
product_id is the primary key of this table.
37+
38+
Each row of this table indicates the product name of each product.
39+
40+
Write an SQL query that reports the`product_name`,`year`, and`price` for each`sale_id` in the`Sales` table.
41+
42+
Return the resulting table in**any order**.
43+
44+
The query result format is in the following example.
45+
46+
**Example 1:**
47+
48+
**Input:** Sales table:
49+
50+
+---------+------------+------+----------+-------+
51+
| sale_id | product_id | year | quantity | price |
52+
+---------+------------+------+----------+-------+
53+
| 1 | 100 | 2008 | 10 | 5000 |
54+
| 2 | 100 | 2009 | 12 | 5000 |
55+
| 7 | 200 | 2011 | 15 | 9000 |
56+
+---------+------------+------+----------+-------+
57+
58+
Product table:
59+
60+
+------------+--------------+
61+
| product_id | product_name |
62+
+------------+--------------+
63+
| 100 | Nokia |
64+
| 200 | Apple |
65+
| 300 | Samsung |
66+
+------------+--------------+
67+
68+
**Output:**
69+
70+
+--------------+-------+-------+
71+
| product_name | year | price |
72+
+--------------+-------+-------+
73+
| Nokia | 2008 | 5000 |
74+
| Nokia | 2009 | 5000 |
75+
| Apple | 2011 | 9000 |
76+
+--------------+-------+-------+
77+
78+
**Explanation:** From sale\_id = 1, we can conclude that Nokia was sold for 5000 in the year 2008. From sale\_id = 2, we can conclude that Nokia was sold for 5000 in the year 2009. From sale\_id = 7, we can conclude that Apple was sold for 9000 in the year 2011.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Write your MySQL query statement below
2+
# #Easy #Database #2023_06_09_Time_1788_ms_(91.74%)_Space_0B_(100.00%)
3+
selectproduct.product_name,sales.sale_year,sales.pricefrom sales
4+
left join productonproduct.product_id=sales.product_id
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
packageg1001_1100.s1061_lexicographically_smallest_equivalent_string;
2+
3+
importstaticorg.hamcrest.CoreMatchers.equalTo;
4+
importstaticorg.hamcrest.MatcherAssert.assertThat;
5+
6+
importorg.junit.jupiter.api.Test;
7+
8+
classSolutionTest {
9+
@Test
10+
voidsmallestEquivalentString() {
11+
assertThat(
12+
newSolution().smallestEquivalentString("hello","world","hold"),equalTo("hdld"));
13+
}
14+
15+
@Test
16+
voidsmallestEquivalentString2() {
17+
assertThat(
18+
newSolution().smallestEquivalentString("parker","morris","parser"),
19+
equalTo("makkek"));
20+
}
21+
22+
@Test
23+
voidsmallestEquivalentString3() {
24+
assertThat(
25+
newSolution().smallestEquivalentString("leetcode","programs","sourcecode"),
26+
equalTo("aauaaaaada"));
27+
}
28+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
packageg1001_1100.s1068_product_sales_analysis_i;
2+
3+
importstaticorg.hamcrest.CoreMatchers.equalTo;
4+
importstaticorg.hamcrest.MatcherAssert.assertThat;
5+
6+
importjava.io.BufferedReader;
7+
importjava.io.FileNotFoundException;
8+
importjava.io.FileReader;
9+
importjava.sql.Connection;
10+
importjava.sql.ResultSet;
11+
importjava.sql.SQLException;
12+
importjava.sql.Statement;
13+
importjava.util.stream.Collectors;
14+
importjavax.sql.DataSource;
15+
importorg.junit.jupiter.api.Test;
16+
importorg.zapodot.junit.db.annotations.EmbeddedDatabase;
17+
importorg.zapodot.junit.db.annotations.EmbeddedDatabaseTest;
18+
importorg.zapodot.junit.db.common.CompatibilityMode;
19+
20+
@EmbeddedDatabaseTest(
21+
compatibilityMode =CompatibilityMode.MySQL,
22+
initialSqls =
23+
"CREATE TABLE Sales(sale_id INTEGER, product_id INTEGER,"
24+
+" sale_year INTEGER, quantity INTEGER, price INTEGER); "
25+
+"INSERT INTO Sales(sale_id, product_id, sale_year, quantity, price)"
26+
+" VALUES (1, 100, 2008, 10, 5000); "
27+
+"INSERT INTO Sales(sale_id, product_id, sale_year, quantity, price)"
28+
+" VALUES (2, 100, 2009, 12, 5000); "
29+
+"INSERT INTO Sales(sale_id, product_id, sale_year, quantity, price)"
30+
+" VALUES (7, 200, 2011, 15, 9000); "
31+
+"CREATE TABLE Product(product_id INTEGER, product_name VARCHAR); "
32+
+"INSERT INTO Product(product_id, product_name)"
33+
+" VALUES (100, 'Nokia'); "
34+
+"INSERT INTO Product(product_id, product_name)"
35+
+" VALUES (200, 'Apple'); "
36+
+"INSERT INTO Product(product_id, product_name)"
37+
+" VALUES (300, 'Samsung'); ")
38+
classMysqlTest {
39+
@Test
40+
voidtestScript(@EmbeddedDatabaseDataSourcedataSource)
41+
throwsSQLException,FileNotFoundException {
42+
try (finalConnectionconnection =dataSource.getConnection()) {
43+
try (finalStatementstatement =connection.createStatement();
44+
finalResultSetresultSet =
45+
statement.executeQuery(
46+
newBufferedReader(
47+
newFileReader(
48+
"src/main/java/g1001_1100/"
49+
+"s1068_product_sales_analysis_i/script.sql"))
50+
.lines()
51+
.collect(Collectors.joining("\n"))
52+
.replaceAll("#.*?\\r?\\n",""))) {
53+
assertThat(resultSet.next(),equalTo(true));
54+
assertThat(resultSet.getNString(1),equalTo("Nokia"));
55+
assertThat(resultSet.getInt(2),equalTo(2008));
56+
assertThat(resultSet.getInt(3),equalTo(5000));
57+
assertThat(resultSet.next(),equalTo(true));
58+
assertThat(resultSet.getNString(1),equalTo("Nokia"));
59+
assertThat(resultSet.getInt(2),equalTo(2009));
60+
assertThat(resultSet.getInt(3),equalTo(5000));
61+
assertThat(resultSet.next(),equalTo(true));
62+
assertThat(resultSet.getNString(1),equalTo("Apple"));
63+
assertThat(resultSet.getInt(2),equalTo(2011));
64+
assertThat(resultSet.getInt(3),equalTo(9000));
65+
assertThat(resultSet.next(),equalTo(false));
66+
}
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp