-
[알고리즘] #Daily Temperatures■ Algorithm 2019. 5. 12. 21:12
출처 : https://leetcode.com/problems/daily-temperatures/
Daily Temperatures - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
문제
Given a list of daily temperatures T, return a list such that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0 instead.
For example, given the list of temperatures T = [73, 74, 75, 71, 69, 72, 76, 73], your output should be [1, 1, 4, 2, 1, 1, 0, 0].
Note: The length of temperatures will be in the range [1, 30000]. Each temperature will be an integer in the range [30, 100].
번역
일일 온도 T의 목록이 주어지면, 주어진 날짜에 대해 더 따뜻한 온도가 될 때까지 기다려야하는 일 수를 알려주는 리스트를 반환하세요. 더이상 따뜻해질 날이 없으면 0을 리턴하세요.
#이중 for문을 이용한 풀이
기준 날짜와 그 이후의 날짜들을 비교하면서 기준 날짜보다 온도가 큰 경우에는 해당날짜까지의 일 수를 리턴한다.
import java.util.Arrays; public class Main { public static void main(String[] args) { Solution solution = new Solution(); int[] t = {73, 74, 75, 71, 69, 72, 76, 73}; int[] answer = solution.dailyTemperatures(t); System.out.println(Arrays.toString(answer)); } } class Solution { public int[] dailyTemperatures(int[] T) { int[] answer = new int[T.length]; for(int i=0; i<T.length; i++) { int day = 0; for(int j=i+1; j<T.length; j++) { day++; if(T[i] < T[j]) { answer[i] = day; break; } } } return answer; } }
결과 값
[1, 1, 4, 2, 1, 1, 0, 0]
'■ Algorithm' 카테고리의 다른 글
[알고리즘] #Maximum Depth of Binary Tree (0) 2019.05.19 [알고리즘] #Palindromic Substrings (0) 2019.05.12 [알고리즘] #Longest Substring Without Repeating Characters (0) 2019.05.12 [알고리즘] #Longest Univalue Path (0) 2019.05.07 [알고리즘] #네트워크 (0) 2019.04.24