Skip to content

Commit 10d9dca

Browse files
committed
add 402
1 parent e03a639 commit 10d9dca

7 files changed

+195
-24
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
3636
| ----- | ----- | ----- | ----- |
3737
| [--] | [9c-basic.md](./list/9c-basic.md) | 80/129 | 2 vips |
3838
| [--] | [9c-top.md](./list/9c-top.md) | 25/48 | - |
39-
| [--] | [lee215.md](./list/lee215.md) | 2/34 | - |
39+
| [--] | [leetcode-lee215.md](./list/leetcode-lee215.md) | 2/34 | - |
4040
| [] | [9c-dp.md](./list/9c-dp.md) | 42/45 | 3 vips |
4141
| [] | [geekbang.md](./list/geekbang.md) | 55/55 | - |
4242
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
43-
| [🔲] | [9c-advanced.md](./list/9c-advanced.md) | 65/92 | 16 vips |
43+
| [🔲] | [9c-advanced.md](./list/9c-advanced.md) | 66/92 | 16 vips |
4444
| [🔲] | [leetcode-contest.md](./list/leetcode-contest.md) | 10/40 | - |
45+
| [🔲] | [leetcode-google.md](./list/leetcode-google.md) | 0/7 | - |
4546

46-
**Solved**: 441 problems
47+
**Solved**: 442 problems
4748

4849
## 类型/Category
4950

@@ -793,13 +794,14 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
793794

794795
## Array
795796

796-
| Link | Problem(5) | Solution | Tag | Time | Space | Ref |
797+
| Link | Problem(6) | Solution | Tag | Time | Space | Ref |
797798
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
798799
| [Leetcode-2210](https://leetcode.com/problems/count-hills-and-valleys-in-an-array/) | Count Hills And Valleys In An Array | [c++](./leetcode/2210.count-hills-and-valleys-in-an-array.cpp), [python3](./leetcode/2210.count-hills-and-valleys-in-an-array.py) | Array | O\(N\) | O\(1\) | - |
799800
| [Leetcode-3285](https://leetcode.com/problems/find-indices-of-stable-mountains/) | Find Indices Of Stable Mountains | [c++](./leetcode/3285.find-indices-of-stable-mountains.cpp), [python3](./leetcode/3285.find-indices-of-stable-mountains.py) | Array | O\(N\) | O\(1\) | - |
800801
| [Leetcode-674](https://leetcode.com/problems/longest-continuous-increasing-subsequence/) | Longest Continuous Increasing Subsequence | [c++](./leetcode/674.longest-continuous-increasing-subsequence.cpp), [python3](./leetcode/674.longest-continuous-increasing-subsequence.py) | Array | O\(N\) | O\(1\) | - |
801802
| [Leetcode-665](https://leetcode.com/problems/non-decreasing-array/) | Non Decreasing Array | [c++](./leetcode/665.non-decreasing-array.cpp), [python3](./leetcode/665.non-decreasing-array.py) | Array | O\(N\) | O\(1\) | - |
802803
| [Leetcode-238](https://leetcode.com/problems/product-of-array-except-self/) | Product Of Array Except Self | [c++](./leetcode/238.product-of-array-except-self.cpp), [python3](./leetcode/238.product-of-array-except-self.py) | Array | O\(N\) | O\(1\) | - |
804+
| [Lintcode-402](https://www.lintcode.com/problem/continuous-subarray-sum/) | Continuous Subarray Sum | [c++](./lintcode/402.continuous-subarray-sum.cpp), [python3](./lintcode/402.continuous-subarray-sum.py) | Array | O\(N\) | O\(N\) | - |
803805

804806
## String
805807

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Tag: Prefix Sum Array, Array
2+
// Time: O(N)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
7+
// Given an integer array, find a continuous subarray where the sum of numbers is the biggest.
8+
// Your code should return the index of the first number and the index of the last number.
9+
// (If their are duplicate answer, return the minimum one in lexicographical order)
10+
//
11+
// **Example 1:**
12+
//
13+
// ```
14+
// Input: [-3, 1, 3, -3, 4]
15+
// Output: [1, 4]
16+
// ```
17+
//
18+
// **Example 2:**
19+
//
20+
// ```
21+
// Input: [0, 1, 0, 1]
22+
// Output: [0, 3]
23+
// Explanation: The minimum one in lexicographical order.
24+
// ```
25+
//
26+
//
27+
28+
class Solution {
29+
public:
30+
/**
31+
* @param a: An integer array
32+
* @return: A list of integers includes the index of the first number and the index of the last number
33+
*/
34+
vector<int> continuousSubarraySum(vector<int> &a) {
35+
// write your code here
36+
int n = a.size();
37+
vector<int> prefix(n + 1, 0);
38+
int l = 0;
39+
int r = 0;
40+
int min_index = 0;
41+
for (int i = 0; i < n; i++) {
42+
prefix[i + 1] = prefix[i] + a[i];
43+
if (prefix[i + 1] - prefix[min_index] > prefix[r + 1] - prefix[l]) {
44+
l = min_index;
45+
r = i;
46+
}
47+
48+
if (prefix[min_index] > prefix[i + 1]) {
49+
min_index = i + 1;
50+
}
51+
}
52+
return vector<int> {l, r};
53+
}
54+
};
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Tag: Prefix Sum Array, Array
2+
# Time: O(N)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
7+
# Given an integer array, find a continuous subarray where the sum of numbers is the biggest.
8+
# Your code should return the index of the first number and the index of the last number.
9+
# (If their are duplicate answer, return the minimum one in lexicographical order)
10+
#
11+
# **Example 1:**
12+
#
13+
# ```
14+
# Input: [-3, 1, 3, -3, 4]
15+
# Output: [1, 4]
16+
# ```
17+
#
18+
# **Example 2:**
19+
#
20+
# ```
21+
# Input: [0, 1, 0, 1]
22+
# Output: [0, 3]
23+
# Explanation: The minimum one in lexicographical order.
24+
# ```
25+
#
26+
#
27+
28+
from typing import (
29+
List,
30+
)
31+
32+
class Solution:
33+
"""
34+
@param a: An integer array
35+
@return: A list of integers includes the index of the first number and the index of the last number
36+
"""
37+
def continuous_subarray_sum(self, a: List[int]) -> List[int]:
38+
# write your code here
39+
n = len(a)
40+
prefix = [0 for i in range(n + 1)]
41+
42+
l = 0
43+
r = 0
44+
min_index = 0
45+
for i in range(n):
46+
prefix[i + 1] = prefix[i] + a[i]
47+
48+
if prefix[i + 1] - prefix[min_index] > prefix[r + 1] - prefix[l]:
49+
l = min_index
50+
r = i
51+
52+
if prefix[min_index] > prefix[i + 1]:
53+
min_index = i + 1
54+
55+
return [l, r]
56+
57+
class Solution:
58+
"""
59+
@param a: An integer array
60+
@return: A list of integers includes the index of the first number and the index of the last number
61+
"""
62+
def continuous_subarray_sum(self, a: List[int]) -> List[int]:
63+
# write your code here
64+
n = len(a)
65+
dp = [0 for i in range(n)]
66+
dp[0] = a[0]
67+
68+
j = 0
69+
l = 0
70+
r = 0
71+
for i in range(1, n):
72+
if dp[i - 1] >= 0:
73+
dp[i] = dp[i - 1] + a[i]
74+
else:
75+
dp[i] = a[i]
76+
j = i
77+
78+
if dp[i] > dp[r]:
79+
r = i
80+
l = j
81+
82+
return [l, r]

list/9c-advanced.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@
9898
79. https://www.lintcode.com/problem/subarray-sum/
9999
80. https://www.lintcode.com/problem/subarray-sum-closest/
100100
81. https://www.lintcode.com/problem/submatrix-sum/
101+
82. https://www.lintcode.com/problem/continuous-subarray-sum/
101102

102-
- https://www.lintcode.com/problem/continuous-subarray-sum/
103103
- https://www.lintcode.com/problem/maximum-subarray/
104104
- https://www.lintcode.com/problem/continuous-subarray-sum-ii/
105105
- https://www.lintcode.com/problem/kth-largest-element/

list/leetcode-google.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# [Leetcode Google Online Accessment](https://leetcode.com/discuss/interview-question/352460/Google-Online-Assessment-Questions)
2+
3+
- https://leetcode.com/discuss/interview-question/553399/
4+
- https://leetcode.com/discuss/interview-question/553399/
5+
- https://leetcode.com/discuss/interview-question/396769/
6+
- https://leetcode.com/discuss/interview-question/356433/
7+
- https://leetcode.com/discuss/interview-question/421787/
8+
- https://leetcode.com/problems/minimum-domino-rotations-for-equal-row
9+
- https://leetcode.com/discuss/interview-question/356477
10+
- https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree
11+
- https://leetcode.com/discuss/interview-question/356520
12+
- https://leetcode.com/problems/k-closest-points-to-origin
13+
- https://leetcode.com/problems/odd-even-jump
14+
- https://leetcode.com/problems/license-key-formatting
15+
- https://leetcode.com/problems/unique-email-addresses
16+
- https://leetcode.com/problems/fruit-into-baskets
17+
- https://leetcode.com/discuss/interview-question/334191
18+
- https://leetcode.com/discuss/interview-question/341295/Google-or-Online-Assessment-2019-or-Fill-Matrix
19+
- https://leetcode.com/discuss/interview-question/350233/Google-or-Summer-Intern-OA-2019-or-Decreasing-Subsequences
20+
- https://leetcode.com/discuss/interview-question/350363/Google-or-OA-2018-or-Max-Distance
21+
- https://leetcode.com/discuss/interview-question/350248/Google-or-Summer-Intern-OA-2019-or-Stores-and-Houses
22+
- https://leetcode.com/discuss/interview-question/396996/
23+
- https://leetcode.com/discuss/interview-question/397156/
24+
- https://leetcode.com/discuss/interview-question/397524/
25+
- https://leetcode.com/discuss/interview-question/356935/
26+
- https://leetcode.com/discuss/interview-question/356378/
27+
28+
File renamed without changes.

test/python.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,33 @@
1111
)
1212

1313
class Solution:
14-
"""
15-
@param nums: A list of integers
16-
@return: A list of integers includes the index of the first number and the index of the last number
17-
"""
18-
def subarray_sum(self, nums: List[int]) -> List[int]:
19-
# write your code here
20-
pre = 0
21-
table = {pre: 0}
22-
for i in range(len(nums)):
23-
pre += nums[i]
24-
table
25-
print(pre, table)
26-
27-
print(table)
28-
29-
return [0, 0]
30-
31-
14+
def minOperations(self, nums: List[int]) -> int:
15+
n = len(nums)
16+
right_min = list(nums)
17+
for i in range(n - 2, -1, -1):
18+
right_min[i] = min(right_min[i + 1], nums[i])
19+
20+
res = 0
21+
for i in range(n - 1):
22+
if nums[i] == nums[i + 1] or nums[i] <= right_min[i + 1]:
23+
continue
24+
25+
for d in range(2, right_min[i + 1] + 1):
26+
if nums[i] % d == 0:
27+
res += 1
28+
j = i
29+
while j > 0 and nums[j] == nums[j - 1]:
30+
res += 1
31+
j -= 1
32+
break
33+
else:
34+
return -1
35+
36+
return res
3237

3338

3439
s = Solution()
3540
ts = datetime.now()
36-
res = s.subarray_sum([3,1,5,8])
41+
res = s.minOperations([675,675,149,149,233])
3742
print(datetime.now() - ts)
3843
print(res)

0 commit comments

Comments
 (0)