我们有一个整数数组X。任务是返回一个Y相同大小的数组,其中ith的元素是具有该元素的最大Y子数组的计数。ithX
例如:
X: [8, 7, 1, 12, 11, 4, 10, 2, 3, 6, 9]
Y: [3, 2, 1, 32, 7, 1, 10, 1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)
这是我的解决方案,时间复杂度为二次。
X: [8, 7, 1, 12, 11, 4, 10, 2, 3, 6, 9]
Y: [3, 2, 1, 32, 7, 1, 10, 1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)
这个想法是,当元素大于当前元素时,我们使用两个指针向左和向右移动。一旦我们有了当前元素为 max 的数组,我们就可以通过以下方式获取子数组的数量(start_index) * (end_index - start_index + 1)
该算法必须在非常大的测试用例上运行。如何将时间复杂度至少降低到NlogN?
我已经实现了一个自定义QTextEdit类,在其中重写了该keyPressEvent方法。
我想检查按下的键是否属于以下键:
1. 大写字母
2. 小写字母
3. 空格
如果我的函数声明是这样的:
def keyPressEvent(self, event):
Run Code Online (Sandbox Code Playgroud)
我可以做类似的事情:
if event.key() in (
QtCore.Qt.Key_A,
QtCore.Qt.Key_B,
QtCore.Qt.Key_C,
QtCore.Qt.Key_D,
....
....
Run Code Online (Sandbox Code Playgroud)
等等。
我将如何循环这些值?我将如何访问按下的实际字符?我想创建一串按下的按键,只要它们是字母即可。