Skip to content

Commit f5929c6

Browse files
committed
feat: add solutions to lc problems: No.2706~2713
1 parent 012dc64 commit f5929c6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+3927
-12
lines changed

solution/1400-1499/1439.Find the Kth Smallest Sum of a Matrix With Sorted Rows/README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,8 @@ class Solution:
8181
def kthSmallest(self, mat: List[List[int]], k: int) -> int:
8282
pre = [0]
8383
for cur in mat:
84-
t = [a + b for a in pre for b in cur[:k]]
85-
t.sort()
86-
pre = t[:k]
87-
return pre[k - 1]
84+
pre = sorted(a + b for a in pre for b in cur[:k])[:k]
85+
return pre[-1]
8886
```
8987

9088
### **Java**

solution/1400-1499/1439.Find the Kth Smallest Sum of a Matrix With Sorted Rows/README_EN.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ class Solution:
5959
def kthSmallest(self, mat: List[List[int]], k: int) -> int:
6060
pre = [0]
6161
for cur in mat:
62-
t = [a + b for a in pre for b in cur[:k]]
63-
t.sort()
64-
pre = t[:k]
65-
return pre[k - 1]
62+
pre = sorted(a + b for a in pre for b in cur[:k])[:k]
63+
return pre[-1]
6664
```
6765

6866
### **Java**

solution/1400-1499/1439.Find the Kth Smallest Sum of a Matrix With Sorted Rows/Solution.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,5 @@ class Solution:
22
def kthSmallest(self, mat: List[List[int]], k: int) -> int:
33
pre = [0]
44
for cur in mat:
5-
t = [a + b for a in pre for b in cur[:k]]
6-
t.sort()
7-
pre = t[:k]
8-
return pre[k - 1]
5+
pre = sorted(a + b for a in pre for b in cur[:k])[:k]
6+
return pre[-1]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# [2706. 购买两块巧克力](https://leetcode.cn/problems/buy-two-chocolates)
2+
3+
[English Version](/solution/2700-2799/2706.Buy%20Two%20Chocolates/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个整数数组&nbsp;<code>prices</code>&nbsp;,它表示一个商店里若干巧克力的价格。同时给你一个整数&nbsp;<code>money</code>&nbsp;,表示你一开始拥有的钱数。</p>
10+
11+
<p>你必须购买 <strong>恰好&nbsp;</strong>两块巧克力,而且剩余的钱数必须是 <strong>非负数</strong>&nbsp;。同时你想最小化购买两块巧克力的总花费。</p>
12+
13+
<p>请你返回在购买两块巧克力后,最多能剩下多少钱。如果购买任意两块巧克力都超过了你拥有的钱,请你返回 <code>money</code>&nbsp;。注意剩余钱数必须是非负数。</p>
14+
15+
<p>&nbsp;</p>
16+
17+
<p><strong>示例 1:</strong></p>
18+
19+
<pre><b>输入:</b>prices = [1,2,2], money = 3
20+
<b>输出:</b>0
21+
<b>解释:</b>分别购买价格为 1 和 2 的巧克力。你剩下 3 - 3 = 0 块钱。所以我们返回 0 。
22+
</pre>
23+
24+
<p><strong>示例 2:</strong></p>
25+
26+
<pre><b>输入:</b>prices = [3,2,3], money = 3
27+
<b>输出:</b>3
28+
<b>解释:</b>购买任意 2 块巧克力都会超过你拥有的钱数,所以我们返回 3 。
29+
</pre>
30+
31+
<p>&nbsp;</p>
32+
33+
<p><strong>提示:</strong></p>
34+
35+
<ul>
36+
<li><code>2 &lt;= prices.length &lt;= 50</code></li>
37+
<li><code>1 &lt;= prices[i] &lt;= 100</code></li>
38+
<li><code>1 &lt;= money &lt;= 100</code></li>
39+
</ul>
40+
41+
## 解法
42+
43+
<!-- 这里可写通用的实现逻辑 -->
44+
45+
<!-- tabs:start -->
46+
47+
### **Python3**
48+
49+
<!-- 这里可写当前语言的特殊实现逻辑 -->
50+
51+
```python
52+
class Solution:
53+
def buyChoco(self, prices: List[int], money: int) -> int:
54+
prices.sort()
55+
cost = prices[0] + prices[1]
56+
return money if money < cost else money - cost
57+
```
58+
59+
### **Java**
60+
61+
<!-- 这里可写当前语言的特殊实现逻辑 -->
62+
63+
```java
64+
class Solution {
65+
public int buyChoco(int[] prices, int money) {
66+
Arrays.sort(prices);
67+
int cost = prices[0] + prices[1];
68+
return money < cost ? money : money - cost;
69+
}
70+
}
71+
```
72+
73+
### **C++**
74+
75+
```cpp
76+
class Solution {
77+
public:
78+
int buyChoco(vector<int>& prices, int money) {
79+
sort(prices.begin(), prices.end());
80+
int cost = prices[0] + prices[1];
81+
return money < cost ? money : money - cost;
82+
}
83+
};
84+
```
85+
86+
### **Go**
87+
88+
```go
89+
func buyChoco(prices []int, money int) int {
90+
sort.Ints(prices)
91+
cost := prices[0] + prices[1]
92+
if money < cost {
93+
return money
94+
}
95+
return money - cost
96+
}
97+
```
98+
99+
### **TypeScript**
100+
101+
```ts
102+
function buyChoco(prices: number[], money: number): number {
103+
prices.sort((a, b) => a - b);
104+
const cost = prices[0] + prices[1];
105+
return money < cost ? money : money - cost;
106+
}
107+
```
108+
109+
### **...**
110+
111+
```
112+
113+
```
114+
115+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# [2706. Buy Two Chocolates](https://leetcode.com/problems/buy-two-chocolates)
2+
3+
[中文文档](/solution/2700-2799/2706.Buy%20Two%20Chocolates/README.md)
4+
5+
## Description
6+
7+
<p>You are given an integer array <code>prices</code> representing the prices of various chocolates in a store. You are also given a single integer <code>money</code>, which represents your initial amount of money.</p>
8+
9+
<p>You must buy <strong>exactly</strong> two chocolates in such a way that you still have some <strong>non-negative</strong> leftover money. You would like to minimize the sum of the prices of the two chocolates you buy.</p>
10+
11+
<p>Return <em>the amount of money you will have leftover after buying the two chocolates</em>. If there is no way for you to buy two chocolates without ending up in debt, return <code>money</code>. Note that the leftover must be non-negative.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> prices = [1,2,2], money = 3
18+
<strong>Output:</strong> 0
19+
<strong>Explanation:</strong> Purchase the chocolates priced at 1 and 2 units respectively. You will have 3 - 3 = 0 units of money afterwards. Thus, we return 0.
20+
</pre>
21+
22+
<p><strong class="example">Example 2:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> prices = [3,2,3], money = 3
26+
<strong>Output:</strong> 3
27+
<strong>Explanation:</strong> You cannot buy 2 chocolates without going in debt, so we return 3.
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>2 &lt;= prices.length &lt;= 50</code></li>
35+
<li><code>1 &lt;= prices[i] &lt;= 100</code></li>
36+
<li><code>1 &lt;= money &lt;= 100</code></li>
37+
</ul>
38+
39+
## Solutions
40+
41+
<!-- tabs:start -->
42+
43+
### **Python3**
44+
45+
```python
46+
class Solution:
47+
def buyChoco(self, prices: List[int], money: int) -> int:
48+
prices.sort()
49+
cost = prices[0] + prices[1]
50+
return money if money < cost else money - cost
51+
```
52+
53+
### **Java**
54+
55+
```java
56+
class Solution {
57+
public int buyChoco(int[] prices, int money) {
58+
Arrays.sort(prices);
59+
int cost = prices[0] + prices[1];
60+
return money < cost ? money : money - cost;
61+
}
62+
}
63+
```
64+
65+
### **C++**
66+
67+
```cpp
68+
class Solution {
69+
public:
70+
int buyChoco(vector<int>& prices, int money) {
71+
sort(prices.begin(), prices.end());
72+
int cost = prices[0] + prices[1];
73+
return money < cost ? money : money - cost;
74+
}
75+
};
76+
```
77+
78+
### **Go**
79+
80+
```go
81+
func buyChoco(prices []int, money int) int {
82+
sort.Ints(prices)
83+
cost := prices[0] + prices[1]
84+
if money < cost {
85+
return money
86+
}
87+
return money - cost
88+
}
89+
```
90+
91+
### **TypeScript**
92+
93+
```ts
94+
function buyChoco(prices: number[], money: number): number {
95+
prices.sort((a, b) => a - b);
96+
const cost = prices[0] + prices[1];
97+
return money < cost ? money : money - cost;
98+
}
99+
```
100+
101+
### **...**
102+
103+
```
104+
105+
```
106+
107+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public:
3+
int buyChoco(vector<int>& prices, int money) {
4+
sort(prices.begin(), prices.end());
5+
int cost = prices[0] + prices[1];
6+
return money < cost ? money : money - cost;
7+
}
8+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func buyChoco(prices []int, money int) int {
2+
sort.Ints(prices)
3+
cost := prices[0] + prices[1]
4+
if money < cost {
5+
return money
6+
}
7+
return money - cost
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution {
2+
public int buyChoco(int[] prices, int money) {
3+
Arrays.sort(prices);
4+
int cost = prices[0] + prices[1];
5+
return money < cost ? money : money - cost;
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def buyChoco(self, prices: List[int], money: int) -> int:
3+
prices.sort()
4+
cost = prices[0] + prices[1]
5+
return money if money < cost else money - cost
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function buyChoco(prices: number[], money: number): number {
2+
prices.sort((a, b) => a - b);
3+
const cost = prices[0] + prices[1];
4+
return money < cost ? money : money - cost;
5+
}

0 commit comments

Comments
 (0)