.Zzumbong

[leetCode/JS] 739. Daily Temperatures ๋ณธ๋ฌธ

coding test/leetCode

[leetCode/JS] 739. Daily Temperatures

์ญ˜๋ด‰ 2022. 12. 18. 10:21

๋‚œ์ด๋„ [ ๐Ÿค” ] 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] 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
[leetCode/JS] 152. Maximum Product Subarray  (0) 2022.12.14
0 Comments
๋Œ“๊ธ€์“ฐ๊ธฐ ํผ