iT邦幫忙

2024 iThome 鐵人賽

DAY 27
0
自我挑戰組

Leetcode 解題之旅:逐日攻克系列 第 27

每日一LeetCode(27)

  • 分享至 

  • xImage
  •  

1051. Height Checker

題目敘述:

A school is trying to take an annual photo of all the students. The students are asked to stand in a single file line in non-decreasing order by height. Let this ordering be represented by the integer array expected where expected[i] is the expected height of the ith student in line.

You are given an integer array heights representing the current order that the students are standing in. Each heights[i] is the height of the ith student in line (0-indexed).

Return the number of indices where heights[i] != expected[i].

class Solution {
public:
    int heightChecker(vector<int>& heights) {
        vector<int> v(heights);
        sort(v.begin(),v.end());
        int count=0;
        for(int i=0;i<heights.size();i++)
        {
            if(heights[i] != v[i])
            count++;
        }
        return count;
    }
};

上一篇
每日一LeetCode(26)
下一篇
每日一LeetCode(28)
系列文
Leetcode 解題之旅:逐日攻克30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言