Skip to content

Latest commit

 

History

History
44 lines (33 loc) · 1.29 KB

File metadata and controls

44 lines (33 loc) · 1.29 KB

Array

An array stores elements of the same type in contiguous memory, enabling O(1) random access by index.

Complexity

Operation Time
Access O(1)
Insert / Delete at end O(1)
Insert / Delete in middle O(n) — shifting elements

Python Usage

Python's list is a dynamic array that grows automatically.

# Create
nums = [10, 20, 30]

# Access & modify
nums[0], nums[-1]    # first & last
nums[1] = 25          # update

# Common operations
nums.sort(); nums.reverse()
max(nums); min(nums); sum(nums)

# Dynamic resizing
nums.append(40)       # O(1) amortized
nums.pop()            # O(1)
nums.remove(20)       # O(n)

Related LeetCode Questions