Notice
Recent Posts
Recent Comments
Today
Total
01-10 18:14
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[LEETCODE] 202. Happy Number ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[LEETCODE] 202. Happy Number

WonderJay 2022. 10. 12. 16:01
728x90
๋ฐ˜์‘ํ˜•
SMALL

https://leetcode.com/

TITLE : 202.Happy Number

Description : Write an algorithm to determine if a numbernis happy.

A happy number is a number defined by the following process:

  • Starting with any positive integer, replace the number by the sum of the squares of its digits.
  • Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
  • Those numbers for which this process ends in 1 are happy.

Return true if n is a happy number, and false if not.

 

Example 1:

Input: n = 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1

Example 2:

Input: n = 2
Output: false

 

Constraints:

  • 1 <= n <= 2^31 - 1

์ •์ˆ˜ N ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ๊ฐ์˜ ์ž๋ฆฟ์ˆ˜์— ๋Œ€ํ•œ ์ œ๊ณฑ์˜ ํ•ฉ์œผ๋กœ N ์„ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•ด์„œ ์ˆ˜ํ–‰ํ•  ๋•Œ N ์ด ๊ฒฐ๊ตญ์— 1์ด ๋  ์ˆ˜ ์žˆ๋‹ค๋ฉด Happy number ๋ผ๊ณ  ์ •์˜ํ•œ๋‹ค. ๋ชจ๋“  ์ž…๋ ฅ๋˜๋Š” ์ˆซ์ž๊ฐ€ happy number ์ด๋ผ๋ฉด ์‰ฝ๊ฒŒ ํ’€์ด๊ฐ€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ๊ทธ๋ ‡์ง€๋Š” ์•Š๋‹ค๋Š” ๊ฒƒ์ด ๋ฌธ์ œ์ด๋‹ค. happy number ๊ฐ€ ์•„๋‹Œ ์ˆซ์ž๊ฐ€ ๋“ค์–ด์˜ค๋Š” ๊ฒฝ์šฐ ์•„๋ž˜์™€ ๊ฐ™์ด cycle ์„ ์ด๋ฃจ๋ฉฐ ๋ฌดํ•œ ๋ฃจํ”„์— ๋น ์ ธ๋ฒ„๋ฆด ์ˆ˜ ์žˆ๋‹ค.  

๊ทธ๋Ÿฌ๋ฏ€๋กœ happy number ์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” ๊ณผ์ • ์ค‘์— ํ•ด๋‹น number ๊ฐ€ cycle ์„ ์ด๋ฃจ๊ณ  ์žˆ์ง€๋Š” ์•Š์€์ง€ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค. 

  ๋˜ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” ๋ฌดํ•œ๋Œ€๋กœ number ๊ฐ€ ๋ฐœ์‚ฐํ•  ๊ฒƒ์ธ์ง€์ด๋‹ค. ์ƒ๊ฐํ•ด๋ณด๋ฉด ๋“ค์–ด์˜ฌ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ์ˆซ์ž๋Š” 2147483647 ์ด์ง€๋งŒ ๊ฐ ์ž๋ฆฟ์ˆ˜์˜ ์ œ๊ณฑํ•ฉ์€ 260์— ๋ถˆ๊ณผํ•˜๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ ˆ๋Œ€๋กœ ๋ฌดํ•œ๋Œ€๋กœ ๋ฐœ์‚ฐํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ, ์ตœ์ข…์ ์œผ๋กœ ๊ณ ๋ คํ•ด์•ผ ํ•  ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” happy number ์ธ ๊ฒฝ์šฐ(cycle ์„ ์ด๋ฃจ์ง€ ์•Š์Œ)์™€ happy number ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ(cycle ์„ ์ด๋ฃจ๋Š” ๊ฒฝ์šฐ) ์ด๋‹ค. ํ•ต์‹ฌ์€ cycle ์˜ ์œ ๋ฌด ํŒ๋ณ„์ด๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ๋– ์˜ฌ๋ฆด ์ˆ˜ ์žˆ๋Š” ํ’€์ด๋Š” 1. Hash table ์„ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•๊ณผ 2. Floyd's Cycle-Finding algorithm ์ด๋‹ค. Hash table ์„ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•์€ ๊ฐ„๋‹จํ•˜๋‹ค. update ํ•ด์•ผํ•  ์ˆซ์ž๋ฅผ ๊ณ„์† hash table ์— insert ํ•˜๋Š”๋ฐ, ๋งŒ์•ฝ ๋‹ค์Œ์œผ๋กœ update ํ•ด์•ผ ํ•  ์ˆซ์ž๊ฐ€ hash table ์— ์ด๋ฏธ ์กด์žฌํ•œ๋‹ค๋ฉด cycle ์„ ์ด๋ฃจ๋Š” ๊ฒƒ์„ detect ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค. Vector ๊ฐ€ ์•„๋‹Œ Hash table ์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” insert ์™€ put, find ๊ฐ€ O(1) ์— ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

[C++]

class Solution {
private:
    int getNext(int n) {
        int next_num = 0;
        while (n > 0) {
            next_num += (n % 10) * (n % 10);
            n /= 10;
        }
        return next_num;
    }

public:
    bool isHappy(int n) {
        set<int> hmap;
        hmap.insert(n); int next_num = 0;
        while (n != 1) {
            next_num = getNext(n);
            if (hmap.count(next_num)) return false;
            hmap.insert(next_num);
            n = next_num;
        } return true;
    }
};

Time complexity : O(logN)

getNext ํ•จ์ˆ˜๋Š” ์ž๋ฆฟ์ˆ˜๋งŒํผ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๊ธฐ ๋•Œ๋ฌธ์— log(n) 

- isHappy ํ•จ์ˆ˜ ๋‚ด์—์„œ log(n) + loglog(n) + logloglog(n) ... 

 

  ๊ฒฐ๊ตญ ์„œ๋กœ ์„œ๋กœ ์—ฐ๊ฒฐ๋œ Linked List(์ดํ•˜ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ) ๊ตฌ์กฐ์—์„œ cycle ์„ detection ํ•˜๋Š” ๊ฒƒ์ด ํ•ต์‹ฌ์ด๋‹ค. ์œ„์™€ ๊ฐ™์ด Hash Set/Map ์„ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ Floyd's Cycle Detection Algorithm ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๊ฒฐํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ํ† ๋ผ์™€ ๊ฑฐ๋ถ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๊ธฐ์–ตํ•˜๋ฉด ์‰ฌ์šด๋ฐ, graph ์— ํ† ๋ผ์™€ ๊ฑฐ๋ถ์ด๋ฅผ ์ง„์ž…์‹œ์ผœ ๋‹ฌ๋ฆฌ๊ฒŒ ํ–ˆ์„ ๋•Œ ๊ฐ™์€ ๋…ธ๋“œ์— ๋„๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ธฐ๋ฉด cycle ์ด ์žˆ๋Š” ๊ฑฐ๊ณ  ๊ทธ๋ ‡์ง€ ์•Š๊ณ  NULL ์„ ๋งŒ๋‚˜๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ acyclic ํ•˜๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ด ๋ฌธ์ œ์—์„œ๋Š” happy number ์ด๋ ค๋ฉด ์‚ฌ์ดํด์ด ์กด์žฌํ•˜๋ฉด ์•ˆ๋˜๋Š” ๊ฒƒ์ด๋ฉฐ, ์‚ฌ์ดํด์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์กฐ๊ฑด์€ ํ† ๋ผ์™€ ๊ฑฐ๋ถ์ด๊ฐ€ ๊ฐ™์€ ๋…ธ๋“œ์— ์œ„์น˜ํ•˜๋ฉด ์•ˆ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ while ๋ฌธ ๋‚ด์—์„œ slow!=fast ๋ฅผ ์กฐ๊ฑด์œผ๋กœ ๋„ฃ์–ด์ฃผ๋ฉด cycle ์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ updating ์„ ๊ทธ๋งŒ๋‘๊ฒŒ ๋˜๊ณ , ์šฐ๋ฆฌ๋Š” ์ด ๋–„์˜ fast ๊ฐ€ 1์ธ์ง€ ์•„๋‹Œ์ง€์— ๋”ฐ๋ผ true/false ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. 

class Solution {
private:
    int getNext(int n) {
        int next_num = 0;
        while (n > 0) {
            next_num += (n % 10) * (n % 10);
            n /= 10;
        }
        return next_num;
    }

public:
    bool isHappy(int n) {
        int slow = n; int fast = getNext(n);
        while (fast!=1 && slow!=fast) {
            slow = getNext(slow);
            fast = getNext(getNext(fast));
        } return fast==1;
    }
};

 

 

Reference : https://leetcode.com/problems/happy-number/solution/

728x90
๋ฐ˜์‘ํ˜•
LIST
Comments