Skip to content

Commit fb2a947

Browse files
committed
leetcode
1 parent 7528ce2 commit fb2a947

File tree

4 files changed

+219
-0
lines changed

4 files changed

+219
-0
lines changed
+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
3+
- 739. Daily Temperatures -*
4+
5+
6+
Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead.
7+
8+
9+
10+
Example 1:
11+
12+
Input: temperatures = [73,74,75,71,69,72,76,73]
13+
Output: [1,1,4,2,1,1,0,0]
14+
Example 2:
15+
16+
Input: temperatures = [30,40,50,60]
17+
Output: [1,1,1,0]
18+
Example 3:
19+
20+
Input: temperatures = [30,60,90]
21+
Output: [1,1,0]
22+
23+
24+
Constraints:
25+
26+
1 <= temperatures.length <= 105
27+
30 <= temperatures[i] <= 100
28+
29+
*/
30+
31+
/*
32+
33+
34+
35+
*/
36+
37+
class A {
38+
List<int> dailyTemperatures(List<int> temperatures) {
39+
List<int> res = List.filled(temperatures.length, 0);
40+
for (int i = temperatures.length - 2; i >= 0; i--) {
41+
int next = i + 1;
42+
while (
43+
next < temperatures.length && temperatures[next] <= temperatures[i]) {
44+
if (res[next] == 0) {
45+
next = temperatures.length;
46+
break;
47+
}
48+
next += res[next];
49+
}
50+
if (next < temperatures.length) {
51+
res[i] = next - i;
52+
}
53+
}
54+
return res;
55+
}
56+
}
57+
58+
class B {
59+
// TLE
60+
List<int> dailyTemperatures(List<int> temperatures) {
61+
List<int> ar = List.filled(temperatures.length, 0);
62+
if (temperatures.length == 1) {
63+
ar[0] = 0;
64+
} else {
65+
for (int x = 0; x < temperatures.length; x++) {
66+
int p = 0;
67+
// int q = 0;
68+
for (int y = x + 1; y < temperatures.length; y++) {
69+
if (temperatures[x] < temperatures[y]) {
70+
ar[x] = (y - x).abs();
71+
p++;
72+
break;
73+
}
74+
}
75+
if (p == 0) {
76+
ar[x] = 0;
77+
}
78+
}
79+
}
80+
return ar;
81+
}
82+
}
83+
84+
class C {
85+
List<int> dailyTemperatures(List<int> temperatures) {
86+
if (temperatures.isEmpty || temperatures.length == 0) {
87+
return [0];
88+
}
89+
int n = temperatures.length;
90+
List<int> res = List.filled(temperatures.length, 0);
91+
List<int> stack = [];
92+
for (int j = n - 1; j >= 0; j--) {
93+
while (stack.isNotEmpty && temperatures[stack.last] <= temperatures[j])
94+
stack.removeLast();
95+
res[j] = stack.isEmpty ? 0 : stack.last - j;
96+
stack.add(j);
97+
}
98+
return res;
99+
}
100+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
func dailyTemperatures(temperatures []int) []int {
4+
var res []int = make([]int, len(temperatures))
5+
for i := len(temperatures) - 2; i >= 0; i-- {
6+
var next int = i + 1
7+
for next < len(temperatures) && temperatures[next] <= temperatures[i] {
8+
if res[next] == 0 {
9+
next = len(temperatures)
10+
break
11+
}
12+
next += res[next]
13+
}
14+
if next < len(temperatures) {
15+
res[i] = next - i
16+
}
17+
}
18+
return res
19+
}
+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# 🔥 Daily Temperatures 🔥 || 3 Approaches || Simple Fast and Easy || with Explanation
2+
3+
## Explanation
4+
5+
### Idea
6+
7+
First, make a monotonic stack for calculating the next greater element:
8+
9+
Then we will traverse the temperatures array and perform the following operations for every index i:
10+
11+
If the stack is not empty, then we will store the index presently at the top of the stack in a variable index. If the temperature on the ith day is strictly greater than that at the index'th day, then we will update the ans[i] with index-i days as we have found a temperature higher than the current day. The index is removed from the stack i.e perform a pop operation on the stack.
12+
The above step continues till the temperature at the ith day is strictly lesser than that at the index'th day.
13+
If the above steps continue, we will update the ans array at positions less than i.
14+
In the end, we will push the current iteration i into the stack.
15+
Finally, we get the answer array with the number of days we have to wait to get a higher temperature for the current days.
16+
17+
#### - Time Complexity
18+
19+
The time complexity of the above code is O(n).
20+
21+
#### - Space Complexity
22+
23+
The space complexity of the above code is O(n).
24+
25+
## Solution - 1
26+
27+
```dart
28+
class Solution {
29+
List<int> dailyTemperatures(List<int> temperatures) {
30+
List<int> res = List.filled(temperatures.length, 0);
31+
for (int i = temperatures.length - 2; i >= 0; i--) {
32+
int next = i + 1;
33+
while (
34+
next < temperatures.length && temperatures[next] <= temperatures[i]) {
35+
if (res[next] == 0) {
36+
next = temperatures.length;
37+
break;
38+
}
39+
next += res[next];
40+
}
41+
if (next < temperatures.length) {
42+
res[i] = next - i;
43+
}
44+
}
45+
return res;
46+
}
47+
}
48+
```
49+
50+
## Solution - 2
51+
52+
```dart
53+
class Solution {
54+
// TLE
55+
List<int> dailyTemperatures(List<int> temperatures) {
56+
List<int> ar = List.filled(temperatures.length, 0);
57+
if (temperatures.length == 1) {
58+
ar[0] = 0;
59+
} else {
60+
for (int x = 0; x < temperatures.length; x++) {
61+
int p = 0;
62+
for (int y = x + 1; y < temperatures.length; y++) {
63+
if (temperatures[x] < temperatures[y]) {
64+
ar[x] = (y - x).abs();
65+
p++;
66+
break;
67+
}
68+
}
69+
if (p == 0) {
70+
ar[x] = 0;
71+
}
72+
}
73+
}
74+
return ar;
75+
}
76+
}
77+
```
78+
79+
## Solution - 3
80+
81+
```dart
82+
class Solution {
83+
List<int> dailyTemperatures(List<int> temperatures) {
84+
if (temperatures.isEmpty || temperatures.length == 0) {
85+
return [0];
86+
}
87+
int n = temperatures.length;
88+
List<int> res = List.filled(temperatures.length, 0);
89+
List<int> stack = [];
90+
for (int j = n - 1; j >= 0; j--) {
91+
while (stack.isNotEmpty && temperatures[stack.last] <= temperatures[j])
92+
stack.removeLast();
93+
res[j] = stack.isEmpty ? 0 : stack.last - j;
94+
stack.add(j);
95+
}
96+
return res;
97+
}
98+
}
99+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
169169
- [**198.** House Robber](HouseRobber/house_robber.dart)
170170
- [**1143.** Longest Common Subsequence](LongestCommonSubsequence/longest_common_subsequence.dart)
171171
- [**150.** Evaluate Reverse Polish Notation](EvaluateReversePolishNotation/evaluate_reverse_polish_notation.dart)
172+
- [**739.** Daily Temperatures](DailyTemperatures/daily_temperatures.dart)
172173

173174
## Reach me via
174175

0 commit comments

Comments
 (0)