Skip to content

Commit ce6f2fb

Browse files
committed
update 53, 215
1 parent 0510305 commit ce6f2fb

8 files changed

+63
-29
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
4545
| [] | [9c-dp.md](./list/9c-dp.md) | 42/45 | 3 vips |
4646
| [] | [geekbang.md](./list/geekbang.md) | 55/55 | - |
4747
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
48-
| [🔲] | [9c-advanced.md](./list/9c-advanced.md) | 66/92 | 16 vips |
48+
| [🔲] | [9c-advanced.md](./list/9c-advanced.md) | 67/92 | 17 vips |
4949
| [🔲] | [leetcode-contest.md](./list/leetcode-contest.md) | 10/40 | - |
5050
| [🔲] | [leetcode-google.md](./list/leetcode-google.md) | 0/7 | - |
5151

leetcode/215.kth-largest-element-in-an-array.cpp

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,6 @@ class Solution {
4949
};
5050

5151
class Solution {
52-
private:
53-
int partition(vector<int>& nums, int start, int end) {
54-
int l = start + 1;
55-
int r = end;
56-
int pivot = nums[start];
57-
58-
while (l <= r) {
59-
if (nums[l] > pivot && nums[r] < pivot) {
60-
swap(nums[l++], nums[r--]);
61-
}
62-
if (nums[l] <= pivot) {
63-
l++;
64-
}
65-
if (nums[r] >= pivot) {
66-
r--;
67-
}
68-
}
69-
swap(nums[start], nums[r]);
70-
return r;
71-
}
72-
7352
public:
7453
int findKthLargest(vector<int>& nums, int k) {
7554

@@ -90,4 +69,24 @@ class Solution {
9069

9170
return nums[k];
9271
}
93-
};
72+
73+
int partition(vector<int>& nums, int start, int end) {
74+
int l = start + 1;
75+
int r = end;
76+
int pivot = nums[start];
77+
78+
while (l <= r) {
79+
if (nums[l] > pivot && nums[r] < pivot) {
80+
swap(nums[l], nums[r]);
81+
}
82+
if (nums[l] <= pivot) {
83+
l++;
84+
}
85+
if (nums[r] >= pivot) {
86+
r--;
87+
}
88+
}
89+
swap(nums[start], nums[r]);
90+
return r;
91+
}
92+
};

leetcode/215.kth-largest-element-in-an-array.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ def partition(self, nums, start, end):
7474
while l <= r:
7575
if (nums[l] > pivot and nums[r] < pivot):
7676
nums[l], nums[r] = nums[r], nums[l]
77-
l += 1
78-
r -= 1
7977

8078
if nums[l] <= pivot:
8179
l += 1

leetcode/53.maximum-subarray.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,37 @@ class Solution {
6666

6767
return largest;
6868
}
69+
};
70+
71+
// follow-up
72+
class Solution {
73+
public:
74+
int maxSubArray(vector<int>& nums) {
75+
return helper(nums, 0, nums.size() - 1);
76+
}
77+
78+
int helper(vector<int>& nums, int left, int right) {
79+
if (left == right) {
80+
return nums[left];
81+
}
82+
83+
int mid = (left + right) >> 1;
84+
int cur = 0;
85+
int left_max = INT_MIN;
86+
for (int i = mid; i >= left; i--) {
87+
cur += nums[i];
88+
left_max = max(left_max, cur);
89+
}
90+
91+
cur = 0;
92+
int right_max = INT_MIN;
93+
for (int i = mid + 1; i <= right; i++) {
94+
cur += nums[i];
95+
right_max = max(right_max, cur);
96+
}
97+
98+
int child_max = max(helper(nums, left, mid), helper(nums, mid + 1, right));
99+
return max(child_max, left_max + right_max);
100+
101+
}
69102
};

leetcode/53.maximum-subarray.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def maxSubArray(self, nums: List[int]) -> int:
6363

6464
return largest
6565

66+
# follow-up
6667
class Solution:
6768
def maxSubArray(self, nums: List[int]) -> int:
6869
return self.helper(nums, 0, len(nums) - 1)

lintcode/403.continuous-subarray-sum-ii.vip

Whitespace-only changes.

list/9c-advanced.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@
9999
80. https://www.lintcode.com/problem/subarray-sum-closest/
100100
81. https://www.lintcode.com/problem/submatrix-sum/
101101
82. https://www.lintcode.com/problem/continuous-subarray-sum/
102+
83. https://www.lintcode.com/problem/maximum-subarray/
103+
84. https://www.lintcode.com/problem/continuous-subarray-sum-ii/
104+
85. https://leetcode.com/problems/kth-largest-element-in-an-array/
102105

103-
- https://www.lintcode.com/problem/maximum-subarray/
104-
- https://www.lintcode.com/problem/continuous-subarray-sum-ii/
105-
- https://www.lintcode.com/problem/kth-largest-element/
106106
- https://www.lintcode.com/problem/wiggle-sort/
107107
- https://www.lintcode.com/problem/wiggle-sort-ii/
108108
- https://www.lintcode.com/problem/nuts-bolts-problem/

pre_commit_readme_update.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,10 @@ def list_table(f):
305305

306306
if len(missing) > 0:
307307
Logger.log(f'----{file_name} missing {len(missing)} link----', Logger.WARNING)
308-
Logger.log(f'{random.sample(missing, 5)}')
308+
if len(missing) >= 5:
309+
Logger.log(f'{random.sample(missing, 5)}')
310+
else:
311+
Logger.log(f'{missing}')
309312

310313
Markdown.table_header(f, ['Status', 'List', 'Progress', 'Notes'])
311314
for row in sorted(list_row):

0 commit comments

Comments
 (0)