Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8e51cc6

Browse files
committed
feat: add/update solutions to lc problems: No.0496,1118
* Update solutions to lc problem: No.0496.Next Greater Element I * Add solutions to lc problem: No.1118.Number of Days in a Month
1 parent 862e62a commit 8e51cc6

File tree

13 files changed

+226
-149
lines changed

13 files changed

+226
-149
lines changed

‎solution/0400-0499/0496.Next Greater Element I/README.md‎

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,13 @@ for i in range(n):
7474
```python
7575
class Solution:
7676
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
77-
stk = []
7877
mp = {}
79-
for num in nums2[::-1]:
80-
while stk and stk[-1] <= num:
81-
stk.pop()
82-
mp[num] = stk[-1] ifstkelse-1
78+
stk = []
79+
for num in nums2:
80+
while stk andstk[-1] < num:
81+
mp[stk.pop()] = num
8382
stk.append(num)
84-
return [mp[num] for num in nums1]
83+
return [mp.get(num, -1) for num in nums1]
8584
```
8685

8786
### **Java**
@@ -93,19 +92,18 @@ class Solution {
9392
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
9493
Deque<Integer> stk = new ArrayDeque<>();
9594
Map<Integer, Integer> mp = new HashMap<>();
96-
for (int i = nums2.length -1; i >=0; --i) {
97-
while (!stk.isEmpty() && stk.peek() <= nums2[i]) {
98-
stk.pop();
95+
for (int num : nums2) {
96+
while (!stk.isEmpty() && stk.peek() < num) {
97+
mp.put(stk.pop(), num);
9998
}
100-
mp.put(nums2[i], stk.isEmpty() ? -1 : stk.peek());
101-
stk.push(nums2[i]);
99+
stk.push(num);
102100
}
103101
int n = nums1.length;
104-
int[] res = new int[n];
102+
int[] ans = new int[n];
105103
for (int i = 0; i < n; ++i) {
106-
res[i] = mp.get(nums1[i]);
104+
ans[i] = mp.getOrDefault(nums1[i], -1);
107105
}
108-
return res;
106+
return ans;
109107
}
110108
}
111109
```
@@ -118,17 +116,16 @@ class Solution {
118116
* @param {number[]} nums2
119117
* @return {number[]}
120118
*/
121-
var nextGreaterElement = function (nums1, nums2) {
122-
let stack = [];
123-
let nextGreater = {};
124-
for (let num of nums2) {
125-
while (stack.length > 0 && stack[stack.length - 1] < num) {
126-
nextGreater[stack.pop()] = num;
119+
var nextGreaterElement = function(nums1, nums2) {
120+
let stk = [];
121+
let nextGreater = {};
122+
for (let num of nums2) {
123+
while (stk && stk[stk.length - 1] < num) {
124+
nextGreater[stk.pop()] = num;
125+
}
126+
stk.push(num);
127127
}
128-
stack.push(num);
129-
}
130-
let res = nums1.map((d) => nextGreater[d] || -1);
131-
return res;
128+
return nums1.map(e => nextGreater[e] || -1);
132129
};
133130
```
134131

@@ -137,20 +134,21 @@ var nextGreaterElement = function (nums1, nums2) {
137134
```cpp
138135
class Solution {
139136
public:
140-
vector<int> nextGreaterElement(vector<int> &nums1, vector<int> &nums2) {
137+
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
141138
stack<int> stk;
142139
unordered_map<int, int> mp;
143-
for (int i = nums2.size() - 1; i >= 0; --i)
140+
for (int num : nums2)
144141
{
145-
while (!stk.empty() && stk.top() <= nums2[i])
142+
while (!stk.empty() && stk.top() < num)
143+
{
144+
mp[stk.top()] = num;
146145
stk.pop();
147-
mp[nums2[i]] = stk.empty() ? -1 : stk.top();
148-
stk.push(nums2[i]);
146+
}
147+
stk.push(num);
149148
}
150-
vector<int> res;
151-
for (int num : nums1)
152-
res.push_back(mp[num]);
153-
return res;
149+
vector<int> ans;
150+
for (int num : nums1) ans.push_back(mp.count(num) ? mp[num] : -1);
151+
return ans;
154152
}
155153
};
156154
```
@@ -161,22 +159,22 @@ public:
161159
func nextGreaterElement(nums1 []int, nums2 []int) []int {
162160
var stk []int
163161
mp := make(map[int]int)
164-
for i := len(nums2) - 1; i >= 0; i-- {
165-
for len(stk) > 0 && stk[len(stk)-1] <= nums2[i] {
162+
for _, num := range nums2 {
163+
for len(stk) > 0 && stk[len(stk)-1] < num {
164+
mp[stk[len(stk)-1]] = num
166165
stk = stk[:len(stk)-1]
167166
}
168-
if len(stk) > 0 {
169-
mp[nums2[i]] = stk[len(stk)-1]
170-
} else {
171-
mp[nums2[i]] = -1
172-
}
173-
stk = append(stk, nums2[i])
167+
stk = append(stk, num)
174168
}
175-
var res []int
169+
var ans []int
176170
for _, num := range nums1 {
177-
res = append(res, mp[num])
171+
val, ok := mp[num]
172+
if !ok {
173+
val = -1
174+
}
175+
ans = append(ans, val)
178176
}
179-
return res
177+
return ans
180178
}
181179
```
182180

‎solution/0400-0499/0496.Next Greater Element I/README_EN.md‎

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,13 @@ For number 4 in the first array, there is no next greater number for it in the s
5252
```python
5353
class Solution:
5454
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
55-
stk = []
5655
mp = {}
57-
for num in nums2[::-1]:
58-
while stk and stk[-1] <= num:
59-
stk.pop()
60-
mp[num] = stk[-1] ifstkelse-1
56+
stk = []
57+
for num in nums2:
58+
while stk andstk[-1] < num:
59+
mp[stk.pop()] = num
6160
stk.append(num)
62-
return [mp[num] for num in nums1]
61+
return [mp.get(num, -1) for num in nums1]
6362
```
6463

6564
### **Java**
@@ -69,19 +68,18 @@ class Solution {
6968
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
7069
Deque<Integer> stk = new ArrayDeque<>();
7170
Map<Integer, Integer> mp = new HashMap<>();
72-
for (int i = nums2.length -1; i >=0; --i) {
73-
while (!stk.isEmpty() && stk.peek() <= nums2[i]) {
74-
stk.pop();
71+
for (int num : nums2) {
72+
while (!stk.isEmpty() && stk.peek() < num) {
73+
mp.put(stk.pop(), num);
7574
}
76-
mp.put(nums2[i], stk.isEmpty() ? -1 : stk.peek());
77-
stk.push(nums2[i]);
75+
stk.push(num);
7876
}
7977
int n = nums1.length;
80-
int[] res = new int[n];
78+
int[] ans = new int[n];
8179
for (int i = 0; i < n; ++i) {
82-
res[i] = mp.get(nums1[i]);
80+
ans[i] = mp.getOrDefault(nums1[i], -1);
8381
}
84-
return res;
82+
return ans;
8583
}
8684
}
8785
```
@@ -95,16 +93,15 @@ class Solution {
9593
* @return {number[]}
9694
*/
9795
var nextGreaterElement = function(nums1, nums2) {
98-
let stack = [];
96+
let stk = [];
9997
let nextGreater = {};
10098
for (let num of nums2) {
101-
while (stack.length>0&& stack[stack.length - 1] < num) {
102-
nextGreater[stack.pop()] = num;
99+
while (stk && stk[stk.length - 1] < num) {
100+
nextGreater[stk.pop()] = num;
103101
}
104-
stack.push(num);
102+
stk.push(num);
105103
}
106-
let res = nums1.map(d => nextGreater[d] || -1);
107-
return res;
104+
return nums1.map(e => nextGreater[e] || -1);
108105
};
109106
```
110107

@@ -113,20 +110,21 @@ var nextGreaterElement = function(nums1, nums2) {
113110
```cpp
114111
class Solution {
115112
public:
116-
vector<int> nextGreaterElement(vector<int> &nums1, vector<int> &nums2) {
113+
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
117114
stack<int> stk;
118115
unordered_map<int, int> mp;
119-
for (int i = nums2.size() - 1; i >= 0; --i)
116+
for (int num : nums2)
120117
{
121-
while (!stk.empty() && stk.top() <= nums2[i])
118+
while (!stk.empty() && stk.top() < num)
119+
{
120+
mp[stk.top()] = num;
122121
stk.pop();
123-
mp[nums2[i]] = stk.empty() ? -1 : stk.top();
124-
stk.push(nums2[i]);
122+
}
123+
stk.push(num);
125124
}
126-
vector<int> res;
127-
for (int num : nums1)
128-
res.push_back(mp[num]);
129-
return res;
125+
vector<int> ans;
126+
for (int num : nums1) ans.push_back(mp.count(num) ? mp[num] : -1);
127+
return ans;
130128
}
131129
};
132130
```
@@ -137,22 +135,22 @@ public:
137135
func nextGreaterElement(nums1 []int, nums2 []int) []int {
138136
var stk []int
139137
mp := make(map[int]int)
140-
for i := len(nums2) - 1; i >= 0; i-- {
141-
for len(stk) > 0 && stk[len(stk)-1] <= nums2[i] {
138+
for _, num := range nums2 {
139+
for len(stk) > 0 && stk[len(stk)-1] < num {
140+
mp[stk[len(stk)-1]] = num
142141
stk = stk[:len(stk)-1]
143142
}
144-
if len(stk) > 0 {
145-
mp[nums2[i]] = stk[len(stk)-1]
146-
} else {
147-
mp[nums2[i]] = -1
148-
}
149-
stk = append(stk, nums2[i])
143+
stk = append(stk, num)
150144
}
151-
var res []int
145+
var ans []int
152146
for _, num := range nums1 {
153-
res = append(res, mp[num])
147+
val, ok := mp[num]
148+
if !ok {
149+
val = -1
150+
}
151+
ans = append(ans, val)
154152
}
155-
return res
153+
return ans
156154
}
157155
```
158156

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
class Solution {
22
public:
3-
vector<int> nextGreaterElement(vector<int> &nums1, vector<int> &nums2) {
3+
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
44
stack<int> stk;
55
unordered_map<int, int> mp;
6-
for (int i = nums2.size() - 1; i >= 0; --i)
6+
for (int num : nums2)
77
{
8-
while (!stk.empty() && stk.top() <= nums2[i])
8+
while (!stk.empty() && stk.top() < num)
9+
{
10+
mp[stk.top()] = num;
911
stk.pop();
10-
mp[nums2[i]] = stk.empty() ? -1 : stk.top();
11-
stk.push(nums2[i]);
12+
}
13+
stk.push(num);
1214
}
13-
vector<int> res;
14-
for (int num : nums1)
15-
res.push_back(mp[num]);
16-
return res;
15+
vector<int> ans;
16+
for (int num : nums1) ans.push_back(mp.count(num) ? mp[num] : -1);
17+
return ans;
1718
}
1819
};
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
func nextGreaterElement(nums1 []int, nums2 []int) []int {
22
var stk []int
33
mp := make(map[int]int)
4-
for i := len(nums2) - 1; i >= 0; i-- {
5-
for len(stk) > 0 && stk[len(stk)-1] <= nums2[i] {
4+
for _, num := range nums2 {
5+
for len(stk) > 0 && stk[len(stk)-1] < num {
6+
mp[stk[len(stk)-1]] = num
67
stk = stk[:len(stk)-1]
78
}
8-
if len(stk) > 0 {
9-
mp[nums2[i]] = stk[len(stk)-1]
10-
} else {
11-
mp[nums2[i]] = -1
12-
}
13-
stk = append(stk, nums2[i])
9+
stk = append(stk, num)
1410
}
15-
var res []int
11+
var ans []int
1612
for _, num := range nums1 {
17-
res = append(res, mp[num])
13+
val, ok := mp[num]
14+
if !ok {
15+
val = -1
16+
}
17+
ans = append(ans, val)
1818
}
19-
return res
19+
return ans
2020
}

‎solution/0400-0499/0496.Next Greater Element I/Solution.java‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@ class Solution {
22
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
33
Deque<Integer> stk = new ArrayDeque<>();
44
Map<Integer, Integer> mp = new HashMap<>();
5-
for (int i = nums2.length - 1; i >= 0; --i) {
6-
while (!stk.isEmpty() && stk.peek() <= nums2[i]) {
7-
stk.pop();
5+
for (int num : nums2) {
6+
while (!stk.isEmpty() && stk.peek() <num) {
7+
mp.put(stk.pop(), num);
88
}
9-
mp.put(nums2[i], stk.isEmpty() ? -1 : stk.peek());
10-
stk.push(nums2[i]);
9+
stk.push(num);
1110
}
1211
int n = nums1.length;
13-
int[] res = new int[n];
12+
int[] ans = new int[n];
1413
for (int i = 0; i < n; ++i) {
15-
res[i] = mp.get(nums1[i]);
14+
ans[i] = mp.getOrDefault(nums1[i], -1);
1615
}
17-
return res;
16+
return ans;
1817
}
1918
}

‎solution/0400-0499/0496.Next Greater Element I/Solution.js‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
* @return {number[]}
55
*/
66
var nextGreaterElement = function(nums1, nums2) {
7-
let stack = [];
7+
let stk = [];
88
let nextGreater = {};
99
for (let num of nums2) {
10-
while (stack.length>0&& stack[stack.length - 1] < num) {
11-
nextGreater[stack.pop()] = num;
10+
while (stk&& stk[stk.length - 1] < num) {
11+
nextGreater[stk.pop()] = num;
1212
}
13-
stack.push(num);
13+
stk.push(num);
1414
}
15-
let res = nums1.map(d => nextGreater[d] || -1);
16-
return res;
15+
return nums1.map(e => nextGreater[e] || -1);
1716
};
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
class Solution:
22
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
3-
stk = []
43
mp = {}
5-
fornuminnums2[::-1]:
6-
whilestkandstk[-1] <=num:
7-
stk.pop()
8-
mp[num] =stk[-1] ifstkelse-1
4+
stk= []
5+
fornuminnums2:
6+
whilestkandstk[-1] <num:
7+
mp[stk.pop()] =num
98
stk.append(num)
10-
return [mp[num] for num in nums1]
9+
return [mp.get(num, -1) for num in nums1]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /