000/Python

/Python/2019/07/29/1829-2997.py
# coding=utf-8
# 1829. 最大数组和 II
# https://leetcode-cn.com/problems/maximize-array-value/
class Solution(object):
def maximizeArrayValue(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
import math
nums = [i - j for i, j in zip(nums, nums[1:])]
nums.sort(reverse=True)
nums = [math.ceil(i / len(nums)) for i in nums]

return sum(nums)

/Python/2019/07/29/328-387.py
# coding=utf-8
# 328. 奇偶问题
# https://leetcode-cn.com/problems/odd-even-linked-list/
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution(object):
def oddEvenList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head:
return head

odd_head = head
odd_tail = odd_head
even_head = head.next
even_tail = even_head

while odd_tail.next and even_tail.next:
odd_tail.next = even_tail.next
odd_tail = odd_tail.next
even_tail.next = odd_tail.next
even_tail = even_tail.next

odd_tail.next = even_head

return odd_head

/Python/2019/07/29/367-529.py
# coding=utf-8
# 367. 有效的完全平方根
# https://leetcode-cn.com/problems/valid-perfect-square/

import math

class Solution(object):
def isPerfectSquare(self, num):
"""
:type num: int
:rtype: bool
"""

if num < 2:
return True

for i in range(1, int(math.sqrt(num)) + 2):
if i * i == num:
return True
return False

/Python/2019/07/29/717-1226.py
# coding=utf-8
# 717. 1-bit and 2-bit Characters
# https://leetcode-cn.com/problems/10000-bit-and-20000-bit-characters/

import collections

class Solution(object):
def isOneBitCharacter(self, bits):
"""
:type bits: List[int]
:rtype: bool
"""

count = 0
for i in bits:
if i == 1:
count += 1
if count == 2:
count = 0

return not count

/Python/2019/07/29/927-1612.py
# coding=utf-8
# 927. 三连击
# https://leetcode-cn.com/problems/triangle-attack/
# 给定一个正整数N,表示在N个点上进行三连击,找出最小的攻击次数。
# 例如:N = 5,可以分为(1, 2, 3) (2, 3, 4) (3, 4, 5) 三种方式
# 例如:N = 6,可以分为(1, 2, 3) (2, 3, 4) (3, 4, 5) (4, 5, 6) 四种方式
# 例如:N = 7,可以分为(1, 2, 3) (2, 3, 4) (3, 4, 5) (4, 5, 6) (5, 6, 7) 五种方式
# 例如:N = 10,可以分为(1, 2, 3) (2, 3, 4) (3, 4, 5) (4, 5, 6) (5, 6, 7) (6, 7, 8) (7, 8, 9) (8, 9, 10) 八种方式
# 例如:N = 12,可以分为(1, 2, 3) (2, 3, 4) (3, 4, 5) (4, 5, 6) (5, 6, 7) (6, 7, 8) (7, 8, 9) (8, 9, 10) (9, 10, 11) (10, 11, 12) 10种方式

from collections import Counter

class Solution(object):
def waysToClimb(self, n):
"""
:type n: int
:rtype: int
"""
if n <= 3:
return n
if n == 4:
return 3
if n >= 5:
return self.waysToClimb(n - 2) + self.waysToClimb(n - 3) + self.waysToClimb(n - 4)

def waysToClimb1(self, n):
"""
:type n: