Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problems: No.1389,1402 #1748

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ nums index target

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

**方法一:模拟**

我们创建一个列表 $target$,用于存储目标数组。由于题目保证数字插入位置总是存在,因此我们可以直接按照给定的顺序插入到对应的位置。

时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。

<!-- tabs:start -->

### **Python3**
Expand All @@ -77,8 +83,8 @@ nums index target
class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
target = []
for i in range(len(nums)):
target.insert(index[i], nums[i])
for x, i in zip(nums, index):
target.insert(i, x)
return target
```

Expand All @@ -94,27 +100,16 @@ class Solution {
for (int i = 0; i < n; ++i) {
target.add(index[i], nums[i]);
}
int[] res = new int[n];
// return target.stream().mapToInt(i -> i).toArray();
int[] ans = new int[n];
for (int i = 0; i < n; ++i) {
res[i] = target.get(i);
ans[i] = target.get(i);
}
return res;
return ans;
}
}
```

### **TypeScript**

```ts
function createTargetArray(nums: number[], index: number[]): number[] {
const ans: number[] = [];
for (let i = 0; i < nums.length; i++) {
ans.splice(index[i], 0, nums[i]);
}
return ans;
}
```

### **C++**

```cpp
Expand All @@ -135,14 +130,26 @@ public:
```go
func createTargetArray(nums []int, index []int) []int {
target := make([]int, len(nums))
for i, v := range nums {
for i, x := range nums {
copy(target[index[i]+1:], target[index[i]:])
target[index[i]] = v
target[index[i]] = x
}
return target
}
```

### **TypeScript**

```ts
function createTargetArray(nums: number[], index: number[]): number[] {
const ans: number[] = [];
for (let i = 0; i < nums.length; i++) {
ans.splice(index[i], 0, nums[i]);
}
return ans;
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ nums index target
class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
target = []
for i in range(len(nums)):
target.insert(index[i], nums[i])
for x, i in zip(nums, index):
target.insert(i, x)
return target
```

Expand All @@ -87,27 +87,16 @@ class Solution {
for (int i = 0; i < n; ++i) {
target.add(index[i], nums[i]);
}
int[] res = new int[n];
// return target.stream().mapToInt(i -> i).toArray();
int[] ans = new int[n];
for (int i = 0; i < n; ++i) {
res[i] = target.get(i);
ans[i] = target.get(i);
}
return res;
return ans;
}
}
```

### **TypeScript**

```ts
function createTargetArray(nums: number[], index: number[]): number[] {
const ans: number[] = [];
for (let i = 0; i < nums.length; i++) {
ans.splice(index[i], 0, nums[i]);
}
return ans;
}
```

### **C++**

```cpp
Expand All @@ -128,14 +117,26 @@ public:
```go
func createTargetArray(nums []int, index []int) []int {
target := make([]int, len(nums))
for i, v := range nums {
for i, x := range nums {
copy(target[index[i]+1:], target[index[i]:])
target[index[i]] = v
target[index[i]] = x
}
return target
}
```

### **TypeScript**

```ts
function createTargetArray(nums: number[], index: number[]): number[] {
const ans: number[] = [];
for (let i = 0; i < nums.length; i++) {
ans.splice(index[i], 0, nums[i]);
}
return ans;
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
func createTargetArray(nums []int, index []int) []int {
target := make([]int, len(nums))
for i, v := range nums {
for i, x := range nums {
copy(target[index[i]+1:], target[index[i]:])
target[index[i]] = v
target[index[i]] = x
}
return target
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ public int[] createTargetArray(int[] nums, int[] index) {
for (int i = 0; i < n; ++i) {
target.add(index[i], nums[i]);
}
int[] res = new int[n];
// return target.stream().mapToInt(i -> i).toArray();
int[] ans = new int[n];
for (int i = 0; i < n; ++i) {
res[i] = target.get(i);
ans[i] = target.get(i);
}
return res;
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
target = []
for i in range(len(nums)):
target.insert(index[i], nums[i])
for x, i in zip(nums, index):
target.insert(i, x)
return target
78 changes: 51 additions & 27 deletions solution/1400-1499/1402.Reducing Dishes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,17 @@

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

**方法一:排序 + 贪心**
**方法一:贪心 + 排序**

假如我们只选择一道菜,那么我们应该选择满意度最大的那道菜 $s_0$,并且判断 $s_0$ 是否大于 0,如果 $s_0 \leq 0$,那么我们就不做菜了,否则我们做这道菜,得到的总满意度为 $s_0$。

假如我们选择两道菜,那么我们应该选择满足度最大的两道菜 $s_0$ 和 $s_1$,满意度为 $s_1 + 2 \times s_0$,此时要保证选择之后的满意度大于选择之前的满意度,即 $s_1 + 2 \times s_0 > s_0$,即 只要满足 $s_1 + s_0 > 0$,我们就可以选择这两道菜。

依此类推,我们可以得到一个规律,即我们应该选择满意度最大的 $k$ 道菜,并且保证前 $k$ 道菜的满意度之和大于 $0$。

在实现上,我们可以先对所有菜的满意度进行排序,然后从满意度最大的菜开始选择,每次累加当前这道菜的满意度,如果累加的结果小于等于 $0$,那么我们就不再选择后面的菜了,否则我们就选择这道菜。

时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组的长度。

<!-- tabs:start -->

Expand All @@ -65,13 +75,12 @@
class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction.sort(reverse=True)
ans = presum = 0
for v in satisfaction:
presum += v
if presum > 0:
ans += presum
else:
ans = s = 0
for x in satisfaction:
s += x
if s <= 0:
break
ans += s
return ans
```

Expand All @@ -83,14 +92,13 @@ class Solution:
class Solution {
public int maxSatisfaction(int[] satisfaction) {
Arrays.sort(satisfaction);
int ans = 0, presum = 0;
int ans = 0, s = 0;
for (int i = satisfaction.length - 1; i >= 0; --i) {
presum += satisfaction[i];
if (presum > 0) {
ans += presum;
} else {
s += satisfaction[i];
if (s <= 0) {
break;
}
ans += s;
}
return ans;
}
Expand All @@ -104,13 +112,13 @@ class Solution {
public:
int maxSatisfaction(vector<int>& satisfaction) {
sort(rbegin(satisfaction), rend(satisfaction));
int ans = 0, presum = 0;
for (int v : satisfaction) {
presum += v;
if (presum > 0)
ans += presum;
else
int ans = 0, s = 0;
for (int x : satisfaction) {
s += x;
if (s <= 0) {
break;
}
ans += s;
}
return ans;
}
Expand All @@ -120,18 +128,34 @@ public:
### **Go**

```go
func maxSatisfaction(satisfaction []int) int {
sort.Ints(satisfaction)
ans, presum := 0, 0
for i := len(satisfaction) - 1; i >= 0; i-- {
presum += satisfaction[i]
if presum > 0 {
ans += presum
} else {
func maxSatisfaction(satisfaction []int) (ans int) {
sort.Slice(satisfaction, func(i, j int) bool { return satisfaction[i] > satisfaction[j] })
s := 0
for _, x := range satisfaction {
s += x
if s <= 0 {
break
}
ans += s
}
return ans
return
}
```

### **TypeScript**

```ts
function maxSatisfaction(satisfaction: number[]): number {
satisfaction.sort((a, b) => b - a);
let [ans, s] = [0, 0];
for (const x of satisfaction) {
s += x;
if (s <= 0) {
break;
}
ans += s;
}
return ans;
}
```

Expand Down
Loading