diff --git a/solution/2200-2299/2239.Find Closest Number to Zero/README.md b/solution/2200-2299/2239.Find Closest Number to Zero/README.md index 894ede1bc3567..517dab57a73da 100644 --- a/solution/2200-2299/2239.Find Closest Number to Zero/README.md +++ b/solution/2200-2299/2239.Find Closest Number to Zero/README.md @@ -50,7 +50,13 @@ ```python - +class Solution: + def findClosestNumber(self, nums: List[int]) -> int: + ans, d = 0, 1000000 + for v in nums: + if (t := abs(v)) < d or (t == d and v > ans): + ans, d = v, t + return ans ``` ### **Java** @@ -58,7 +64,62 @@ ```java +class Solution { + public int findClosestNumber(int[] nums) { + int ans = 0, d = 1000000; + for (int v : nums) { + int t = Math.abs(v); + if (t < d || (t == d && v > ans)) { + ans = v; + d = t; + } + } + return ans; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + int findClosestNumber(vector& nums) { + int ans = 0, d = 1e6; + for (int& v : nums) + { + int t = abs(v); + if (t < d || (t == d && v > ans)) + { + ans = v; + d = t; + } + } + return ans; + } +}; +``` +### **Go** + +```go +func findClosestNumber(nums []int) int { + ans, d := 0, 1000000 + for _, v := range nums { + t := abs(v) + if t < d || (t == d && v > ans) { + ans, d = v, t + } + } + return ans +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} ``` ### **TypeScript** diff --git a/solution/2200-2299/2239.Find Closest Number to Zero/README_EN.md b/solution/2200-2299/2239.Find Closest Number to Zero/README_EN.md index 80815bb1e1ad0..90fca41ccb8cc 100644 --- a/solution/2200-2299/2239.Find Closest Number to Zero/README_EN.md +++ b/solution/2200-2299/2239.Find Closest Number to Zero/README_EN.md @@ -43,13 +43,74 @@ Thus, the closest number to 0 in the array is 1. ### **Python3** ```python - +class Solution: + def findClosestNumber(self, nums: List[int]) -> int: + ans, d = 0, 1000000 + for v in nums: + if (t := abs(v)) < d or (t == d and v > ans): + ans, d = v, t + return ans ``` ### **Java** ```java +class Solution { + public int findClosestNumber(int[] nums) { + int ans = 0, d = 1000000; + for (int v : nums) { + int t = Math.abs(v); + if (t < d || (t == d && v > ans)) { + ans = v; + d = t; + } + } + return ans; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + int findClosestNumber(vector& nums) { + int ans = 0, d = 1e6; + for (int& v : nums) + { + int t = abs(v); + if (t < d || (t == d && v > ans)) + { + ans = v; + d = t; + } + } + return ans; + } +}; +``` +### **Go** + +```go +func findClosestNumber(nums []int) int { + ans, d := 0, 1000000 + for _, v := range nums { + t := abs(v) + if t < d || (t == d && v > ans) { + ans, d = v, t + } + } + return ans +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} ``` ### **TypeScript** diff --git a/solution/2200-2299/2239.Find Closest Number to Zero/Solution.cpp b/solution/2200-2299/2239.Find Closest Number to Zero/Solution.cpp new file mode 100644 index 0000000000000..6fc2597045a09 --- /dev/null +++ b/solution/2200-2299/2239.Find Closest Number to Zero/Solution.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int findClosestNumber(vector& nums) { + int ans = 0, d = 1e6; + for (int& v : nums) + { + int t = abs(v); + if (t < d || (t == d && v > ans)) + { + ans = v; + d = t; + } + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/2200-2299/2239.Find Closest Number to Zero/Solution.go b/solution/2200-2299/2239.Find Closest Number to Zero/Solution.go new file mode 100644 index 0000000000000..b93f51d8c032b --- /dev/null +++ b/solution/2200-2299/2239.Find Closest Number to Zero/Solution.go @@ -0,0 +1,17 @@ +func findClosestNumber(nums []int) int { + ans, d := 0, 1000000 + for _, v := range nums { + t := abs(v) + if t < d || (t == d && v > ans) { + ans, d = v, t + } + } + return ans +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} \ No newline at end of file diff --git a/solution/2200-2299/2239.Find Closest Number to Zero/Solution.java b/solution/2200-2299/2239.Find Closest Number to Zero/Solution.java new file mode 100644 index 0000000000000..4f71cec42ae6a --- /dev/null +++ b/solution/2200-2299/2239.Find Closest Number to Zero/Solution.java @@ -0,0 +1,13 @@ +class Solution { + public int findClosestNumber(int[] nums) { + int ans = 0, d = 1000000; + for (int v : nums) { + int t = Math.abs(v); + if (t < d || (t == d && v > ans)) { + ans = v; + d = t; + } + } + return ans; + } +} \ No newline at end of file diff --git a/solution/2200-2299/2239.Find Closest Number to Zero/Solution.py b/solution/2200-2299/2239.Find Closest Number to Zero/Solution.py new file mode 100644 index 0000000000000..3e9bff2fe985e --- /dev/null +++ b/solution/2200-2299/2239.Find Closest Number to Zero/Solution.py @@ -0,0 +1,7 @@ +class Solution: + def findClosestNumber(self, nums: List[int]) -> int: + ans, d = 0, 1000000 + for v in nums: + if (t := abs(v)) < d or (t == d and v > ans): + ans, d = v, t + return ans diff --git a/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README.md b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README.md index 7c18d7eb0ed01..961a2794479c3 100644 --- a/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README.md +++ b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README.md @@ -49,7 +49,13 @@ ```python - +class Solution: + def waysToBuyPensPencils(self, total: int, cost1: int, cost2: int) -> int: + ans = 0 + for x in range(total // cost1 + 1): + v = total - x * cost1 + ans += v // cost2 + 1 + return ans ``` ### **Java** @@ -57,7 +63,46 @@ ```java +class Solution { + public long waysToBuyPensPencils(int total, int cost1, int cost2) { + long ans = 0; + for (int x = 0; x <= total / cost1; ++x) { + int v = total - x * cost1; + ans += v / cost2 + 1; + } + return ans; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + long long waysToBuyPensPencils(int total, int cost1, int cost2) { + long long ans = 0; + for (int x = 0; x <= total / cost1; ++x) + { + int v = total - x * cost1; + ans += v / cost2 + 1; + } + return ans; + } +}; +``` +### **Go** + +```go +func waysToBuyPensPencils(total int, cost1 int, cost2 int) int64 { + var ans int64 + for x := 0; x <= total/cost1; x++ { + v := total - x*cost1 + ans += int64(v/cost2 + 1) + } + return ans +} ``` ### **TypeScript** diff --git a/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README_EN.md b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README_EN.md index 3c325be800128..ef4d430b39308 100644 --- a/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README_EN.md +++ b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README_EN.md @@ -43,13 +43,58 @@ The total number of ways to buy pens and pencils is 5 + 3 + 1 = 9. ### **Python3** ```python - +class Solution: + def waysToBuyPensPencils(self, total: int, cost1: int, cost2: int) -> int: + ans = 0 + for x in range(total // cost1 + 1): + v = total - x * cost1 + ans += v // cost2 + 1 + return ans ``` ### **Java** ```java +class Solution { + public long waysToBuyPensPencils(int total, int cost1, int cost2) { + long ans = 0; + for (int x = 0; x <= total / cost1; ++x) { + int v = total - x * cost1; + ans += v / cost2 + 1; + } + return ans; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + long long waysToBuyPensPencils(int total, int cost1, int cost2) { + long long ans = 0; + for (int x = 0; x <= total / cost1; ++x) + { + int v = total - x * cost1; + ans += v / cost2 + 1; + } + return ans; + } +}; +``` +### **Go** + +```go +func waysToBuyPensPencils(total int, cost1 int, cost2 int) int64 { + var ans int64 + for x := 0; x <= total/cost1; x++ { + v := total - x*cost1 + ans += int64(v/cost2 + 1) + } + return ans +} ``` ### **TypeScript** diff --git a/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.cpp b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.cpp new file mode 100644 index 0000000000000..91609af3996eb --- /dev/null +++ b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + long long waysToBuyPensPencils(int total, int cost1, int cost2) { + long long ans = 0; + for (int x = 0; x <= total / cost1; ++x) + { + int v = total - x * cost1; + ans += v / cost2 + 1; + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.go b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.go new file mode 100644 index 0000000000000..a1c8892ec3e4f --- /dev/null +++ b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.go @@ -0,0 +1,8 @@ +func waysToBuyPensPencils(total int, cost1 int, cost2 int) int64 { + var ans int64 + for x := 0; x <= total/cost1; x++ { + v := total - x*cost1 + ans += int64(v/cost2 + 1) + } + return ans +} \ No newline at end of file diff --git a/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.java b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.java new file mode 100644 index 0000000000000..e0cab29917ffe --- /dev/null +++ b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.java @@ -0,0 +1,10 @@ +class Solution { + public long waysToBuyPensPencils(int total, int cost1, int cost2) { + long ans = 0; + for (int x = 0; x <= total / cost1; ++x) { + int v = total - x * cost1; + ans += v / cost2 + 1; + } + return ans; + } +} \ No newline at end of file diff --git a/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.py b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.py new file mode 100644 index 0000000000000..321469db251a8 --- /dev/null +++ b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.py @@ -0,0 +1,7 @@ +class Solution: + def waysToBuyPensPencils(self, total: int, cost1: int, cost2: int) -> int: + ans = 0 + for x in range(total // cost1 + 1): + v = total - x * cost1 + ans += v // cost2 + 1 + return ans diff --git a/solution/2200-2299/2241.Design an ATM Machine/README.md b/solution/2200-2299/2241.Design an ATM Machine/README.md index b992264963abc..cdb34911b3f80 100644 --- a/solution/2200-2299/2241.Design an ATM Machine/README.md +++ b/solution/2200-2299/2241.Design an ATM Machine/README.md @@ -67,7 +67,32 @@ atm.withdraw(550); // 返回 [0,1,0,0,1] ,机器会返回 1 张 $50 的 ```python - +class ATM: + + def __init__(self): + self.cnt = [0] * 5 + self.m = [500, 200, 100, 50, 20] + + def deposit(self, banknotesCount: List[int]) -> None: + for i, v in enumerate(banknotesCount[::-1]): + self.cnt[i] += v + + def withdraw(self, amount: int) -> List[int]: + ans = [0] * 5 + for i, n in enumerate(self.cnt): + ans[i] = min(amount // self.m[i], n) + amount -= self.m[i] * ans[i] + if amount > 0: + return [-1] + for i, v in enumerate(ans): + self.cnt[i] -= v + return ans[::-1] + + +# Your ATM object will be instantiated and called as such: +# obj = ATM() +# obj.deposit(banknotesCount) +# param_2 = obj.withdraw(amount) ``` ### **Java** diff --git a/solution/2200-2299/2241.Design an ATM Machine/README_EN.md b/solution/2200-2299/2241.Design an ATM Machine/README_EN.md index 25e29e3492b3f..73169f6e635c0 100644 --- a/solution/2200-2299/2241.Design an ATM Machine/README_EN.md +++ b/solution/2200-2299/2241.Design an ATM Machine/README_EN.md @@ -66,7 +66,32 @@ atm.withdraw(550); // Returns [0,1,0,0,1]. The machine uses 1 $50 banknot ### **Python3** ```python - +class ATM: + + def __init__(self): + self.cnt = [0] * 5 + self.m = [500, 200, 100, 50, 20] + + def deposit(self, banknotesCount: List[int]) -> None: + for i, v in enumerate(banknotesCount[::-1]): + self.cnt[i] += v + + def withdraw(self, amount: int) -> List[int]: + ans = [0] * 5 + for i, n in enumerate(self.cnt): + ans[i] = min(amount // self.m[i], n) + amount -= self.m[i] * ans[i] + if amount > 0: + return [-1] + for i, v in enumerate(ans): + self.cnt[i] -= v + return ans[::-1] + + +# Your ATM object will be instantiated and called as such: +# obj = ATM() +# obj.deposit(banknotesCount) +# param_2 = obj.withdraw(amount) ``` ### **Java** diff --git a/solution/2200-2299/2241.Design an ATM Machine/Solution.py b/solution/2200-2299/2241.Design an ATM Machine/Solution.py new file mode 100644 index 0000000000000..ae6560855d6ea --- /dev/null +++ b/solution/2200-2299/2241.Design an ATM Machine/Solution.py @@ -0,0 +1,26 @@ +class ATM: + + def __init__(self): + self.cnt = [0] * 5 + self.m = [500, 200, 100, 50, 20] + + def deposit(self, banknotesCount: List[int]) -> None: + for i, v in enumerate(banknotesCount[::-1]): + self.cnt[i] += v + + def withdraw(self, amount: int) -> List[int]: + ans = [0] * 5 + for i, n in enumerate(self.cnt): + ans[i] = min(amount // self.m[i], n) + amount -= self.m[i] * ans[i] + if amount > 0: + return [-1] + for i, v in enumerate(ans): + self.cnt[i] -= v + return ans[::-1] + + +# Your ATM object will be instantiated and called as such: +# obj = ATM() +# obj.deposit(banknotesCount) +# param_2 = obj.withdraw(amount) diff --git a/solution/2200-2299/2242.Maximum Score of a Node Sequence/README.md b/solution/2200-2299/2242.Maximum Score of a Node Sequence/README.md index 0f693bf84a21b..c4099b929d966 100644 --- a/solution/2200-2299/2242.Maximum Score of a Node Sequence/README.md +++ b/solution/2200-2299/2242.Maximum Score of a Node Sequence/README.md @@ -67,6 +67,10 @@ +**方法一:枚举中间边** + +枚举中间边 $(a, b)$,假设与 $a$ 相邻的点为 $c$,与 $b$ 相邻的点为 $d$。对于相邻点,取分数最大的三个点进行枚举。 + ### **Python3** @@ -74,7 +78,22 @@ ```python - +class Solution: + def maximumScore(self, scores: List[int], edges: List[List[int]]) -> int: + g = defaultdict(list) + for a, b in edges: + g[a].append(b) + g[b].append(a) + for k in g.keys(): + g[k] = nlargest(3, g[k], key=lambda x: scores[x]) + ans = -1 + for a, b in edges: + for c in g[a]: + for d in g[b]: + if b != c != d != a: + t = scores[a] + scores[b] + scores[c] + scores[d] + ans = max(ans, t) + return ans ``` ### **Java** @@ -82,7 +101,37 @@ ```java - +class Solution { + public int maximumScore(int[] scores, int[][] edges) { + int n = scores.length; + List[] g = new List[n]; + for (int i = 0; i < n; ++i) { + g[i] = new ArrayList<>(); + } + for (int[] e : edges) { + int a = e[0], b = e[1]; + g[a].add(b); + g[b].add(a); + } + for (int i = 0; i < n; ++i) { + g[i].sort((a, b) -> scores[b] - scores[a]); + g[i] = g[i].subList(0, Math.min(3, g[i].size())); + } + int ans = -1; + for (int[] e : edges) { + int a = e[0], b = e[1]; + for (int c : g[a]) { + for (int d : g[b]) { + if (c != b && c != d && a != d) { + int t = scores[a] + scores[b] + scores[c] + scores[d]; + ans = Math.max(ans, t); + } + } + } + } + return ans; + } +} ``` ### **TypeScript** diff --git a/solution/2200-2299/2242.Maximum Score of a Node Sequence/README_EN.md b/solution/2200-2299/2242.Maximum Score of a Node Sequence/README_EN.md index 32a3ad3af8431..8f24141223c9a 100644 --- a/solution/2200-2299/2242.Maximum Score of a Node Sequence/README_EN.md +++ b/solution/2200-2299/2242.Maximum Score of a Node Sequence/README_EN.md @@ -62,13 +62,58 @@ There are no valid node sequences of length 4, so we return -1. ### **Python3** ```python - +class Solution: + def maximumScore(self, scores: List[int], edges: List[List[int]]) -> int: + g = defaultdict(list) + for a, b in edges: + g[a].append(b) + g[b].append(a) + for k in g.keys(): + g[k] = nlargest(3, g[k], key=lambda x: scores[x]) + ans = -1 + for a, b in edges: + for c in g[a]: + for d in g[b]: + if b != c != d != a: + t = scores[a] + scores[b] + scores[c] + scores[d] + ans = max(ans, t) + return ans ``` ### **Java** ```java - +class Solution { + public int maximumScore(int[] scores, int[][] edges) { + int n = scores.length; + List[] g = new List[n]; + for (int i = 0; i < n; ++i) { + g[i] = new ArrayList<>(); + } + for (int[] e : edges) { + int a = e[0], b = e[1]; + g[a].add(b); + g[b].add(a); + } + for (int i = 0; i < n; ++i) { + g[i].sort((a, b) -> scores[b] - scores[a]); + g[i] = g[i].subList(0, Math.min(3, g[i].size())); + } + int ans = -1; + for (int[] e : edges) { + int a = e[0], b = e[1]; + for (int c : g[a]) { + for (int d : g[b]) { + if (c != b && c != d && a != d) { + int t = scores[a] + scores[b] + scores[c] + scores[d]; + ans = Math.max(ans, t); + } + } + } + } + return ans; + } +} ``` ### **TypeScript** diff --git a/solution/2200-2299/2242.Maximum Score of a Node Sequence/Solution.java b/solution/2200-2299/2242.Maximum Score of a Node Sequence/Solution.java new file mode 100644 index 0000000000000..77c4d3fd5fd3b --- /dev/null +++ b/solution/2200-2299/2242.Maximum Score of a Node Sequence/Solution.java @@ -0,0 +1,31 @@ +class Solution { + public int maximumScore(int[] scores, int[][] edges) { + int n = scores.length; + List[] g = new List[n]; + for (int i = 0; i < n; ++i) { + g[i] = new ArrayList<>(); + } + for (int[] e : edges) { + int a = e[0], b = e[1]; + g[a].add(b); + g[b].add(a); + } + for (int i = 0; i < n; ++i) { + g[i].sort((a, b) -> scores[b] - scores[a]); + g[i] = g[i].subList(0, Math.min(3, g[i].size())); + } + int ans = -1; + for (int[] e : edges) { + int a = e[0], b = e[1]; + for (int c : g[a]) { + for (int d : g[b]) { + if (c != b && c != d && a != d) { + int t = scores[a] + scores[b] + scores[c] + scores[d]; + ans = Math.max(ans, t); + } + } + } + } + return ans; + } +} \ No newline at end of file diff --git a/solution/2200-2299/2242.Maximum Score of a Node Sequence/Solution.py b/solution/2200-2299/2242.Maximum Score of a Node Sequence/Solution.py new file mode 100644 index 0000000000000..384d746493f44 --- /dev/null +++ b/solution/2200-2299/2242.Maximum Score of a Node Sequence/Solution.py @@ -0,0 +1,16 @@ +class Solution: + def maximumScore(self, scores: List[int], edges: List[List[int]]) -> int: + g = defaultdict(list) + for a, b in edges: + g[a].append(b) + g[b].append(a) + for k in g.keys(): + g[k] = nlargest(3, g[k], key=lambda x: scores[x]) + ans = -1 + for a, b in edges: + for c in g[a]: + for d in g[b]: + if b != c != d != a: + t = scores[a] + scores[b] + scores[c] + scores[d] + ans = max(ans, t) + return ans