LeetCode TIL 241214

두선아 DusunaΒ·2024λ…„ 12μ›” 14일

algorithm

λͺ©λ‘ 보기
4/14

Try to solve problems in JavaScript, Python, and Java.
This is TIL(memo of key learnings) for solving problems in these languages,
but mostly python.


Solved Problems πŸ“

241214


Key Learnings πŸ€”

how can I make a member variable in python?

In Python, a member variable is defined inside a class by assigning a value to it in the init method, like a constructor in js, it create a instance of the class.

Member variables are then bound to instances of the class, use self to refer to a instance-level variables

class MyClass:
    def __init__(self, value):
        self.member_var = value  # This is a member variable

why should we use array[:] to copy the array in python? (shallow copy)

Data integrity:

  • Protecting the original data
  • Independent data manipulation

You should create a shallow copy of nums, to ensures that self.origin points to a new list, independent nums, while preserving the integrity of the original data.

what is Fisher-Yates shuffle (Knuth shuffle)?

it pronounce as ν”Όμ…”-예이츠 μ…”ν”Œ (λ„ˆμŠ€ μ…”ν”Œ)

Fisher-Yates shuffle is an algorithm for randomly shuffling a list in-place.

πŸ‘‰ ensures a uniform random shuffle, with time complexity O(n).
πŸ‘‰ no additional memory, because it shuffles in-place.

import random
def fisher_yates_shuffle(nums):
    for i in range(len(nums) - 1, 0, -1):
        j = random.randint(0, i)
        nums[i], nums[j] = nums[j], nums[i]
    return nums

module random in python, how to use it?

https://www.w3schools.com/python/module_random.asp

  • random.shuffle: shuffles a list in place
  • random.sample: can make a new list, shuffles it, and return it.

why random.sample is slower than random.shuffle?

differences between random.shuffle() and random.sample()

  • random.shuffle works in-place, modifying the original list and does not create a new list. It is generally faster because it directly alters the list.
  • random.sample creates a new list, returns a shuffled version of the original. The extra overhead of creating a new list and copying elements contributes to its slower performance, especially for large lists.

how can I do shallow copy in java?

Shallow copy of an array or collection can be done using the clone(), it functions like ArrayList's constructor for lists.

// shallow copy for an array
int[] original = {1, 2, 3};
int[] copy = original.clone();

// shallow copy for an ArrayList
ArrayList<Integer> originalList = new ArrayList<>(Arrays.asList(1, 2, 3));
ArrayList<Integer> copyList = new ArrayList<>(originalList); 

why do we declare the instance as a class-level field?

Declaring the Random object as a class-level field (instance variable), to ensures that you only create object once per instance of the class.

πŸ‘‰ avoids unnecessary object creation.

public class Solution {
    private Random random = new Random();  // class-level field
    
    public int[] shuffle() {
        // use the class-level random object
        int j = random.nextInt(i + 1);
    }
}
profile
μ•ˆλ…•ν•˜μ„Έμš”.

0개의 λŒ“κΈ€