From b72c7846b684a801cbcebce6e4e794c7c22f6d61 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 18 Oct 2022 05:12:55 +0300 Subject: [PATCH 1/2] Added tasks 171-187. --- README.md | 26 +++++ .../s0171_excel_sheet_column_number/readme.md | 51 +++++++++ .../s0172_factorial_trailing_zeroes/readme.md | 54 +++++++++ .../readme.md | 92 +++++++++++++++ .../g0101_0200/s0174_dungeon_game/readme.md | 73 ++++++++++++ .../s0175_combine_two_tables/readme.md | 82 +++++++++++++ .../s0176_second_highest_salary/readme.md | 75 ++++++++++++ .../s0177_nth_highest_salary/readme.md | 79 +++++++++++++ .../g0101_0200/s0178_rank_scores/readme.md | 65 +++++++++++ .../g0101_0200/s0179_largest_number/readme.md | 54 +++++++++ .../s0180_consecutive_numbers/readme.md | 61 ++++++++++ .../readme.md | 59 ++++++++++ .../s0182_duplicate_emails/readme.md | 55 +++++++++ .../s0183_customers_who_never_order/readme.md | 79 +++++++++++++ .../s0184_department_highest_salary/readme.md | 95 +++++++++++++++ .../readme.md | 108 ++++++++++++++++++ .../s0187_repeated_dna_sequences/readme.md | 69 +++++++++++ 17 files changed, 1177 insertions(+) create mode 100644 src/main/kotlin/g0101_0200/s0171_excel_sheet_column_number/readme.md create mode 100644 src/main/kotlin/g0101_0200/s0172_factorial_trailing_zeroes/readme.md create mode 100644 src/main/kotlin/g0101_0200/s0173_binary_search_tree_iterator/readme.md create mode 100644 src/main/kotlin/g0101_0200/s0174_dungeon_game/readme.md create mode 100644 src/main/kotlin/g0101_0200/s0175_combine_two_tables/readme.md create mode 100644 src/main/kotlin/g0101_0200/s0176_second_highest_salary/readme.md create mode 100644 src/main/kotlin/g0101_0200/s0177_nth_highest_salary/readme.md create mode 100644 src/main/kotlin/g0101_0200/s0178_rank_scores/readme.md create mode 100644 src/main/kotlin/g0101_0200/s0179_largest_number/readme.md create mode 100644 src/main/kotlin/g0101_0200/s0180_consecutive_numbers/readme.md create mode 100644 src/main/kotlin/g0101_0200/s0181_employees_earning_more_than_their_managers/readme.md create mode 100644 src/main/kotlin/g0101_0200/s0182_duplicate_emails/readme.md create mode 100644 src/main/kotlin/g0101_0200/s0183_customers_who_never_order/readme.md create mode 100644 src/main/kotlin/g0101_0200/s0184_department_highest_salary/readme.md create mode 100644 src/main/kotlin/g0101_0200/s0185_department_top_three_salaries/readme.md create mode 100644 src/main/kotlin/g0101_0200/s0187_repeated_dna_sequences/readme.md diff --git a/README.md b/README.md index 04f51493..00626a00 100644 --- a/README.md +++ b/README.md @@ -486,6 +486,7 @@ | | | | | | |-|-|-|-|-|- | 0061 |[Rotate List](src/main/kotlin/g0001_0100/s0061_rotate_list)| Medium | Two_Pointers, Linked_List | 193 | 92.16 +| 0173 |[Binary Search Tree Iterator](src/main/kotlin/g0101_0200/s0173_binary_search_tree_iterator)| Medium | Tree, Binary_Tree, Stack, Design, Binary_Search_Tree, Iterator | 563 | 46.91 #### Day 17 @@ -588,6 +589,7 @@ | | | | | | |-|-|-|-|-|- +| 0183 |[Customers Who Never Order](src/main/kotlin/g0101_0200/s0183_customers_who_never_order)| Easy | Database | 712 | 33.67 #### Day 2 Select and Order @@ -603,11 +605,13 @@ | | | | | | |-|-|-|-|-|- +| 0176 |[Second Highest Salary](src/main/kotlin/g0101_0200/s0176_second_highest_salary)| Medium | Database | 219 | 92.54 #### Day 5 Union | | | | | | |-|-|-|-|-|- +| 0175 |[Combine Two Tables](src/main/kotlin/g0101_0200/s0175_combine_two_tables)| Easy | Database | 473 | 54.97 #### Day 6 Union @@ -633,6 +637,7 @@ | | | | | | |-|-|-|-|-|- +| 0182 |[Duplicate Emails](src/main/kotlin/g0101_0200/s0182_duplicate_emails)| Easy | Database | 396 | 68.40 ### Level 1 @@ -783,6 +788,7 @@ |-|-|-|-|-|- | 0108 |[Convert Sorted Array to Binary Search Tree](src/main/kotlin/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree)| Easy | Top_Interview_Questions, Array, Tree, Binary_Tree, Binary_Search_Tree, Divide_and_Conquer | 334 | 35.39 | 0230 |[Kth Smallest Element in a BST](src/main/kotlin/g0201_0300/s0230_kth_smallest_element_in_a_bst)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 393 | 33.33 +| 0173 |[Binary Search Tree Iterator](src/main/kotlin/g0101_0200/s0173_binary_search_tree_iterator)| Medium | Tree, Binary_Tree, Stack, Design, Binary_Search_Tree, Iterator | 563 | 46.91 #### Day 10 Graph/BFS/DFS @@ -865,6 +871,7 @@ | 0136 |[Single Number](src/main/kotlin/g0101_0200/s0136_single_number)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation | 344 | 83.63 | 0007 |[Reverse Integer](src/main/kotlin/g0001_0100/s0007_reverse_integer)| Medium | Top_Interview_Questions, Math | 245 | 60.32 | 0009 |[Palindrome Number](src/main/kotlin/g0001_0100/s0009_palindrome_number)| Easy | Math | 238 | 96.24 +| 0172 |[Factorial Trailing Zeroes](src/main/kotlin/g0101_0200/s0172_factorial_trailing_zeroes)| Medium | Top_Interview_Questions, Math | 220 | 67.65 | 0050 |[Pow(x, n)](src/main/kotlin/g0001_0100/s0050_powx_n)| Medium | Top_Interview_Questions, Math, Recursion | 264 | 52.98 #### Udemy Strings @@ -872,6 +879,7 @@ | | | | | | |-|-|-|-|-|- | 0014 |[Longest Common Prefix](src/main/kotlin/g0001_0100/s0014_longest_common_prefix)| Easy | Top_Interview_Questions, String | 209 | 88.86 +| 0187 |[Repeated DNA Sequences](src/main/kotlin/g0101_0200/s0187_repeated_dna_sequences)| Medium | String, Hash_Table, Bit_Manipulation, Sliding_Window, Hash_Function, Rolling_Hash | 319 | 79.03 | 0003 |[Longest Substring Without Repeating Characters](src/main/kotlin/g0001_0100/s0003_longest_substring_without_repeating_characters)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window | 258 | 91.09 | 0020 |[Valid Parentheses](src/main/kotlin/g0001_0100/s0020_valid_parentheses)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Stack | 226 | 72.53 | 0005 |[Longest Palindromic Substring](src/main/kotlin/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming | 323 | 75.48 @@ -1179,6 +1187,7 @@ | | | | | | |-|-|-|-|-|- +| 0187 |[Repeated DNA Sequences](src/main/kotlin/g0101_0200/s0187_repeated_dna_sequences)| Medium | String, Hash_Table, Bit_Manipulation, Sliding_Window, Hash_Function, Rolling_Hash | 319 | 79.03 | 0005 |[Longest Palindromic Substring](src/main/kotlin/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming | 323 | 75.48 #### Day 10 Linked List @@ -1234,6 +1243,7 @@ | | | | | | |-|-|-|-|-|- | 0230 |[Kth Smallest Element in a BST](src/main/kotlin/g0201_0300/s0230_kth_smallest_element_in_a_bst)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 393 | 33.33 +| 0173 |[Binary Search Tree Iterator](src/main/kotlin/g0101_0200/s0173_binary_search_tree_iterator)| Medium | Tree, Binary_Tree, Stack, Design, Binary_Search_Tree, Iterator | 563 | 46.91 #### Day 18 Tree @@ -1525,6 +1535,22 @@ | 0199 |[Binary Tree Right Side View](src/main/kotlin/g0101_0200/s0199_binary_tree_right_side_view)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_II_Day_16_Tree, Level_2_Day_15_Tree | 194 | 92.89 | 0198 |[House Robber](src/main/kotlin/g0101_0200/s0198_house_robber)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_3, Level_2_Day_12_Dynamic_Programming, Udemy_Dynamic_Programming | 156 | 92.24 | 0189 |[Rotate Array](src/main/kotlin/g0101_0200/s0189_rotate_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers, Algorithm_I_Day_2_Two_Pointers, Udemy_Arrays | 483 | 86.95 +| 0187 |[Repeated DNA Sequences](src/main/kotlin/g0101_0200/s0187_repeated_dna_sequences)| Medium | String, Hash_Table, Bit_Manipulation, Sliding_Window, Hash_Function, Rolling_Hash, Data_Structure_II_Day_9_String, Udemy_Strings | 319 | 79.03 +| 0185 |[Department Top Three Salaries](src/main/kotlin/g0101_0200/s0185_department_top_three_salaries)| Hard | Database | 757 | 87.06 +| 0184 |[Department Highest Salary](src/main/kotlin/g0101_0200/s0184_department_highest_salary)| Medium | Database | 637 | 76.01 +| 0183 |[Customers Who Never Order](src/main/kotlin/g0101_0200/s0183_customers_who_never_order)| Easy | Database, SQL_I_Day_1_Select | 712 | 33.67 +| 0182 |[Duplicate Emails](src/main/kotlin/g0101_0200/s0182_duplicate_emails)| Easy | Database, SQL_I_Day_10_Where | 396 | 68.40 +| 0181 |[Employees Earning More Than Their Managers](src/main/kotlin/g0101_0200/s0181_employees_earning_more_than_their_managers)| Easy | Database | 351 | 94.75 +| 0180 |[Consecutive Numbers](src/main/kotlin/g0101_0200/s0180_consecutive_numbers)| Medium | Database | 695 | 38.05 +| 0179 |[Largest Number](src/main/kotlin/g0101_0200/s0179_largest_number)| Medium | Top_Interview_Questions, String, Sorting, Greedy | 380 | 43.40 +| 0178 |[Rank Scores](src/main/kotlin/g0101_0200/s0178_rank_scores)| Medium | Database | 292 | 85.14 +| 0177 |[Nth Highest Salary](src/main/kotlin/g0101_0200/s0177_nth_highest_salary)| Medium | Database | 338 | 86.68 +| 0176 |[Second Highest Salary](src/main/kotlin/g0101_0200/s0176_second_highest_salary)| Medium | Database, SQL_I_Day_4_Union_and_Select | 219 | 92.54 +| 0175 |[Combine Two Tables](src/main/kotlin/g0101_0200/s0175_combine_two_tables)| Easy | Database, SQL_I_Day_5_Union | 473 | 54.97 +| 0174 |[Dungeon Game](src/main/kotlin/g0101_0200/s0174_dungeon_game)| Hard | Array, Dynamic_Programming, Matrix | 300 | 66.67 +| 0173 |[Binary Search Tree Iterator](src/main/kotlin/g0101_0200/s0173_binary_search_tree_iterator)| Medium | Tree, Binary_Tree, Stack, Design, Binary_Search_Tree, Iterator, Data_Structure_II_Day_17_Tree, Programming_Skills_II_Day_16, Level_2_Day_9_Binary_Search_Tree | 563 | 46.91 +| 0172 |[Factorial Trailing Zeroes](src/main/kotlin/g0101_0200/s0172_factorial_trailing_zeroes)| Medium | Top_Interview_Questions, Math, Udemy_Integers | 220 | 67.65 +| 0171 |[Excel Sheet Column Number](src/main/kotlin/g0101_0200/s0171_excel_sheet_column_number)| Easy | Top_Interview_Questions, String, Math | 310 | 30.68 | 0169 |[Majority Element](src/main/kotlin/g0101_0200/s0169_majority_element)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Counting, Divide_and_Conquer, Data_Structure_II_Day_1_Array, Udemy_Famous_Algorithm | 460 | 51.25 | 0168 |[Excel Sheet Column Title](src/main/kotlin/g0101_0200/s0168_excel_sheet_column_title)| Easy | String, Math | 246 | 30.12 | 0167 |[Two Sum II - Input Array Is Sorted](src/main/kotlin/g0101_0200/s0167_two_sum_ii_input_array_is_sorted)| Medium | Array, Binary_Search, Two_Pointers, Algorithm_I_Day_3_Two_Pointers, Binary_Search_I_Day_7 | 403 | 68.74 diff --git a/src/main/kotlin/g0101_0200/s0171_excel_sheet_column_number/readme.md b/src/main/kotlin/g0101_0200/s0171_excel_sheet_column_number/readme.md new file mode 100644 index 00000000..47ba54bf --- /dev/null +++ b/src/main/kotlin/g0101_0200/s0171_excel_sheet_column_number/readme.md @@ -0,0 +1,51 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 171\. Excel Sheet Column Number + +Easy + +Given a string `columnTitle` that represents the column title as appears in an Excel sheet, return _its corresponding column number_. + +For example: + +A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ... + +**Example 1:** + +**Input:** columnTitle = "A" + +**Output:** 1 + +**Example 2:** + +**Input:** columnTitle = "AB" + +**Output:** 28 + +**Example 3:** + +**Input:** columnTitle = "ZY" + +**Output:** 701 + +**Constraints:** + +* `1 <= columnTitle.length <= 7` +* `columnTitle` consists only of uppercase English letters. +* `columnTitle` is in the range `["A", "FXSHRXW"]`. + +## Solution + +```kotlin +class Solution { + fun titleToNumber(s: String): Int { + var num = 0 + var pow = 0 + for (i in s.length - 1 downTo 0) { + num += Math.pow(26.0, pow++.toDouble()).toInt() * (s[i].code - 'A'.code + 1) + } + return num + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0101_0200/s0172_factorial_trailing_zeroes/readme.md b/src/main/kotlin/g0101_0200/s0172_factorial_trailing_zeroes/readme.md new file mode 100644 index 00000000..936307b0 --- /dev/null +++ b/src/main/kotlin/g0101_0200/s0172_factorial_trailing_zeroes/readme.md @@ -0,0 +1,54 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 172\. Factorial Trailing Zeroes + +Medium + +Given an integer `n`, return _the number of trailing zeroes in_ `n!`. + +Note that `n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1`. + +**Example 1:** + +**Input:** n = 3 + +**Output:** 0 + +**Explanation:** 3! = 6, no trailing zero. + +**Example 2:** + +**Input:** n = 5 + +**Output:** 1 + +**Explanation:** 5! = 120, one trailing zero. + +**Example 3:** + +**Input:** n = 0 + +**Output:** 0 + +**Constraints:** + +* 0 <= n <= 104 + +**Follow up:** Could you write a solution that works in logarithmic time complexity? + +## Solution + +```kotlin +class Solution { + fun trailingZeroes(n: Int): Int { + var base = 5 + var count = 0 + while (n >= base) { + count += n / base + base = base * 5 + } + return count + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0101_0200/s0173_binary_search_tree_iterator/readme.md b/src/main/kotlin/g0101_0200/s0173_binary_search_tree_iterator/readme.md new file mode 100644 index 00000000..eacf1568 --- /dev/null +++ b/src/main/kotlin/g0101_0200/s0173_binary_search_tree_iterator/readme.md @@ -0,0 +1,92 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 173\. Binary Search Tree Iterator + +Medium + +Implement the `BSTIterator` class that represents an iterator over the **[in-order traversal](https://en.wikipedia.org/wiki/Tree_traversal#In-order_(LNR))** of a binary search tree (BST): + +* `BSTIterator(TreeNode root)` Initializes an object of the `BSTIterator` class. The `root` of the BST is given as part of the constructor. The pointer should be initialized to a non-existent number smaller than any element in the BST. +* `boolean hasNext()` Returns `true` if there exists a number in the traversal to the right of the pointer, otherwise returns `false`. +* `int next()` Moves the pointer to the right, then returns the number at the pointer. + +Notice that by initializing the pointer to a non-existent smallest number, the first call to `next()` will return the smallest element in the BST. + +You may assume that `next()` calls will always be valid. That is, there will be at least a next number in the in-order traversal when `next()` is called. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2018/12/25/bst-tree.png) + +**Input** ["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"] [[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []] + +**Output:** [null, 3, 7, true, 9, true, 15, true, 20, false] + +**Explanation:** BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]); bSTIterator.next(); // return 3 bSTIterator.next(); // return 7 bSTIterator.hasNext(); // return True bSTIterator.next(); // return 9 bSTIterator.hasNext(); // return True bSTIterator.next(); // return 15 bSTIterator.hasNext(); // return True bSTIterator.next(); // return 20 bSTIterator.hasNext(); // return False + +**Constraints:** + +* The number of nodes in the tree is in the range [1, 105]. +* 0 <= Node.val <= 106 +* At most 105 calls will be made to `hasNext`, and `next`. + +**Follow up:** + +* Could you implement `next()` and `hasNext()` to run in average `O(1)` time and use `O(h)` memory, where `h` is the height of the tree? + +## Solution + +```kotlin +import com_github_leetcode.TreeNode + +/* + * Example: + * var ti = TreeNode(5) + * var v = ti.`val` + * Definition for a binary tree node. + * class TreeNode(var `val`: Int) { + * var left: TreeNode? = null + * var right: TreeNode? = null + * } + */ +class BSTIterator(root: TreeNode?) { + private var node: TreeNode? + + init { + node = root + } + + operator fun next(): Int { + var res = -1 + while (node != null) { + if (node!!.left != null) { + var rightMost: TreeNode? = node!!.left + while (rightMost!!.right != null) { + rightMost = rightMost.right + } + rightMost.right = node + val temp: TreeNode? = node!!.left + node!!.left = null + node = temp + } else { + res = node!!.`val` + node = node!!.right + return res + } + } + return res + } + + operator fun hasNext(): Boolean { + return node != null + } +} + +/* + * Your BSTIterator object will be instantiated and called as such: + * var obj = BSTIterator(root) + * var param_1 = obj.next() + * var param_2 = obj.hasNext() + */ +``` \ No newline at end of file diff --git a/src/main/kotlin/g0101_0200/s0174_dungeon_game/readme.md b/src/main/kotlin/g0101_0200/s0174_dungeon_game/readme.md new file mode 100644 index 00000000..1b3c7c2b --- /dev/null +++ b/src/main/kotlin/g0101_0200/s0174_dungeon_game/readme.md @@ -0,0 +1,73 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 174\. Dungeon Game + +Hard + +The demons had captured the princess and imprisoned her in **the bottom-right corner** of a `dungeon`. The `dungeon` consists of `m x n` rooms laid out in a 2D grid. Our valiant knight was initially positioned in **the top-left room** and must fight his way through `dungeon` to rescue the princess. + +The knight has an initial health point represented by a positive integer. If at any point his health point drops to `0` or below, he dies immediately. + +Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers). + +To reach the princess as quickly as possible, the knight decides to move only **rightward** or **downward** in each step. + +Return _the knight's minimum initial health so that he can rescue the princess_. + +**Note** that any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg) + +**Input:** dungeon = \[\[-2,-3,3],[-5,-10,1],[10,30,-5]] + +**Output:** 7 + +**Explanation:** The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN. + +**Example 2:** + +**Input:** dungeon = \[\[0]] + +**Output:** 1 + +**Constraints:** + +* `m == dungeon.length` +* `n == dungeon[i].length` +* `1 <= m, n <= 200` +* `-1000 <= dungeon[i][j] <= 1000` + +## Solution + +```kotlin +class Solution { + fun calculateMinimumHP(dungeon: Array): Int { + val m = dungeon.size + val n = dungeon[0].size + val memo = Array(m) { IntArray(n) } + return Math.max(1, 1 - traverse(dungeon, 0, 0, memo)) + } + + private fun traverse(dungeon: Array, r: Int, c: Int, memo: Array): Int { + if (r == dungeon.size - 1 && c == dungeon[0].size - 1) { + return dungeon[r][c] + } + if (memo[r][c] != 0) { + return memo[r][c] + } + var res = Int.MIN_VALUE + if (r + 1 < dungeon.size) { + res = Math.max(res, traverse(dungeon, r + 1, c, memo)) + } + if (c + 1 < dungeon[0].size) { + res = Math.max(res, traverse(dungeon, r, c + 1, memo)) + } + res = Math.min(dungeon[r][c], res + dungeon[r][c]) + memo[r][c] = res + return res + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0101_0200/s0175_combine_two_tables/readme.md b/src/main/kotlin/g0101_0200/s0175_combine_two_tables/readme.md new file mode 100644 index 00000000..eb2cc4ee --- /dev/null +++ b/src/main/kotlin/g0101_0200/s0175_combine_two_tables/readme.md @@ -0,0 +1,82 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 175\. Combine Two Tables + +Easy + +SQL Schema + +Table: `Person` + + +-------------+---------+ + | Column Name | Type | + +-------------+---------+ + | personId | int | + | lastName | varchar | + | firstName | varchar | + +-------------+---------+ + + personId is the primary key column for this table. + This table contains information about the ID of some persons and their first and last names. + +Table: `Address` + + +-------------+---------+ + | Column Name | Type | + +-------------+---------+ + | addressId | int | + | personId | int | + | city | varchar | + | state | varchar | + +-------------+---------+ + + addressId is the primary key column for this table. + Each row of this table contains information about the city and state of one person with ID = PersonId. + +Write an SQL query to report the first name, last name, city, and state of each person in the `Person` table. If the address of a `personId` is not present in the `Address` table, report `null` instead. + +Return the result table in **any order**. + +The query result format is in the following example. + +**Example 1:** + +**Input:** + + Person table: + +----------+----------+-----------+ + | personId | lastName | firstName | + +----------+----------+-----------+ + | 1 | Wang | Allen | + | 2 | Alice | Bob | + +----------+----------+-----------+ + + Address table: + +-----------+----------+---------------+------------+ + | addressId | personId | city | state | + +-----------+----------+---------------+------------+ + | 1 | 2 | New York City | New York | + | 2 | 3 | Leetcode | California | + +-----------+----------+---------------+------------+ + +**Output:** + + +-----------+----------+---------------+----------+ + | firstName | lastName | city | state | + +-----------+----------+---------------+----------+ + | Allen | Wang | Null | Null | + | Bob | Alice | New York City | New York | + +-----------+----------+---------------+----------+ + +**Explanation:** + +There is no address in the address table for the personId = 1 so we return null in their city and state. addressId = 1 contains information about the address of personId = 2. + +## Solution + +```sql +# Write your MySQL query statement below +SELECT FirstName, LastName, City, State +FROM Person LEFT JOIN Address USING (PersonId) +``` \ No newline at end of file diff --git a/src/main/kotlin/g0101_0200/s0176_second_highest_salary/readme.md b/src/main/kotlin/g0101_0200/s0176_second_highest_salary/readme.md new file mode 100644 index 00000000..787d6969 --- /dev/null +++ b/src/main/kotlin/g0101_0200/s0176_second_highest_salary/readme.md @@ -0,0 +1,75 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 176\. Second Highest Salary + +Medium + +SQL Schema + +Table: `Employee` + + +-------------+------+ + | Column Name | Type | + +-------------+------+ + | id | int | + | salary | int | + +-------------+------+ + id is the primary key column for this table. + Each row of this table contains information about the salary of an employee. + +Write an SQL query to report the second highest salary from the `Employee` table. If there is no second highest salary, the query should report `null`. + +The query result format is in the following example. + +**Example 1:** + +**Input:** + + Employee table: + +----+--------+ + | id | salary | + +----+--------+ + | 1 | 100 | + | 2 | 200 | + | 3 | 300 | + +----+--------+ + +**Output:** + + +---------------------+ + | SecondHighestSalary | + +---------------------+ + | 200 | + +---------------------+ + +**Example 2:** + +**Input:** + + Employee table: + +----+--------+ + | id | salary | + +----+--------+ + | 1 | 100 | + +----+--------+ + +**Output:** + + +---------------------+ + | SecondHighestSalary | + +---------------------+ + | null | + +---------------------+ + +## Solution + +```sql +# Write your MySQL query statement below +SELECT ifnull( + (SELECT distinct(Salary) + FROM Employee + ORDER BY Salary DESC + LIMIT 1 + OFFSET 1), NULL) SecondHighestSalary; +``` \ No newline at end of file diff --git a/src/main/kotlin/g0101_0200/s0177_nth_highest_salary/readme.md b/src/main/kotlin/g0101_0200/s0177_nth_highest_salary/readme.md new file mode 100644 index 00000000..e163bb8b --- /dev/null +++ b/src/main/kotlin/g0101_0200/s0177_nth_highest_salary/readme.md @@ -0,0 +1,79 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 177\. Nth Highest Salary + +Medium + +SQL Schema + +Table: `Employee` + + +-------------+------+ + | Column Name | Type | + +-------------+------+ + | id | int | + | salary | int | + +-------------+------+ + id is the primary key column for this table. Each row of this table contains information about the salary of an employee. + +Write an SQL query to report the `nth` highest salary from the `Employee` table. If there is no `nth` highest salary, the query should report `null`. + +The query result format is in the following example. + +**Example 1:** + +**Input:** + + Employee table: + +----+--------+ + | id | salary | + +----+--------+ + | 1 | 100 | + | 2 | 200 | + | 3 | 300 | + +----+--------+ + n = 2 + +**Output:** + + +------------------------+ + | getNthHighestSalary(2) | + +------------------------+ + | 200 | + +------------------------+ + +**Example 2:** + +**Input:** + + Employee table: + +----+--------+ + | id | salary | + +----+--------+ + | 1 | 100 | + +----+--------+ + n = 2 + +**Output:** + + +------------------------+ + | getNthHighestSalary(2) | + +------------------------+ + | null | + +------------------------+ + +## Solution + +```sql +CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT +BEGIN +DECLARE M INT; +SET M=N-1; + RETURN ( + # Write your MySQL query statement below. + # #Medium #Database #2022_10_18_Time_338_ms_(86.68%)_Space_0B_(100.00%) + SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT M, 1 + ); +END +``` \ No newline at end of file diff --git a/src/main/kotlin/g0101_0200/s0178_rank_scores/readme.md b/src/main/kotlin/g0101_0200/s0178_rank_scores/readme.md new file mode 100644 index 00000000..7fe3aaad --- /dev/null +++ b/src/main/kotlin/g0101_0200/s0178_rank_scores/readme.md @@ -0,0 +1,65 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 178\. Rank Scores + +Medium + +SQL Schema + +Table: `Scores` + + +-------------+---------+ + | Column Name | Type | + +-------------+---------+ + | id | int | + | score | decimal | + +-------------+---------+ + id is the primary key for this table. + Each row of this table contains the score of a game. Score is a floating point value with two decimal places. + +Write an SQL query to rank the scores. The ranking should be calculated according to the following rules: + +* The scores should be ranked from the highest to the lowest. +* If there is a tie between two scores, both should have the same ranking. +* After a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no holes between ranks. + +Return the result table ordered by `score` in descending order. + +The query result format is in the following example. + +**Example 1:** + +**Input:** + + Scores table: + +----+-------+ + | id | score | + +----+-------+ + | 1 | 3.50 | + | 2 | 3.65 | + | 3 | 4.00 | + | 4 | 3.85 | + | 5 | 4.00 | + | 6 | 3.65 | + +----+-------+ + +**Output:** + + +-------+------+ + | score | rank | + +-------+------+ + | 4.00 | 1 | + | 4.00 | 1 | + | 3.85 | 2 | + | 3.65 | 3 | + | 3.65 | 3 | + | 3.50 | 4 | + +-------+------+ + +## Solution + +```sql +# Write your MySQL query statement below +select Score, DENSE_RANK() OVER(order by Score Desc) as "Rank" from Scores order by "Rank" Asc; +``` \ No newline at end of file diff --git a/src/main/kotlin/g0101_0200/s0179_largest_number/readme.md b/src/main/kotlin/g0101_0200/s0179_largest_number/readme.md new file mode 100644 index 00000000..6caf2884 --- /dev/null +++ b/src/main/kotlin/g0101_0200/s0179_largest_number/readme.md @@ -0,0 +1,54 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 179\. Largest Number + +Medium + +Given a list of non-negative integers `nums`, arrange them such that they form the largest number and return it. + +Since the result may be very large, so you need to return a string instead of an integer. + +**Example 1:** + +**Input:** nums = [10,2] + +**Output:** "210" + +**Example 2:** + +**Input:** nums = [3,30,34,5,9] + +**Output:** "9534330" + +**Constraints:** + +* `1 <= nums.length <= 100` +* 0 <= nums[i] <= 109 + +## Solution + +```kotlin +import java.util.Arrays + +class Solution { + fun largestNumber(nums: IntArray): String { + val n = nums.size + val s = arrayOfNulls(n) + for (i in 0 until n) { + s[i] = nums[i].toString() + } + Arrays.sort(s) { a: String?, b: String? -> + (b + a).compareTo( + a + b + ) + } + val sb = StringBuilder() + for (str in s) { + sb.append(str) + } + val result = sb.toString() + return if (result.startsWith("0")) "0" else result + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0101_0200/s0180_consecutive_numbers/readme.md b/src/main/kotlin/g0101_0200/s0180_consecutive_numbers/readme.md new file mode 100644 index 00000000..72723679 --- /dev/null +++ b/src/main/kotlin/g0101_0200/s0180_consecutive_numbers/readme.md @@ -0,0 +1,61 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 180\. Consecutive Numbers + +Medium + +SQL Schema + +Table: `Logs` + + +-------------+---------+ + | Column Name | Type | + +-------------+---------+ + | id | int | + | num | varchar | + +-------------+---------+ + id is the primary key for this table. + +Write an SQL query to find all numbers that appear at least three times consecutively. + +Return the result table in **any order**. + +The query result format is in the following example. + +**Example 1:** + +**Input:** + + Logs table: + +----+-----+ + | id | num | + +----+-----+ + | 1 | 1 | + | 2 | 1 | + | 3 | 1 | + | 4 | 2 | + | 5 | 1 | + | 6 | 2 | + | 7 | 2 | + +----+-----+ + +**Output:** + + +-----------------+ + | ConsecutiveNums | + +-----------------+ + | 1 | + +-----------------+ + +**Explanation:** 1 is the only number that appears consecutively for at least three times. + +## Solution + +```sql +# Write your MySQL query statement below +select distinct num as ConsecutiveNums from +(select num, lag(num,1) over(order by id) as l1, lag(num,2) over(order by id) as l2 +from Logs) con_thr +where num = l1 and num = l2 +``` \ No newline at end of file diff --git a/src/main/kotlin/g0101_0200/s0181_employees_earning_more_than_their_managers/readme.md b/src/main/kotlin/g0101_0200/s0181_employees_earning_more_than_their_managers/readme.md new file mode 100644 index 00000000..8b3166c4 --- /dev/null +++ b/src/main/kotlin/g0101_0200/s0181_employees_earning_more_than_their_managers/readme.md @@ -0,0 +1,59 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 181\. Employees Earning More Than Their Managers + +Easy + +SQL Schema + +Table: `Employee` + + +-------------+---------+ + | Column Name | Type | + +-------------+---------+ + | id | int | + | name | varchar | + | salary | int | + | managerId | int | + +-------------+---------+ + id is the primary key column for this table. + Each row of this table indicates the ID of an employee, their name, salary, and the ID of their manager. + +Write an SQL query to find the employees who earn more than their managers. + +Return the result table in **any order**. + +The query result format is in the following example. + +**Example 1:** + +**Input:** + + Employee table: + +----+-------+--------+-----------+ + | id | name | salary | managerId | + +----+-------+--------+-----------+ + | 1 | Joe | 70000 | 3 | + | 2 | Henry | 80000 | 4 | + | 3 | Sam | 60000 | Null | + | 4 | Max | 90000 | Null | + +----+-------+--------+-----------+ + +**Output:** + + +----------+ + | Employee | + +----------+ + | Joe | + +----------+ + +**Explanation:** Joe is the only employee who earns more than his manager. + +## Solution + +```sql +# Write your MySQL query statement below +select a.Name as Employee from Employee a left join Employee b on a.ManagerId=b.Id +where a.Salary > b.Salary and a.ManagerId is not null +``` \ No newline at end of file diff --git a/src/main/kotlin/g0101_0200/s0182_duplicate_emails/readme.md b/src/main/kotlin/g0101_0200/s0182_duplicate_emails/readme.md new file mode 100644 index 00000000..f5e634e7 --- /dev/null +++ b/src/main/kotlin/g0101_0200/s0182_duplicate_emails/readme.md @@ -0,0 +1,55 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 182\. Duplicate Emails + +Easy + +SQL Schema + +Table: `Person` + + +-------------+---------+ + | Column Name | Type | + +-------------+---------+ + | id | int | + | email | varchar | + +-------------+---------+ + id is the primary key column for this table. + Each row of this table contains an email. The emails will not contain uppercase letters. + +Write an SQL query to report all the duplicate emails. + +Return the result table in **any order**. + +The query result format is in the following example. + +**Example 1:** + +**Input:** + + Person table: + +----+---------+ + | id | email | + +----+---------+ + | 1 | a@b.com | + | 2 | c@d.com | + | 3 | a@b.com | + +----+---------+ + +**Output:** + + +---------+ + | Email | + +---------+ + | a@b.com | + +---------+ + +**Explanation:** a@b.com is repeated two times. + +## Solution + +```sql +# Write your MySQL query statement below +SELECT Email FROM Person GROUP BY Email HAVING COUNT(Email) > 1; +``` \ No newline at end of file diff --git a/src/main/kotlin/g0101_0200/s0183_customers_who_never_order/readme.md b/src/main/kotlin/g0101_0200/s0183_customers_who_never_order/readme.md new file mode 100644 index 00000000..9b1a633c --- /dev/null +++ b/src/main/kotlin/g0101_0200/s0183_customers_who_never_order/readme.md @@ -0,0 +1,79 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 183\. Customers Who Never Order + +Easy + +SQL Schema + +Table: `Customers` + + +-------------+---------+ + | Column Name | Type | + +-------------+---------+ + | id | int | + | name | varchar | + +-------------+---------+ + id is the primary key column for this table. + Each row of this table indicates the ID and name of a customer. + +Table: `Orders` + + +-------------+------+ + | Column Name | Type | + +-------------+------+ + | id | int | + | customerId | int | + +-------------+------+ + id is the primary key column for this table. + customerId is a foreign key of the ID from the Customers table. + Each row of this table indicates the ID of an order and the ID of the customer who ordered it. + +Write an SQL query to report all customers who never order anything. + +Return the result table in **any order**. + +The query result format is in the following example. + +**Example 1:** + +**Input:** + + Customers table: + +----+-------+ + | id | name | + +----+-------+ + | 1 | Joe | + | 2 | Henry | + | 3 | Sam | + | 4 | Max | + +----+-------+ + + Orders table: + +----+------------+ + | id | customerId | + +----+------------+ + | 1 | 3 | + | 2 | 1 | + +----+------------+ + +**Output:** + + +-----------+ + | Customers | + +-----------+ + | Henry | + | Max | + +-----------+ + +## Solution + +```sql +# Write your MySQL query statement below +SELECT c.Name as Customers +FROM Customers as c +LEFT JOIN Orders as o +ON c.Id = o.CustomerId +WHERE o.CustomerId is null +``` \ No newline at end of file diff --git a/src/main/kotlin/g0101_0200/s0184_department_highest_salary/readme.md b/src/main/kotlin/g0101_0200/s0184_department_highest_salary/readme.md new file mode 100644 index 00000000..3819872d --- /dev/null +++ b/src/main/kotlin/g0101_0200/s0184_department_highest_salary/readme.md @@ -0,0 +1,95 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 184\. Department Highest Salary + +Medium + +SQL Schema + +Table: `Employee` + + +--------------+---------+ + | Column Name | Type | + +--------------+---------+ + | id | int | + | name | varchar | + | salary | int | + | departmentId | int | + +--------------+---------+ + id is the primary key column for this table. + departmentId is a foreign key of the ID from the `Department` table. + Each row of this table indicates the ID, name, and salary of an employee. It also contains the ID of their department. + +Table: `Department` + + +-------------+---------+ + | Column Name | Type | + +-------------+---------+ + | id | int | + | name | varchar | + +-------------+---------+ + id is the primary key column for this table. + Each row of this table indicates the ID of a department and its name. + +Write an SQL query to find employees who have the highest salary in each of the departments. + +Return the result table in **any order**. + +The query result format is in the following example. + +**Example 1:** + +**Input:** + + Employee table: + +----+-------+--------+--------------+ + | id | name | salary | departmentId | + +----+-------+--------+--------------+ + | 1 | Joe | 70000 | 1 | + | 2 | Jim | 90000 | 1 | + | 3 | Henry | 80000 | 2 | + | 4 | Sam | 60000 | 2 | + | 5 | Max | 90000 | 1 | + +----+-------+--------+--------------+ + + Department table: + +----+-------+ + | id | name | + +----+-------+ + | 1 | IT | + | 2 | Sales | + +----+-------+ + +**Output:** + + +------------+----------+--------+ + | Department | Employee | Salary | + +------------+----------+--------+ + | IT | Jim | 90000 | + | Sales | Henry | 80000 | + | IT | Max | 90000 | + +------------+----------+--------+ + +**Explanation:** Max and Jim both have the highest salary in the IT department and Henry has the highest salary in the Sales department. + +## Solution + +```sql +# Write your MySQL query statement below +SELECT + d.Name AS Department, + Sel.Name AS Employee, + Sel.Salary AS Salary +FROM +( + SELECT + Name, + Salary, + DepartmentId, + DENSE_RANK() OVER (PARTITION BY DepartmentId ORDER BY Salary DESC) AS dr + FROM Employee +) AS Sel +INNER JOIN Department d ON d.Id = Sel.DepartmentId +WHERE Sel.dr = 1 +``` \ No newline at end of file diff --git a/src/main/kotlin/g0101_0200/s0185_department_top_three_salaries/readme.md b/src/main/kotlin/g0101_0200/s0185_department_top_three_salaries/readme.md new file mode 100644 index 00000000..5c9d46db --- /dev/null +++ b/src/main/kotlin/g0101_0200/s0185_department_top_three_salaries/readme.md @@ -0,0 +1,108 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 185\. Department Top Three Salaries + +Hard + +SQL Schema + +Table: `Employee` + + +--------------+---------+ + | Column Name | Type | + +--------------+---------+ + | id | int | + | name | varchar | + | salary | int | + | departmentId | int | + +--------------+---------+ + id is the primary key column for this table. + departmentId is a foreign key of the ID from the `Department` table. Each row of this table indicates the ID, name, and salary of an employee. It also contains the ID of their department. + +Table: `Department` + + +-------------+---------+ + | Column Name | Type | + +-------------+---------+ + | id | int | + | name | varchar | + +-------------+---------+ + id is the primary key column for this table. + Each row of this table indicates the ID of a department and its name. + +A company's executives are interested in seeing who earns the most money in each of the company's departments. A **high earner** in a department is an employee who has a salary in the **top three unique** salaries for that department. + +Write an SQL query to find the employees who are **high earners** in each of the departments. + +Return the result table **in any order**. + +The query result format is in the following example. + +**Example 1:** + +**Input:** + +Employee table: + + +----+-------+--------+--------------+ + | id | name | salary | departmentId | + +----+-------+--------+--------------+ + | 1 | Joe | 85000 | 1 | + | 2 | Henry | 80000 | 2 | + | 3 | Sam | 60000 | 2 | + | 4 | Max | 90000 | 1 | + | 5 | Janet | 69000 | 1 | + | 6 | Randy | 85000 | 1 | + | 7 | Will | 70000 | 1 | + +----+-------+--------+--------------+ + + Department table: + +----+-------+ + | id | name | + +----+-------+ + | 1 | IT | + | 2 | Sales | + +----+-------+ + +**Output:** + + +------------+----------+--------+ + | Department | Employee | Salary | + +------------+----------+--------+ + | IT | Max | 90000 | + | IT | Joe | 85000 | + | IT | Randy | 85000 | + | IT | Will | 70000 | + | Sales | Henry | 80000 | + | Sales | Sam | 60000 | + +------------+----------+--------+ + +**Explanation:** + + In the IT department: + - Max earns the highest unique salary + - Both Randy and Joe earn the second-highest unique salary + - Will earns the third-highest unique salary + + In the Sales department: + - Henry earns the highest salary + - Sam earns the second-highest salary + - There is no third-highest salary as there are only two employees + +## Solution + +```sql +# Write your MySQL query statement below +SELECT D.name AS Department, + A.name AS Employee, + A.salary AS Salary +FROM + (SELECT *, + dense_rank() over(PARTITION BY departmentID + ORDER BY salary DESC) AS rn + FROM Employee) A + INNER JOIN Department D ON A.departmentid = D.id +WHERE A.rn < 4 +ORDER BY Department, Salary DESC +``` \ No newline at end of file diff --git a/src/main/kotlin/g0101_0200/s0187_repeated_dna_sequences/readme.md b/src/main/kotlin/g0101_0200/s0187_repeated_dna_sequences/readme.md new file mode 100644 index 00000000..b5df9e61 --- /dev/null +++ b/src/main/kotlin/g0101_0200/s0187_repeated_dna_sequences/readme.md @@ -0,0 +1,69 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 187\. Repeated DNA Sequences + +Medium + +The **DNA sequence** is composed of a series of nucleotides abbreviated as `'A'`, `'C'`, `'G'`, and `'T'`. + +* For example, `"ACGAATTCCG"` is a **DNA sequence**. + +When studying **DNA**, it is useful to identify repeated sequences within the DNA. + +Given a string `s` that represents a **DNA sequence**, return all the **`10`\-letter-long** sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in **any order**. + +**Example 1:** + +**Input:** s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT" + +**Output:** ["AAAAACCCCC","CCCCCAAAAA"] + +**Example 2:** + +**Input:** s = "AAAAAAAAAAAAA" + +**Output:** ["AAAAAAAAAA"] + +**Constraints:** + +* 1 <= s.length <= 105 +* `s[i]` is either `'A'`, `'C'`, `'G'`, or `'T'`. + +## Solution + +```kotlin +class Solution { + fun findRepeatedDnaSequences(s: String): List { + if (s.length < 10) { + return emptyList() + } + val seen = BooleanArray(1024 * 1024) + val added = BooleanArray(1024 * 1024) + val chars = s.toCharArray() + var buf = 0 + val map = IntArray(128) + map['A'.code] = 0 + map['C'.code] = 1 + map['G'.code] = 2 + map['T'.code] = 3 + val ans: MutableList = ArrayList(s.length / 2) + for (i in 0..9) { + buf = (buf shl 2) + map[chars[i].code] + } + seen[buf] = true + for (i in 10 until chars.size) { + buf = (buf shl 2 and 0xFFFFF) + map[chars[i].code] + if (seen[buf]) { + if (!added[buf]) { + ans.add(String(chars, i - 9, 10)) + added[buf] = true + } + } else { + seen[buf] = true + } + } + return ans + } +} +``` \ No newline at end of file From 8ae5f9559ed90d0910990a8734bc921a3ca82b1c Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 18 Oct 2022 05:16:18 +0300 Subject: [PATCH 2/2] Update readme.md --- src/main/kotlin/g0101_0200/s0177_nth_highest_salary/readme.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/kotlin/g0101_0200/s0177_nth_highest_salary/readme.md b/src/main/kotlin/g0101_0200/s0177_nth_highest_salary/readme.md index e163bb8b..ba7fc476 100644 --- a/src/main/kotlin/g0101_0200/s0177_nth_highest_salary/readme.md +++ b/src/main/kotlin/g0101_0200/s0177_nth_highest_salary/readme.md @@ -72,8 +72,7 @@ DECLARE M INT; SET M=N-1; RETURN ( # Write your MySQL query statement below. - # #Medium #Database #2022_10_18_Time_338_ms_(86.68%)_Space_0B_(100.00%) SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT M, 1 ); END -``` \ No newline at end of file +```