Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- Sort
- ํ์ค์คํค
- ์ฝ๋ฉํ ์คํธ
- ์ ์ค์๋ด
- ์ฐ์ฃผ
- LeetCode
- ๊ฒ์
- ๋์ซ
- ์๊ณ ๋ฆฌ์ฆ
- ์ ์ค
- ์ก๋ฆฌ๋จ
- ์ ์ฅ
- ์ฝํ
- javascript
- ์กํ
- ๋ง์ง
- ์ ํฌ๋ธ
Archives
- Today
- Total
.Zzumbong
[leetCode/JS] 739. Daily Temperatures ๋ณธ๋ฌธ
๋์ด๋ [ ๐ค ] Medium
๋ฌธ์ ์ค๋ช
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.
์ผ์ผ ์จ๋๊ฐ ์ ๋ ฅ๋ ๋ฐฐ์ด์์ ๋ ๋ฐ๋ฏํด์ง๋ ๋ ๊น์ง์ ์ผ ์์ ๋ฐฐ์ด์ ๋ฐํํ๋ฉด๋๋ค. ์์ผ๋ฉด 0์ผ๋ก ๋ฆฌํดํ๋ค.
์ ์ถ๋ ฅ ์
Example 1:
Input: temperatures = [73,74,75,71,69,72,76,73]
Output: [1,1,4,2,1,1,0,0]
Example 2:
Input: temperatures = [30,40,50,60]
Output: [1,1,1,0]
Example 3:
Input: temperatures = [30,60,90]
Output: [1,1,0]
Constraints
1 <= temperatures.length <= 105
30 <= temperatures\[i\] <= 100
๋ด ์๋ฃจ์
- ๊ทธ๋ฅ ์ํํด์ index ์ฐพ์์ index ๋ฆฌํดํด์ ๋๋ผ๋ผ ํ๋๋ฐ...
- Time Limit์ ๊ฑธ๋ฆฌ๊ณ beats 5%๋ก ์์ฒญ ๋๋ ธ๋ค.
// Time Limit
var dailyTemperatures = function(temperatures) {
const answer = [];
for(let i = 0; i < temperatures.length; i++) {
const index = temperatures.slice(i+1).findIndex(t => temperatures[i] < t) + 1;
answer.push(index);
}
return answer;
};
// Runtime 8683ms, beats 5%
var dailyTemperatures = function(temperatures) {
const answer = [];
for(let i = 0; i < temperatures.length; i++) {
for(let j = i; j < temperatures.length; j++) {
if(temperatures[i] < temperatures[j]) {
answer.push(j-i);
break;
} else if ( j === temperatures.length - 1){
answer.push(0);
}
}
}
return answer;
};
- ๊ฒฐ๊ตญ์ ๊ฒ์์์! 2ํฌ์ธํธ ์์น๋ ์คํ์ ์๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋๋ผ..
- ์ดํดํ๊ธฐ ์ฌ์ด๊ฑด ์คํ์ ์ด์ฉํ ํ์์ธ๋ฐ, ํ์ฌ์ ์ธ๋ฑ์ค๋ฅผ ์์๋๊ณ
- ๊ฐ์ฅ ์ต๊ทผ์ ์์ธ ์จ๋๋ณด๋ค ๋์ ์จ๋๊ฐ ๋์ค๋ฉด ์คํ์ ๊บผ๋ด๊ณ ๊ทธ ์ฐจ์ด๋ฅผ ๊ณ์ฐ์์ ์ ๋ ฅํ๋ ๋ฐฉ์์ด๋ค.
- ๋ง๋ก๋ ์ด๋ ต์ง๋ง ์ฝ๋๋ฅผ ๋ณด๋ฉด ์ฌ์ ๋ค. ์ฅ์์ฅ
var dailyTemperatures = function(temperatures) {
const answer = Array(temperatures.length).fill(0);
const stack = [];
for(let i = 0; i < temperatures.length; i++) {
let top = stack[stack.length-1];
while(stack.length && temperatures[top] < temperatures[i]) {
const idx = stack.pop();
answer[idx] = i - idx;
top = stack[stack.length-1];
}
stack.push(i);
}
return answer;
};
๊ฐ์ํ
- ์ด๋ณด๋ค ๋ ๋๋ฆฌ๊ฒ ํ ์ ์์๊น?
- ๋คํ๋ค~ ์ถ์ ๋, ๋๋ ค์ ๋น ๊พธ๋จน์ผ๋ฉด ์ง์ง ํ์จ์ด ใ ใ
'coding test > leetCode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[leetCode/JS] 905. Sort Array By Parity (0) | 2023.09.28 |
---|---|
[leetCode/JS] 841. Keys and Rooms (0) | 2022.12.20 |
[leetCode/JS] 150. Evaluate Reverse Polish Notation (0) | 2022.12.17 |
[leetCode/JS] 232. Implement Queue using Stacks (0) | 2022.12.16 |
[leetCode/JS] 1143. Longest Common Subsequence (0) | 2022.12.15 |
Comments