力扣解法汇总911-在线选举

原题链接:力扣


描述:

给你两个整数数组 persons 和 times 。在选举中,第 i 张票是在时刻为 times[i] 时投给候选人 persons[i] 的。

对于发生在时刻 t 的每个查询,需要找出在 t 时刻在选举中领先的候选人的编号。

在 t 时刻投出的选票也将被计入我们的查询之中。在平局的情况下,最近获得投票的候选人将会获胜。

实现 TopVotedCandidate 类:

TopVotedCandidate(int[] persons, int[] times) 使用 persons 和 times 数组初始化对象。
int q(int t) 根据前面描述的规则,返回在时刻 t 在选举中领先的候选人的编号。
 
示例:

输入:
["TopVotedCandidate", "q", "q", "q", "q", "q", "q"]
[[[0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]], [3], [12], [25], [15], [24], [8]]
输出:
[null, 0, 1, 1, 0, 0, 1]

解释:
TopVotedCandidate topVotedCandidate = new TopVotedCandidate([0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]);
topVotedCandidate.q(3); // 返回 0 ,在时刻 3 ,票数分布为 [0] ,编号为 0 的候选人领先。
topVotedCandidate.q(12); // 返回 1 ,在时刻 12 ,票数分布为 [0,1,1] ,编号为 1 的候选人领先。
topVotedCandidate.q(25); // 返回 1 ,在时刻 25 ,票数分布为 [0,1,1,0,0,1] ,编号为 1 的候选人领先。(在平局的情况下,1 是最近获得投票的候选人)。
topVotedCandidate.q(15); // 返回 0
topVotedCandidate.q(24); // 返回 0
topVotedCandidate.q(8); // 返回 1

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/online-election
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解题思路:

* 成员变量如下:
* voteMap:实时的记录选举人和票数
* maxList:按时间顺序记录对应时刻的胜利者
* times:时间数组
* 遍历persons,动态的去计票,则每次循环中,voteMap中记录着当时的选举人的票数。则就可以直到对应时间谁是胜者。
* 结束后,输入一个时间,根据二分法查找找到对应时间在times中的index值,再去maxList取即可。

代码:

public class Solution911 {

    public static class TopVotedCandidate {
        /**
         * key:选举人
         * value:票数
         */
        Map<Integer, Integer> voteMap = new HashMap<>();
        /**
         * 时间点对应的胜者
         */
        List<Integer> maxList = new ArrayList<>();

        int[] times;


        public TopVotedCandidate(int[] persons, int[] times) {
            this.times = times;
            int win = 0;

            for (int i = 0; i < times.length; i++) {
                int person = persons[i];
//                int time = times[i];//时间点
                Integer integer = voteMap.getOrDefault(person, 0);
                voteMap.put(person, ++integer);
                Integer integer1 = voteMap.get(win);
                if (integer >= integer1) {
                    win = person;
                }
                maxList.add(win);
            }
        }

        public int q(int t) {
            //查找t在time中的位置
            int index = middleSelect(t);
            Integer win = maxList.get(index);
            return win;
        }

        /**
         * @param t
         * @return index位置
         */
        private int middleSelect(int t) {
            int start = 0;
            int end = times.length - 1;
            if (t >= times[end]) {
                return end;
            }
            int middle;
            do {
                middle = (start + end) / 2;
                if (t == times[middle]) {
                    return middle;
                }
                if (t > times[middle]) {
                    if (t < times[middle + 1]) {
                        return middle;
                    }
                    start = middle;
                } else {
                    if (t > times[middle - 1]) {
                        return middle - 1;
                    }
                    end = middle;
                }
            } while (true);
        }

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

失落夏天

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

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

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

打赏作者

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

抵扣说明:

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

余额充值