File tree Expand file tree Collapse file tree 4 files changed +50
-2
lines changed
19_python内置常用算法和数据结构/builtins Expand file tree Collapse file tree 4 files changed +50
-2
lines changed Original file line number Diff line number Diff line change 194
194
< li class ="toctree-l2 "> < a href ="#python_1 "> python 交换列表元素的坑</ a > </ li >
195
195
196
196
197
+ < li class ="toctree-l2 "> < a href ="#_4 "> 兼容提交格式</ a > </ li >
198
+
199
+
197
200
</ ul >
198
201
</ li >
199
202
@@ -636,6 +639,51 @@ <h1 id="python_1">python 交换列表元素的坑</h1>
636
639
637
640
return n+1
638
641
</ code > </ pre >
642
+ < h1 id ="_4 "> 兼容提交格式</ h1 >
643
+ < p > 注意牛客网有两种模式,一种是和 leetcode 一样的提交(无需处理输入),只需要提交核心代码。
644
+ 一种是 ACM 模式,还需要自己处理输入和输出。
645
+ 建议使用这种兼容写法,同样的题目可以同时提交到 牛客和leetcode。
646
+ 这道题目为例子 [679] 奖品分配 https://www.acwing.com/problem/content/681/</ p >
647
+ < pre > < code class ="language-py "> # 这段代码可以直接以OJ输入模式提交,如果题目一样,直接复制 Solution 类就可以同时提交到leetcode
648
+ class Solution:
649
+ def solve(self, scores):
650
+ """
651
+ 思路:记忆化搜索。时间O(N)
652
+ 对于旁边都比自己大的点,它肯定是1
653
+ 对于旁边有比自己小的点,先算出比自己小的点的值再+1就好了。
654
+ 每个点如果计算过了就记忆化,下次再计算他的时候不用重复递归直接返回。
655
+ 参考:https://www.acwing.com/solution/acwing/content/1520/
656
+ """
657
+ from functools import lru_cache
658
+ n = len(scores)
659
+
660
+ @lru_cache(maxsize=None)
661
+ def dfs(x):
662
+ left = (x-1+n) % n
663
+ right = (x+1) % n
664
+
665
+ if scores[x] <= scores[left] and scores[x] <= scores[right]: # 注意是 <= ,下边是 <
666
+ return 1
667
+
668
+ l, r = 0, 0
669
+ if scores[left] < scores[x]:
670
+ l = dfs(left)
671
+ if scores[right] < scores[x]:
672
+ r = dfs(right)
673
+
674
+ return max(l, r) + 1
675
+
676
+ return sum([dfs(i) for i in range(n)])
677
+
678
+
679
+ if __name__ == "__main__": # python3 提交,python3 input 都当做 str 输入
680
+ so = Solution() # 构造 Solution 实例后续调用
681
+ n = int(input())
682
+ for i in range(n):
683
+ arrlen = input()
684
+ arr = list(map(int, input().split()))
685
+ print(so.solve(arr))
686
+ </ code > </ pre >
639
687
640
688
</ div >
641
689
</ div >
Original file line number Diff line number Diff line change @@ -599,5 +599,5 @@ <h2 id="_22">本电子书制作和写作方式</h2>
599
599
600
600
<!--
601
601
MkDocs version : 1.0.4
602
- Build Date UTC : 2022-02-19 02:41:07
602
+ Build Date UTC : 2022-02-19 03:29:49
603
603
-->
Load Diff Large diffs are not rendered by default.
You can’t perform that action at this time.
0 commit comments