首页 \ 问答 \ 从阵列中找到领导者(Find the leader from an Array)

从阵列中找到领导者(Find the leader from an Array)

领导者是一个数组,它比数组元素的半角出现更多。 如果数组中的两个连续部分具有THAT领导者(即,领导者出现在respective部分中的其他元素的一半以上),则等同领导者将被定义。 由于该部分可以变化,该阵列可以有多个Equi-Leader。 说,我找到了一个阵列的领导者x,现在我想找到一些领导者。

如果一个部分和连续部分x出现的次数多于其他元素(该部分的x> non_X),是否意味着在这两个部分x中出现了超过一半的其他元素? 请记住, x_Total > Array_Length/2

一个部分是一个子阵列,也就是说,将这个元素放在S的索引中。这样就创建了2个连续的[0,1,.....,S]和[S + 1,S + 2,...的子阵列。 。N-1],我们需要两个部门的领导。

我知道这是事实,但是,如何证明这一点? 不寻找代码实现。


A leader is a number that occurs MORE than HALF of the elements of an array. The Equi-leader will be defined if two consecutive section in the array has THAT leader (ie the leader occurs more than half of other elements in the respective section). As the section can vary, the array can have more than one Equi-leader. Say, I find the leader x of an array and now I would like to find the number of equi-leaders.

If a section and the consecutive section x occurs more than other elements (x > non_X for the segment), does it mean that in those 2 segments x occurred more than half of other elements as well? Remember, x_Total > Array_Length/2

A section is a subarray, say, take the element in the index of S. That creates 2 consecutive subarrays of [0,1,....., S] and [S+1, S+2, ..... N-1] and we need leaders both of the sections.

I know this is true, but, how to prove that? Not looking for code implementation.


原文:https://stackoverflow.com/questions/50888814
更新时间:2023-04-29 12:04

最满意答案

有很多方法可以解决这个问题,这实际上取决于你如何进行导航。 如果您正在使用实际链接或使用router.navigate()更改路由,您的router将调度route:<route name>事件,您可以侦听,将相同的参数传递给处理程序,因为它传递给路由功能。

结果证明用Backbone设置测试用例代码需要多长时间,我让你成为一个jsFiddle来说明这种方法: http//jsfiddle.net/nrabinowitz/ZrgJF/7/

很多这只是设置代码; 这个问题的重要部分是路由器:

var MyRouter = Backbone.Router.extend({
    routes: {
        'view/:id' : 'openView'
    },
    openView: function(id) {
        app.openView(id)
    }
});
router = new MyRouter();

并且视图将删除绑定到路由:

var MyView = Backbone.View.extend({
    initialize: function(opts) {
        this.id = opts.id;
        router.bind('route:openView', this.dispose, this); 
    },
    // id is the same as the route argument
    dispose: function(id) {
        if (id != this.id) {
            this.remove();
        }
    }
    // etc
});

There are lots of ways to approach this, and it really depends on how you're doing the navigation. If you're changing routes with actual links, or by using router.navigate(), your router will dispatch a route:<route name> event that you can listen to, passing the same arguments to the handler as it passes to the route function.

In what turned out to be a demonstration of just how long it takes to set up test case code with Backbone, I made you a jsFiddle to illustrate this approach: http://jsfiddle.net/nrabinowitz/ZrgJF/7/

A lot of this is just setup code; the important parts for this question are the router:

var MyRouter = Backbone.Router.extend({
    routes: {
        'view/:id' : 'openView'
    },
    openView: function(id) {
        app.openView(id)
    }
});
router = new MyRouter();

And the view, which binds removal to the route:

var MyView = Backbone.View.extend({
    initialize: function(opts) {
        this.id = opts.id;
        router.bind('route:openView', this.dispose, this); 
    },
    // id is the same as the route argument
    dispose: function(id) {
        if (id != this.id) {
            this.remove();
        }
    }
    // etc
});

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)