每日温度

给定一个每日temperatures的列表,产生一个列表,对于输入的每天,告诉我们你要等多少天才能够等到一个更高的温度。如果没有可能的未来日期,输出0作为替代。

比如,给定列表 temperatures = [73, 74, 75, 71, 69, 72, 76, 73],你的输出应该是[1, 1, 4, 2, 1, 1, 0, 0]

样例

样例 1:
	输入:  temperatures = [73, 74, 75, 71, 69, 72, 76, 73]
	输出:  [1, 1, 4, 2, 1, 1, 0, 0]
	
	解释:
	找到每个数字后面第一个大于自己的数字,输出两者的距离。

	
样例 2:
	输入: temperatures = [50, 40, 39, 30]
	输出:  [0,0,0,0]
	

注意事项

  1. temperatures的长度应该在[1, 30000]范围内。每一个温度应该是一个在 [30, 100]内的整数。
class Solution {
public:
    /**
     * @param temperatures: a list of daily temperatures
     * @return: a list of how many days you would have to wait until a warmer temperature
     */
    vector<int> dailyTemperatures(vector<int> &T) {
        // Write your code here
        vector<int> ans(T.size());
        stack<int> s; // 这里放元素索引,而不是元素
        for (int i = T.size() - 1; i >= 0; i--) {
            while (!s.empty() && T[s.top()] <= T[i]) {
                s.pop();
            }
            ans[i] = s.empty() ? 0 : (s.top() - i); // 得到索引间距
            s.push(i); // 加入索引,而不是元素
        }
        return ans;
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,根据您的要求,我会回答您的问题。首先,需要说明的是,这个问题涉及到比较复杂的数据处理和算法,需要您具备一定的Hadoop MapReduce编程基础。下面是该问题的解决方案和代码实现: 1. 数据格式 本次数据分析使用的数据格式为气象数据,包含以下字段: - date: 日期(格式为YYYY-MM-DD) - time: 时间(格式为hh:mm:ss) - temperature: 温度(单位为摄氏度) 数据格式如下: ``` 2019-01-01,00:00:00,20 2019-01-01,01:00:00,22 2019-01-01,02:00:00,18 ...... ``` 2. 数据分析思路 本次数据分析需要筛选出每日温度最高的三个小时段。具体思路如下: - 将数据按日期和时间分组; - 对于每个日期,找出该日期温度最高的三个小时段; - 输出结果,格式为:日期、小时段、温度。 3. MapReduce 代码实现 下面是基于Eclipse Hadoop MapReduce的代码实现,包含Mapper、Reducer和Driver三个类。在Mapper类中,首先将输入按日期和时间分组,然后将每组数据作为一个键值对发送给Reducer。在Reducer类中,首先对每个日期的所有数据进行排序,然后取出每个日期温度最高的三个小时段,最后输出结果。 Mapper类: ```java public static class TempMapper extends Mapper<LongWritable, Text, Text, Text> { private Text outKey = new Text(); private Text outValue = new Text(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); String[] fields = line.split(","); String date = fields[0].trim(); String time = fields[1].trim(); String temperature = fields[2].trim(); outKey.set(date); outValue.set(time + "," + temperature); context.write(outKey, outValue); } } ``` Reducer类: ```java public static class TempReducer extends Reducer<Text, Text, Text, Text> { private Text outKey = new Text(); private Text outValue = new Text(); public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { List<String[]> dataList = new ArrayList<String[]>(); for (Text value : values) { String[] fields = value.toString().split(","); dataList.add(fields); } Collections.sort(dataList, new Comparator<String[]>() { public int compare(String[] o1, String[] o2) { return Integer.parseInt(o2[1]) - Integer.parseInt(o1[1]); } }); String top1 = dataList.get(0)[0]; String top2 = dataList.get(1)[0]; String top3 = dataList.get(2)[0]; outKey.set(key.toString()); outValue.set(top1 + "," + top2 + "," + top3); context.write(outKey, outValue); } } ``` Driver类: ```java public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "Temperature Analysis"); job.setJarByClass(TemperatureAnalysis.class); job.setMapperClass(TempMapper.class); job.setReducerClass(TempReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } ``` 4. 总结 本次数据分析使用了Hadoop MapReduce框架,通过Mapper和Reducer的协作,实现了对气象数据的分析。需要注意的是,本次方案仅是一种思路,实际应用中需要根据具体情况进行调整和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值