260415
260415

260415

  • Debian Setup
  • AutoBuilder
Backlinks (0)

No backlinks found.

260413
260413

260413

Ramp의 AX (회사를 AI로 물들이는 법)

Backlinks (0)

No backlinks found.

260409
260409

260409

  • AutoBuilder

Cursor is incredibly good harness.

Opus is also not Oputhetic anymore

Backlinks (0)

No backlinks found.

0001 Two Sum
0001 Two Sum

0001 Two Sum

Solved at: 220710

Question

  • Two Sum - LeetCode

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.

Solution

So the first obvious answer is to iterate twice. This finishes calculations in O(n2)O(n^2)O(n2) time.

python
class Solution:    def twoSum(self, nums: List[int], target: int) -> List[int]:        for idx1, val1 in enumerate(nums):            for idx2, val2 in enumerate(nums):                if idx1 == idx2:                    continue                if val1 + val2 == target:                    return [idx1, idx2]

However, this gives a timeout.

Improved

I used Python Dictionary to store complementing values. Python Dictionary will have O(1)O(1)O(1) access time for most cases. This solution will run in O(n)O(n)O(n) time.

  • One caveat: depending on the hash function, it can go as bad as O(n2)O(n^2)O(n2).
python
class Solution:    def twoSum(self, nums: List[int], target: int) -> List[int]:
        # map for complementing elements: complementary-idx        complementing_map = {}
        for idx, val in enumerate(nums):            if val in complementing_map:                return [complementing_map[val], idx]            complementing_map[target - val] = idx

Results

Runtime

  • 60 ms, faster than 97.16% of Python3 online submissions for Two Sum.

Memory Usage

  • 15.4 MB, less than 14.24% of Python3 online submissions for Two Sum.

Other Answers Online

  • Sort first, O(nlog⁡n)O(n \log n)O(nlogn)
  • For all elements, O(n)O(n)O(n)
    • Perform binary search O(log⁡n)O(\log n)O(logn)
  • In total: O(nlog⁡n)O(n \log n)O(nlogn)
Backlinks (2)
  • 220710
  • Coding Tests
Index
cho.sh
I prefer CLIBB9A08260619260619컴퓨트로늄37A88F컴퓨트로늄0CF03F컴퓨트로늄2C60FB260618260618260418260418260528260528AutoBuilder63849A260419260419Setup9AC296StellaD226F7260415260415Debian SetupD2F701260414260414anaclumos/configs/AGENTS.mdED86A3Ramp의 AX (회사를 AI로 물들이는 법)840774260413260413How to get your company AI pilled46544C260411260411260409260409260407260407260406260406Separating Claude Code Personal Sub and Claude Code Company Sub33A53C
Warning
This post is more than a year old. Information may be outdated.
class Solution:    def twoSum(self, nums: List[int], target: int) -> List[int]:        for idx1, val1 in enumerate(nums):            for idx2, val2 in enumerate(nums):                if idx1 == idx2:                    continue                if val1 + val2 == target:                    return [idx1, idx2]
class Solution:    def twoSum(self, nums: List[int], target: int) -> List[int]:
        # map for complementing elements: complementary-idx        complementing_map = {}
        for idx, val in enumerate(nums):            if val in complementing_map:                return [complementing_map[val], idx]            complementing_map[target - val] = idx