From 638d7eb1e7c4d24bfec785ec030e344b082eb424 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Sun, 6 Apr 2025 14:44:21 +0530 Subject: [PATCH 1/2] feat: add pandas solution to lc problem: No.0184 (#4331) --- .../0184.Department Highest Salary/README.md | 25 +++++++++++++++++++ .../README_EN.md | 25 +++++++++++++++++++ .../Solution.py | 20 +++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 solution/0100-0199/0184.Department Highest Salary/Solution.py diff --git a/solution/0100-0199/0184.Department Highest Salary/README.md b/solution/0100-0199/0184.Department Highest Salary/README.md index f22679744afbb..fe36aca302cbb 100644 --- a/solution/0100-0199/0184.Department Highest Salary/README.md +++ b/solution/0100-0199/0184.Department Highest Salary/README.md @@ -114,6 +114,31 @@ WHERE ); ``` +### Pandas + +```python +import pandas as pd + + +def department_highest_salary( + employee: pd.DataFrame, department: pd.DataFrame +) -> pd.DataFrame: + # Merge the two tables on departmentId and department id + merged = employee.merge(department, left_on='departmentId', right_on='id') + + # Find the maximum salary for each department + max_salaries = merged.groupby('departmentId')['salary'].transform('max') + + # Filter employees who have the highest salary in their department + top_earners = merged[merged['salary'] == max_salaries] + + # Select required columns and rename them + result = top_earners[['name_y', 'name_x', 'salary']].copy() + result.columns = ['Department', 'Employee', 'Salary'] + + return result +``` + diff --git a/solution/0100-0199/0184.Department Highest Salary/README_EN.md b/solution/0100-0199/0184.Department Highest Salary/README_EN.md index 68aad979846ff..48725a31b1208 100644 --- a/solution/0100-0199/0184.Department Highest Salary/README_EN.md +++ b/solution/0100-0199/0184.Department Highest Salary/README_EN.md @@ -116,6 +116,31 @@ WHERE ); ``` +### Pandas + +```python +import pandas as pd + + +def department_highest_salary( + employee: pd.DataFrame, department: pd.DataFrame +) -> pd.DataFrame: + # Merge the two tables on departmentId and department id + merged = employee.merge(department, left_on='departmentId', right_on='id') + + # Find the maximum salary for each department + max_salaries = merged.groupby('departmentId')['salary'].transform('max') + + # Filter employees who have the highest salary in their department + top_earners = merged[merged['salary'] == max_salaries] + + # Select required columns and rename them + result = top_earners[['name_y', 'name_x', 'salary']].copy() + result.columns = ['Department', 'Employee', 'Salary'] + + return result +``` + diff --git a/solution/0100-0199/0184.Department Highest Salary/Solution.py b/solution/0100-0199/0184.Department Highest Salary/Solution.py new file mode 100644 index 0000000000000..278093fdb7117 --- /dev/null +++ b/solution/0100-0199/0184.Department Highest Salary/Solution.py @@ -0,0 +1,20 @@ +import pandas as pd + + +def department_highest_salary( + employee: pd.DataFrame, department: pd.DataFrame +) -> pd.DataFrame: + # Merge the two tables on departmentId and department id + merged = employee.merge(department, left_on='departmentId', right_on='id') + + # Find the maximum salary for each department + max_salaries = merged.groupby('departmentId')['salary'].transform('max') + + # Filter employees who have the highest salary in their department + top_earners = merged[merged['salary'] == max_salaries] + + # Select required columns and rename them + result = top_earners[['name_y', 'name_x', 'salary']].copy() + result.columns = ['Department', 'Employee', 'Salary'] + + return result \ No newline at end of file From c5bfe3fec0257fc4b194fc1824f1005e8c1a4487 Mon Sep 17 00:00:00 2001 From: "Mr. Khatri" <117172329+361930@users.noreply.github.com> Date: Sun, 6 Apr 2025 17:04:29 +0530 Subject: [PATCH 2/2] fix: update solution to lc problem: No.0857 (#4332) --- .../README.md | 28 ++++++------------- .../README_EN.md | 28 ++++++------------- .../Solution.cpp | 2 +- .../Solution.go | 2 +- .../Solution.java | 24 +++++----------- 5 files changed, 27 insertions(+), 57 deletions(-) diff --git a/solution/0800-0899/0857.Minimum Cost to Hire K Workers/README.md b/solution/0800-0899/0857.Minimum Cost to Hire K Workers/README.md index 26653f615fa58..aaa16bb1bda2f 100644 --- a/solution/0800-0899/0857.Minimum Cost to Hire K Workers/README.md +++ b/solution/0800-0899/0857.Minimum Cost to Hire K Workers/README.md @@ -106,35 +106,25 @@ class Solution: class Solution { public double mincostToHireWorkers(int[] quality, int[] wage, int k) { int n = quality.length; - Pair[] t = new Pair[n]; + Pair[] t = new Pair[n]; for (int i = 0; i < n; ++i) { - t[i] = new Pair(quality[i], wage[i]); + t[i] = new Pair<>((double) wage[i] / quality[i], quality[i]); } - Arrays.sort(t, (a, b) -> Double.compare(a.x, b.x)); + Arrays.sort(t, (a, b) -> Double.compare(a.getKey(), b.getKey())); PriorityQueue pq = new PriorityQueue<>((a, b) -> b - a); - double ans = 1e9; + double ans = 1e18; int tot = 0; for (var e : t) { - tot += e.q; - pq.offer(e.q); + tot += e.getValue(); + pq.offer(e.getValue()); if (pq.size() == k) { - ans = Math.min(ans, tot * e.x); + ans = Math.min(ans, tot * e.getKey()); tot -= pq.poll(); } } return ans; } } - -class Pair { - double x; - int q; - - Pair(int q, int w) { - this.q = q; - this.x = (double) w / q; - } -} ``` #### C++ @@ -150,7 +140,7 @@ public: } sort(t.begin(), t.end()); priority_queue pq; - double ans = 1e9; + double ans = 1e18; int tot = 0; for (auto& [x, q] : t) { tot += q; @@ -176,7 +166,7 @@ func mincostToHireWorkers(quality []int, wage []int, k int) float64 { } sort.Slice(t, func(i, j int) bool { return t[i].x < t[j].x }) tot := 0 - var ans float64 = 1e9 + var ans float64 = 1e18 pq := hp{} for _, e := range t { tot += e.q diff --git a/solution/0800-0899/0857.Minimum Cost to Hire K Workers/README_EN.md b/solution/0800-0899/0857.Minimum Cost to Hire K Workers/README_EN.md index a1218f717f1e0..306df16aee8cc 100644 --- a/solution/0800-0899/0857.Minimum Cost to Hire K Workers/README_EN.md +++ b/solution/0800-0899/0857.Minimum Cost to Hire K Workers/README_EN.md @@ -91,35 +91,25 @@ class Solution: class Solution { public double mincostToHireWorkers(int[] quality, int[] wage, int k) { int n = quality.length; - Pair[] t = new Pair[n]; + Pair[] t = new Pair[n]; for (int i = 0; i < n; ++i) { - t[i] = new Pair(quality[i], wage[i]); + t[i] = new Pair<>((double) wage[i] / quality[i], quality[i]); } - Arrays.sort(t, (a, b) -> Double.compare(a.x, b.x)); + Arrays.sort(t, (a, b) -> Double.compare(a.getKey(), b.getKey())); PriorityQueue pq = new PriorityQueue<>((a, b) -> b - a); - double ans = 1e9; + double ans = 1e18; int tot = 0; for (var e : t) { - tot += e.q; - pq.offer(e.q); + tot += e.getValue(); + pq.offer(e.getValue()); if (pq.size() == k) { - ans = Math.min(ans, tot * e.x); + ans = Math.min(ans, tot * e.getKey()); tot -= pq.poll(); } } return ans; } } - -class Pair { - double x; - int q; - - Pair(int q, int w) { - this.q = q; - this.x = (double) w / q; - } -} ``` #### C++ @@ -135,7 +125,7 @@ public: } sort(t.begin(), t.end()); priority_queue pq; - double ans = 1e9; + double ans = 1e18; int tot = 0; for (auto& [x, q] : t) { tot += q; @@ -161,7 +151,7 @@ func mincostToHireWorkers(quality []int, wage []int, k int) float64 { } sort.Slice(t, func(i, j int) bool { return t[i].x < t[j].x }) tot := 0 - var ans float64 = 1e9 + var ans float64 = 1e18 pq := hp{} for _, e := range t { tot += e.q diff --git a/solution/0800-0899/0857.Minimum Cost to Hire K Workers/Solution.cpp b/solution/0800-0899/0857.Minimum Cost to Hire K Workers/Solution.cpp index e0cd02c4e27b5..940263977b9be 100644 --- a/solution/0800-0899/0857.Minimum Cost to Hire K Workers/Solution.cpp +++ b/solution/0800-0899/0857.Minimum Cost to Hire K Workers/Solution.cpp @@ -8,7 +8,7 @@ class Solution { } sort(t.begin(), t.end()); priority_queue pq; - double ans = 1e9; + double ans = 1e18; int tot = 0; for (auto& [x, q] : t) { tot += q; diff --git a/solution/0800-0899/0857.Minimum Cost to Hire K Workers/Solution.go b/solution/0800-0899/0857.Minimum Cost to Hire K Workers/Solution.go index cd286f89e00ca..912341e4e9f7c 100644 --- a/solution/0800-0899/0857.Minimum Cost to Hire K Workers/Solution.go +++ b/solution/0800-0899/0857.Minimum Cost to Hire K Workers/Solution.go @@ -5,7 +5,7 @@ func mincostToHireWorkers(quality []int, wage []int, k int) float64 { } sort.Slice(t, func(i, j int) bool { return t[i].x < t[j].x }) tot := 0 - var ans float64 = 1e9 + var ans float64 = 1e18 pq := hp{} for _, e := range t { tot += e.q diff --git a/solution/0800-0899/0857.Minimum Cost to Hire K Workers/Solution.java b/solution/0800-0899/0857.Minimum Cost to Hire K Workers/Solution.java index 2090d170f8829..84f779425be1e 100644 --- a/solution/0800-0899/0857.Minimum Cost to Hire K Workers/Solution.java +++ b/solution/0800-0899/0857.Minimum Cost to Hire K Workers/Solution.java @@ -1,32 +1,22 @@ class Solution { public double mincostToHireWorkers(int[] quality, int[] wage, int k) { int n = quality.length; - Pair[] t = new Pair[n]; + Pair[] t = new Pair[n]; for (int i = 0; i < n; ++i) { - t[i] = new Pair(quality[i], wage[i]); + t[i] = new Pair<>((double) wage[i] / quality[i], quality[i]); } - Arrays.sort(t, (a, b) -> Double.compare(a.x, b.x)); + Arrays.sort(t, (a, b) -> Double.compare(a.getKey(), b.getKey())); PriorityQueue pq = new PriorityQueue<>((a, b) -> b - a); - double ans = 1e9; + double ans = 1e18; int tot = 0; for (var e : t) { - tot += e.q; - pq.offer(e.q); + tot += e.getValue(); + pq.offer(e.getValue()); if (pq.size() == k) { - ans = Math.min(ans, tot * e.x); + ans = Math.min(ans, tot * e.getKey()); tot -= pq.poll(); } } return ans; } } - -class Pair { - double x; - int q; - - Pair(int q, int w) { - this.q = q; - this.x = (double) w / q; - } -} \ No newline at end of file