逆序 码蹄集

题目来源:码蹄集

题目描述:

在这里插入图片描述
在这里插入图片描述

Python代码实现:

参考链接:https://yxsmarter.blog.csdn.net/article/details/128211350?spm=1001.2014.3001.5502

import time
from typing import List,Tuple
from collections import deque, Counter
from queue import PriorityQueue
import math
from functools import lru_cache
import random
import copy


def main():
    n = int(input())
    arr = list(map(int,input().split()))

    ss = set()
    c = Counter()
    ma,mi=-1,-1

    for v in arr:
        if ma<v:
            ss.add(v)
        else:
            if mi<v:
                c[ma]+=1
        
        if v>ma:
            ma,mi=v,ma
        elif v>mi:
            mi=v

    mx = -1
    ans = None
    for i in range(1,n+1):
        val = 0
        if i in ss:
            val -= 1
        if i in c:
            val+=c[i]
        if val>mx:
            mx=val
            ans=i

    print(ans)


if __name__ == '__main__':
    main();

C++代码实现:

#include <iostream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>

using namespace std;

int main() {
    int n;
    cin >> n;

    vector<int> arr(n);
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
    }

    unordered_set<int> ss;
    unordered_map<int, int> c;
    int ma = -1, mi = -1;

    for (int v : arr) {
        if (ma < v) {
            ss.insert(v);
        } else {
            if (mi < v) {
                c[ma]++;
            }
        }

        if (v > ma) {
            mi = ma;
            ma = v;
        } else if (v > mi) {
            mi = v;
        }
    }

    int mx = -1;
    int ans = -1;
    for (int i = 1; i <= n; ++i) {
        int val = 0;
        if (ss.count(i)) {
            val -= 1;
        }
        if (c.count(i)) {
            val += c[i];
        }
        if (val > mx) {
            mx = val;
            ans = i;
        }
    }

    cout << ans << endl;

    return 0;
}

Java代码实现:

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();

        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }

        Set<Integer> ss = new HashSet<>();
        Map<Integer, Integer> c = new HashMap<>();
        int ma = -1, mi = -1;

        for (int v : arr) {
            if (ma < v) {
                ss.add(v);
            } else {
                if (mi < v) {
                    c.put(ma, c.getOrDefault(ma, 0) + 1);
                }
            }

            if (v > ma) {
                mi = ma;
                ma = v;
            } else if (v > mi) {
                mi = v;
            }
        }

        int mx = -1;
        int ans = -1;
        for (int i = 1; i <= n; ++i) {
            int val = 0;
            if (ss.contains(i)) {
                val -= 1;
            }
            if (c.containsKey(i)) {
                val += c.get(i);
            }
            if (val > mx) {
                mx = val;
                ans = i;
            }
        }

        System.out.println(ans);
    }
}

代码提交测试结果:

在这里插入图片描述
附B站老师讲解链接可供参考:https://www.bilibili.com/video/BV1cs4y137za/?t=1885.1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Magneto_万磁王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值