Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

61 9kyo hwang #214

Merged
merged 12 commits into from
Sep 19, 2024
Merged

61 9kyo hwang #214

merged 12 commits into from
Sep 19, 2024

Conversation

9kyo-hwang
Copy link
Collaborator

πŸ”— 문제 링크

ν…Œμ΄λΈ” ν•΄μ‹œ ν•¨μˆ˜

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

15λΆ„

✨ μˆ˜λ„ μ½”λ“œ

1. 문제

2차원 ν–‰λ ¬λ‘œ ν‘œν˜„λ˜λŠ” ν…Œμ΄λΈ”μ΄ 주어진닀. 각각의 열은 컬럼, 행은 νŠœν”Œμ„ λ‚˜νƒ€λ‚΄λ©°, 첫 번째 μ»¬λŸΌμ€ λͺ¨λ“  νŠœν”Œμ— λŒ€ν•΄ κ·Έ 값이 μ€‘λ³΅λ˜μ§€ μ•Šλ„λ‘ 보μž₯λœλ‹€.

이 ν…Œμ΄λΈ”μ— λŒ€ν•œ ν•΄μ‹œ ν•¨μˆ˜λ₯Ό λ‹€μŒκ³Ό 같이 μ •μ˜ν•œλ‹€.

  1. ν•΄μ‹œ ν•¨μˆ˜λŠ” col, row_begin, row_endλ₯Ό μž…λ ₯λ°›λŠ”λ‹€.
  2. ν…Œμ΄λΈ”μ˜ νŠœν”Œμ„ col번째 컬럼 κ°’ κΈ°μ€€μœΌλ‘œ μ˜€λ¦„μ°¨μˆœ μ •λ ¬ν•œλ‹€. 단, κ·Έ 값이 λ™μΌν•œ 경우 기본킀인 첫 번째 컬럼 값을 κΈ°μ€€μœΌλ‘œ λ‚΄λ¦Όμ°¨μˆœ μ •λ ¬ν•œλ‹€.
  3. μ •λ ¬λœ λ°μ΄ν„°μ—μ„œ $S_i$λ₯Ό i번째 ν–‰ νŠœν”Œμ— λŒ€ν•΄ 각 컬럼 값을 i둜 λ‚˜λˆˆ λ‚˜λ¨Έμ§€λ“€μ˜ ν•©μœΌλ‘œ μ •μ˜ν•œλ‹€.
    $S_i =$ data[i][0] $mod \space i$ + data[i][1] $mod \space i$ + ...
  4. row_begin $\le i \le$ row_end인 λͺ¨λ“  $S_i$λ₯Ό λˆ„μ ν•˜μ—¬ bitwise XORν•œ 값을 ν•΄μ‹œ κ°’μœΌλ‘œ λ°˜ν™˜ν•œλ‹€.

ν…Œμ΄λΈ”μ˜ ν•΄μ‹œ 값을 returnν•˜λΌ.

μ œν•œ 사항

  • $1 \le$ data 길이 $\le 2,500$
  • $1 \le$ data의 μ›μ†Œ 길이 $\le 500$
  • $1 \le$ data[i][j] $\le 1,000,000$, data[i][j]λŠ” $i+1$번째 νŠœν”Œ $j+1$번째 컬럼 κ°’
  • $1 \le col \le$ data의 μ›μ†Œ 길이
  • $1 \le$ row_begin $\le$ row_end $\le$ data 길이

2. 풀이

λ¬Έμ œμ—μ„œ μ„€λͺ…ν•œ λŒ€λ‘œ κ΅¬ν˜„ν•˜λ©΄ λ˜λŠ” μ‰¬μš΄ λ¬Έμ œμ΄λ‹€.

νŠœν”Œ μ •λ ¬

sort(Data.begin(), Data.end(), [&](const auto& Lhs, const auto& Rhs)
     {
         return Lhs[Col - 1] == Rhs[Col - 1] ? Lhs[0] > Rhs[0] : Lhs[Col - 1] < Rhs[Col - 1];
     });

col번째 컬럼 값이 λ™μΌν•˜λ‹€λ©΄ 기본킀인 첫 번째 컬럼 값을 κΈ°μ€€μœΌλ‘œ λ‚΄λ¦Όμ°¨μˆœμ„, μ•„λ‹ˆλΌλ©΄ ν•΄λ‹Ή 값을 κΈ°μ€€μœΌλ‘œ μ˜€λ¦„μ°¨μˆœ μ •λ ¬ν•œλ‹€.

  • col 값이 1λΆ€ν„° μ‹œμž‘ν•˜κΈ° λ•Œλ¬Έμ— col - 1을 κΈ°μ€€μœΌλ‘œ μž‘λŠ”λ‹€.

$S_i$ κ΅¬ν•˜κΈ° 및 ν•΄μ‹œκ°’ κ°±μ‹ 

int HashVal = 0;
for(int i = RowBegin; i <= RowEnd; ++i)
{
    int S_i = 0;
    for(const int Val : Data[i - 1])
    {
        S_i += Val % i;
    }
    
    HashVal ^= S_i;
}

μš°λ¦¬κ°€ ν•„μš”ν•œ 건 row_begin, row_end ν–‰ 컬럼의 $S_i$ 값이기 λ•Œλ¬Έμ— 행을 λ‚˜νƒ€λ‚΄λŠ” i의 값을 row_begin, row_end둜 μ œν•œν•œλ‹€.
κ·Έ ν›„ λ¬Έμ œμ—μ„œ μ„€λͺ…ν•œ λŒ€λ‘œ $S_i$의 값을 i번째 ν–‰ νŠœν”Œ 값듀을 μ΄μš©ν•΄ κ΅¬ν•œλ‹€.

  • row_begin, row_end λ˜ν•œ 1λΆ€ν„° μ‹œμž‘μ΄κΈ° λ•Œλ¬Έμ— 이에 영ν–₯을 받은 i λ˜ν•œ 1λΆ€ν„° μ‹œμž‘ν•˜κ²Œ λœλ‹€.
  • λ”°λΌμ„œ μ‹€μ œ μΈλ±μŠ€λŠ” i - 1이닀.

ν•΄μ‹œκ°’μ€ μ΄λ ‡κ²Œ κ΅¬ν•œ $S_i$ 값듀을 λˆ„μ  XORν•œ κ²ƒμœΌλ‘œ μ •μ˜ν–ˆμœΌλ―€λ‘œ HashVal에 κ³„μ†ν•΄μ„œ ^= ν•΄μ€€λ‹€.

μ΄λ ‡κ²Œ κ΅¬ν•œ HashVal 값을 λ°˜ν™˜ν•΄μ£Όλ©΄ 완성이닀.

전체 μ½”λ“œ

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int solution(vector<vector<int>> Data, int Col, int RowBegin, int RowEnd) 
{
    sort(Data.begin(), Data.end(), [&](const auto& Lhs, const auto& Rhs)
         {
             return Lhs[Col - 1] == Rhs[Col - 1] ? Lhs[0] > Rhs[0] : Lhs[Col - 1] < Rhs[Col - 1];
         });
    
    int HashVal = 0;
    for(int i = RowBegin; i <= RowEnd; ++i)
    {
        int S_i = 0;
        for(const int Val : Data[i - 1])
        {
            S_i += Val % i;
        }
        HashVal ^= S_i;
    }
    
    return HashVal;
}

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

음 ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ 레벨 2 μ•ˆ ν‘Ό λ¬Έμ œλ“€ ν’€κ³  μžˆλŠ”λ°, ν™•μ‹€νžˆ λ‚œμ΄λ„ 차이가 μ’€ μ‹¬ν•˜λ„€μš”.
μ›”μš”μΌμ— ν‘Ό 것도 레벨 2인데...

Copy link
Collaborator

@mjj111 mjj111 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

문제λ₯Ό μ΄ν•΄ν•˜μ§€ λͺ»ν–ˆλ‹€κ°€ μ˜ˆμ‹œ μ„€λͺ… ν•œμ°Έμ„ 보고...
μ΄ν•΄ν•˜λ‹ˆ ν—ˆνƒˆν•˜λ„€μš” γ…‹γ…‹γ…‹γ…‹γ…‹ γ… 

def solution(Data, Col, RowBegin, RowEnd):
    # Col번째 값이 κ°™μœΌλ©΄ 첫 번째 κ°’μœΌλ‘œ λ‚΄λ¦Όμ°¨μˆœ, μ•„λ‹ˆλ©΄ Col번째 κ°’μœΌλ‘œ μ˜€λ¦„μ°¨μˆœ
    Data.sort(key=lambda x: (x[Col - 1], -x[0]))
    answer = 0
    
    for i in range(RowBegin, RowEnd + 1):
        tmp_sum = 0
        for val in Data[i-1] :
            tmp_sum += val % i
            
        answer ^= tmp_sum 
    
    return answer

@9kyo-hwang 9kyo-hwang merged commit 9208996 into main Sep 19, 2024
4 checks passed
@9kyo-hwang 9kyo-hwang deleted the 61-9kyo-hwang branch September 19, 2024 05:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants