Daily/Code Fest

LeetCode 2020 July Challenge | LeetCode 2020 July Challenge

지난 회고에 알고리즘 공부좀 해야겠다고 적었다.이전에 5월 챌린지, 6월 챌린지도 조금 했었는데 두번 다 그냥 대충 내가 아는문제만 풀다가(dfs, tree 문제들 푸는걸 귀찮아 해서 그런 문제가 나오면 넘겼다.) 끝까지 완주하지 않고 안했었다.회고에 적었던 알고리즘 공부를 사실 안 지키고 있었는데ㅋㅋ코딩테스트 준비를 하려고 알고리즘 공부를 한 2일정도 했는데, 묘하게 재밌다 싶어서 시작하게 되었다.https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/ Explore - LeetCodeLeetCode Explore is the best place for everyone to start practicing and learning on ..

LeetCode 2020 July Challenge | LeetCode 2020 July Challenge

728x90

지난 회고에 알고리즘 공부좀 해야겠다고 적었다.

이전에 5월 챌린지, 6월 챌린지도 조금 했었는데 두번 다 그냥 대충 내가 아는문제만 풀다가(dfs, tree 문제들 푸는걸 귀찮아 해서 그런 문제가 나오면 넘겼다.) 끝까지 완주하지 않고 안했었다.

회고에 적었던 알고리즘 공부를 사실 안 지키고 있었는데ㅋㅋ
코딩테스트 준비를 하려고 알고리즘 공부를 한 2일정도 했는데, 묘하게 재밌다 싶어서 시작하게 되었다.

https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/

 

Explore - LeetCode

LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore.

leetcode.com

리트코드에서 매일매일 24시간안에 한 문제를 풀어야하는 과제가 내려오는 형식인데 
한국 시간 기준으로는 오후 4시에 새로운 문제가 업로드 된다.

이 챌린지를 계속 하다보니 일일커밋도 자연스럽게 하게 되었다 (지금 깃캣도 키우고있음..ㅎㅎ) 
7월 레포지토리 기준 33 커밋이다.

매일매일 문제를 풀고 싶었으나 그럼에도 머리가 안돌아가서 못푼문제, 사람들의 discussion을 봐도 이해가 안되는 문제들은 가볍게 패스했다. 풀다보면 나중에 봤을때 이해가 되는 순간이 있지 않을까 싶어서 그랬다.

7월 챌린지는 84 프로로, 27 / 31 ( solve / total ) 달성 했다.

생각보다 하나씩 해결하는게 재밌어서 8월에도 할 듯?

In my last retrospective, I wrote that I should study algorithms.

I had done a bit of the May Challenge and June Challenge before, but both times I just half-heartedly solved only the problems I already knew how to do (I was too lazy to solve DFS and tree problems, so I'd skip them whenever they came up) and never finished either one.

I wasn't actually keeping up with the algorithm study I mentioned in my retrospective lol
But I spent about 2 days studying algorithms to prepare for a coding test, and it was oddly fun, so I decided to jump in.

https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/

 

Explore - LeetCode

LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore.

leetcode.com

The format is that LeetCode gives you one problem each day that you have to solve within 24 hours. 
Based on Korean time, new problems are uploaded at 4 PM.

Keeping up with this challenge naturally led me to make daily commits too (I'm even raising a GitCat right now lol). 
Based on the July repository, I've made 33 commits.

I wanted to solve a problem every single day, but there were still some problems I couldn't solve because my brain just wouldn't cooperate, and some that I couldn't understand even after reading people's discussions — I just lightly skipped those. I figured that if I keep at it, there'll come a time when I look back and finally get them.

I finished the July Challenge at 84 percent, achieving 27 / 31 ( solve / total ).

It was more fun than I expected to knock them out one by one, so I'll probably keep going in August too!

댓글

Comments

Algorithm

Leet code submit 주의점 - 전역변수 초기화 | Leet Code Submit Caution - Global Variable Initialization

https://leetcode.com/problems/add-two-numbers/ Add Two Numbers - LeetCodeLevel up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.leetcode.com이문제 풀다가 간단한 문제고, 문제 풀이도 잘 했는데 뭐가 문제지 했었다. 결론부터 말하자면 LeetCode에서 코드를 제출할 때 static 변수를 사용하지 말자. 사용하더라도 한번의 코드가 끝나면 static 변수를 초기화해주자public class Solution{ static int carry = 0..

Leet code submit 주의점 - 전역변수 초기화 | Leet Code Submit Caution - Global Variable Initialization

728x90

https://leetcode.com/problems/add-two-numbers/

 

Add Two Numbers - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

이문제 풀다가 간단한 문제고, 문제 풀이도 잘 했는데 뭐가 문제지 했었다. 

결론부터 말하자면 LeetCode에서 코드를 제출할 때 static 변수를 사용하지 말자. 사용하더라도 한번의 코드가 끝나면 static 변수를 초기화해주자

public class Solution{
	static int carry = 0;
    
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    	// carry에 따라 결과값 변화
    }
}

 

대충 요약하면 이런 상황이었는데,

이상하게 testcase에 해당 wrong answer이 나는 걸 그대로 테스트하면 accept 되는데, 실제로 submit을 해서 넣어 돌리면 wrong answer이 떳다. 이상하게 내 output에 이 올바르지 않은 값이었다.

그래서 엥,, 이상하다 하고 leet code 측에 메일을 보냈었는데, 문뜩 생각이 들어서 확인을 해보니, test case를 1000개를 실행해 주었고, 내가 전역변수로 설정한 carry값이 다음 test case 실행에 영향을 미췄더라면? 이라는 가설을 내고 확인해보았더니 맞았다.

리트코드에서 전역변수를 사용할 때는 꼭 전역변수를 초기화해주자. 초기화하지 않은 전역변수 값이 submition시 test case에 영향을 줄 수 있다.

알고리즘을 역시 안풀어보니까 이런거에도 끙끙 대는구나 싶다... 다시 리트코드 풀러가야지 총총

https://leetcode.com/problems/add-two-numbers/

 

Add Two Numbers - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

I was solving this problem and it was a simple one — I thought my solution was correct, so I couldn't figure out what was going wrong. 

Long story short: when submitting code on LeetCode, don't use static variables. If you do use them, make sure to reset the static variables after each run.

public class Solution{
	static int carry = 0;
    
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    	// carry에 따라 결과값 변화
    }
}

 

To summarize, here's what was going on:

Strangely, when I tested the exact wrong answer test case myself, it would pass just fine. But when I actually submitted the code, it came back as a wrong answer. Somehow my output had this incorrect value.

So I was like, huh, that's weird, and I even sent an email to the LeetCode team. But then it suddenly hit me — I checked and found out that they run around 1,000 test cases, and I hypothesized: what if the carry value I declared as a global variable was carrying over and affecting the next test case? I tested that theory, and sure enough, that was exactly the problem.

When using global variables on LeetCode, always make sure to reset them. Uninitialized global variable values can affect other test cases during submission.

I guess this is what happens when you don't practice algorithms regularly — you end up struggling with stuff like this... Time to get back to grinding LeetCode problems. Off I go~

댓글

Comments