File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed
docs/19_python内置常用算法和数据结构 Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -383,3 +383,52 @@ class Solution(object):
383383
384384 return n+1
385385```
386+
387+ # 兼容提交格式
388+
389+ 注意牛客网有两种模式,一种是和 leetcode 一样的提交(无需处理输入),只需要提交核心代码。
390+ 一种是 ACM 模式,还需要自己处理输入和输出。
391+ 建议使用这种兼容写法,同样的题目可以同时提交到 牛客和leetcode。
392+ 这道题目为例子 [ 679] 奖品分配 https://www.acwing.com/problem/content/681/
393+
394+ ``` py
395+ # 这段代码可以直接以OJ输入模式提交,如果题目一样,直接复制 Solution 类就可以同时提交到leetcode
396+ class Solution :
397+ def solve (self , scores ):
398+ """
399+ 思路:记忆化搜索。时间O(N)
400+ 对于旁边都比自己大的点,它肯定是1
401+ 对于旁边有比自己小的点,先算出比自己小的点的值再+1就好了。
402+ 每个点如果计算过了就记忆化,下次再计算他的时候不用重复递归直接返回。
403+ 参考:https://www.acwing.com/solution/acwing/content/1520/
404+ """
405+ from functools import lru_cache
406+ n = len (scores)
407+
408+ @lru_cache (maxsize = None )
409+ def dfs (x ):
410+ left = (x- 1 + n) % n
411+ right = (x+ 1 ) % n
412+
413+ if scores[x] <= scores[left] and scores[x] <= scores[right]: # 注意是 <= ,下边是 <
414+ return 1
415+
416+ l, r = 0 , 0
417+ if scores[left] < scores[x]:
418+ l = dfs(left)
419+ if scores[right] < scores[x]:
420+ r = dfs(right)
421+
422+ return max (l, r) + 1
423+
424+ return sum ([dfs(i) for i in range (n)])
425+
426+
427+ if __name__ == " __main__" : # python3 提交,python3 input 都当做 str 输入
428+ so = Solution() # 构造 Solution 实例后续调用
429+ n = int (input ())
430+ for i in range (n):
431+ arrlen = input ()
432+ arr = list (map (int , input ().split()))
433+ print (so.solve(arr))
434+ ```
You can’t perform that action at this time.
0 commit comments