Skip to content

Commit f9f70e5

Browse files
committed
feat: add solutions to lc problems
* No.1475.Final Prices With a Special Discount in a Shop * No.1476.Subrectangle Queries
1 parent 0e01030 commit f9f70e5

File tree

11 files changed

+459
-89
lines changed

11 files changed

+459
-89
lines changed

solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/README.md

+93-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54-
**方法一:单调栈**
54+
**方法一:暴力枚举**
55+
56+
按题意模拟,采用双重循环枚举 `i``j`
57+
58+
时间复杂度为 $O(n^2)$,忽略结果数组的空间消耗,空间复杂度 $O(1)$。
59+
60+
**方法二:单调栈**
5561

5662
单调栈常见模型:找出每个数左/右边**离它最近的****比它大/小的数**。模板:
5763

@@ -73,6 +79,19 @@ for i in range(n):
7379

7480
<!-- 这里可写当前语言的特殊实现逻辑 -->
7581

82+
```python
83+
class Solution:
84+
def finalPrices(self, prices: List[int]) -> List[int]:
85+
ans = []
86+
for i, v in enumerate(prices):
87+
ans.append(v)
88+
for j in range(i + 1, len(prices)):
89+
if prices[j] <= v:
90+
ans[-1] -= prices[j]
91+
break
92+
return ans
93+
```
94+
7695
```python
7796
class Solution:
7897
def finalPrices(self, prices: List[int]) -> List[int]:
@@ -103,6 +122,25 @@ class Solution:
103122

104123
<!-- 这里可写当前语言的特殊实现逻辑 -->
105124

125+
```java
126+
class Solution {
127+
public int[] finalPrices(int[] prices) {
128+
int n = prices.length;
129+
int[] ans = new int[n];
130+
for (int i = 0; i < n; ++i) {
131+
ans[i] = prices[i];
132+
for (int j = i + 1; j < n; ++j) {
133+
if (prices[j] <= prices[i]) {
134+
ans[i] -= prices[j];
135+
break;
136+
}
137+
}
138+
}
139+
return ans;
140+
}
141+
}
142+
```
143+
106144
```java
107145
class Solution {
108146
public int[] finalPrices(int[] prices) {
@@ -144,6 +182,26 @@ class Solution {
144182

145183
### **C++**
146184

185+
```cpp
186+
class Solution {
187+
public:
188+
vector<int> finalPrices(vector<int>& prices) {
189+
int n = prices.size();
190+
vector<int> ans(n);
191+
for (int i = 0; i < n; ++i) {
192+
ans[i] = prices[i];
193+
for (int j = i + 1; j < n; ++j) {
194+
if (prices[j] <= prices[i]) {
195+
ans[i] -= prices[j];
196+
break;
197+
}
198+
}
199+
}
200+
return ans;
201+
}
202+
};
203+
```
204+
147205
```cpp
148206
class Solution {
149207
public:
@@ -186,6 +244,23 @@ public:
186244
187245
### **Go**
188246
247+
```go
248+
func finalPrices(prices []int) []int {
249+
n := len(prices)
250+
ans := make([]int, n)
251+
for i, v := range prices {
252+
ans[i] = v
253+
for j := i + 1; j < n; j++ {
254+
if prices[j] <= v {
255+
ans[i] -= prices[j]
256+
break
257+
}
258+
}
259+
}
260+
return ans
261+
}
262+
```
263+
189264
```go
190265
func finalPrices(prices []int) []int {
191266
var stk []int
@@ -224,6 +299,23 @@ func finalPrices(prices []int) []int {
224299

225300
### **TypeScript**
226301

302+
```ts
303+
function finalPrices(prices: number[]): number[] {
304+
const n = prices.length;
305+
const ans = new Array(n);
306+
for (let i = 0; i < n; ++i) {
307+
ans[i] = prices[i];
308+
for (let j = i + 1; j < n; ++j) {
309+
if (prices[j] <= prices[i]) {
310+
ans[i] -= prices[j];
311+
break;
312+
}
313+
}
314+
}
315+
return ans;
316+
}
317+
```
318+
227319
```ts
228320
function finalPrices(prices: number[]): number[] {
229321
const n = prices.length;

solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/README_EN.md

+86
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ For items 3 and 4 you will not receive any discount at all.
5050

5151
### **Python3**
5252

53+
```python
54+
class Solution:
55+
def finalPrices(self, prices: List[int]) -> List[int]:
56+
ans = []
57+
for i, v in enumerate(prices):
58+
ans.append(v)
59+
for j in range(i + 1, len(prices)):
60+
if prices[j] <= v:
61+
ans[-1] -= prices[j]
62+
break
63+
return ans
64+
```
65+
5366
```python
5467
class Solution:
5568
def finalPrices(self, prices: List[int]) -> List[int]:
@@ -78,6 +91,25 @@ class Solution:
7891

7992
### **Java**
8093

94+
```java
95+
class Solution {
96+
public int[] finalPrices(int[] prices) {
97+
int n = prices.length;
98+
int[] ans = new int[n];
99+
for (int i = 0; i < n; ++i) {
100+
ans[i] = prices[i];
101+
for (int j = i + 1; j < n; ++j) {
102+
if (prices[j] <= prices[i]) {
103+
ans[i] -= prices[j];
104+
break;
105+
}
106+
}
107+
}
108+
return ans;
109+
}
110+
}
111+
```
112+
81113
```java
82114
class Solution {
83115
public int[] finalPrices(int[] prices) {
@@ -119,6 +151,26 @@ class Solution {
119151

120152
### **C++**
121153

154+
```cpp
155+
class Solution {
156+
public:
157+
vector<int> finalPrices(vector<int>& prices) {
158+
int n = prices.size();
159+
vector<int> ans(n);
160+
for (int i = 0; i < n; ++i) {
161+
ans[i] = prices[i];
162+
for (int j = i + 1; j < n; ++j) {
163+
if (prices[j] <= prices[i]) {
164+
ans[i] -= prices[j];
165+
break;
166+
}
167+
}
168+
}
169+
return ans;
170+
}
171+
};
172+
```
173+
122174
```cpp
123175
class Solution {
124176
public:
@@ -161,6 +213,23 @@ public:
161213
162214
### **Go**
163215
216+
```go
217+
func finalPrices(prices []int) []int {
218+
n := len(prices)
219+
ans := make([]int, n)
220+
for i, v := range prices {
221+
ans[i] = v
222+
for j := i + 1; j < n; j++ {
223+
if prices[j] <= v {
224+
ans[i] -= prices[j]
225+
break
226+
}
227+
}
228+
}
229+
return ans
230+
}
231+
```
232+
164233
```go
165234
func finalPrices(prices []int) []int {
166235
var stk []int
@@ -199,6 +268,23 @@ func finalPrices(prices []int) []int {
199268

200269
### **TypeScript**
201270

271+
```ts
272+
function finalPrices(prices: number[]): number[] {
273+
const n = prices.length;
274+
const ans = new Array(n);
275+
for (let i = 0; i < n; ++i) {
276+
ans[i] = prices[i];
277+
for (let j = i + 1; j < n; ++j) {
278+
if (prices[j] <= prices[i]) {
279+
ans[i] -= prices[j];
280+
break;
281+
}
282+
}
283+
}
284+
return ans;
285+
}
286+
```
287+
202288
```ts
203289
function finalPrices(prices: number[]): number[] {
204290
const n = prices.length;

0 commit comments

Comments
 (0)