[C++] ํ•ด์‹œ(Hash)

2020. 4. 29. 18:34ยท๐Ÿ“ Computer Science/โœ Algorithm

ํ•ด์‹œ(Hash)

์ •๋ ฌ์ด ํ•„์š” ์—†๊ณ  ๋น ๋ฅธ ๊ฒ€์ƒ‰์„ ์›ํ•  ๋•Œ ํ•ด์‹œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

๋ฌธ์ œ

 

https://programmers.co.kr/learn/courses/30/lessons/42576

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์™„์ฃผํ•˜์ง€ ๋ชปํ•œ ์„ ์ˆ˜

์ˆ˜๋งŽ์€ ๋งˆ๋ผํ†ค ์„ ์ˆ˜๋“ค์ด ๋งˆ๋ผํ†ค์— ์ฐธ์—ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋‹จ ํ•œ ๋ช…์˜ ์„ ์ˆ˜๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” ๋ชจ๋“  ์„ ์ˆ˜๊ฐ€ ๋งˆ๋ผํ†ค์„ ์™„์ฃผํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋งˆ๋ผํ†ค์— ์ฐธ์—ฌํ•œ ์„ ์ˆ˜๋“ค์˜ ์ด๋ฆ„์ด ๋‹ด๊ธด ๋ฐฐ์—ด participant์™€ ์™„์ฃผํ•œ ์„ ์ˆ˜๏ฟฝ๏ฟฝ

programmers.co.kr

#include <vector>
#include <string>
#include <unordered_map> // ํ•ด์‹œ๋งต
using namespace std;

string solution(vector<string> participant, vector<string> completion){
	string answer = "";
	unordered_map<string, int> temp;
    
	for (string name : participant)
		temp[name]++;
	for (string name : completion)
		temp[name]--;
	for (auto pair : temp)
		if (pair.second > 0){
			answer = pair.first;
			break;
		}
	return answer;
}

 

https://leetcode.com/problems/longest-substring-without-repeating-characters/

 

Longest Substring Without Repeating Characters - 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

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        unordered_map<char, int> hash;
        int answer = 0;

        for (int startIndex = 0, endIndex = 0; endIndex < s.size(); ++endIndex) {
            char c = s[endIndex];

            if (hash[c] != 0)
                startIndex = max(startIndex, hash[c]);

            answer = max(answer, endIndex - startIndex + 1);
            hash[c] = endIndex + 1;
        }

        return answer;
    }
};

 

https://leetcode.com/problems/valid-sudoku/

 

Valid Sudoku - 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

#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;

class Solution {
public:
    bool isValidSudokuLine(vector<char>& board) 
    {
        unordered_set<char> check;

        for (char value : board) {
            if (value == '.')
                continue;

            if (check.find(value) != check.end()) 
                return false;
            check.insert(value);
        }

        return true;
    }

    bool isValidSudoku(vector<vector<char>>& board)
    {
        vector<char> boardLine1, boardLine2;

        for (int i = 0; i < 9; i++) {
            boardLine1.clear();
            boardLine2.clear();

            for (int j = 0; j < 9; j++) {
                boardLine1.push_back(board[i][j]);
                boardLine2.push_back(board[j][i]);
            }

            if (isValidSudokuLine(boardLine1) == false || isValidSudokuLine(boardLine2) == false)
                return false;
        }

        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++) {
                boardLine1.clear();

                for (int k = 0; k < 3; k++)
                    for (int h = 0; h < 3; h++)
                        boardLine1.push_back(board[i * 3 + k][j * 3 + h]);

                if (isValidSudokuLine(boardLine1) == false)
                    return false;
            }

        return true;
    }
};

 

https://leetcode.com/problems/max-points-on-a-line/

class Solution
{
public:
	int maxPoints(vector<vector<int>>& points)
	{
		if (points.empty()) return 0;

		unordered_map <long long, int > map;

		int ans = 1;
		for(int i=0;i<points.size();++i)
		{
			for(int j=i+1;j<points.size();++j)
			{
				int x1 = points[i][0]; int y1 = points[i][1];
				int x2 = points[j][0]; int y2 = points[j][1];

				int dx = x2 - x1;
				int dy = y2 - y1;

				map[getLineKey(dx, dy)]++;
			}

			for(const auto cnt : map)
			{
				ans = max(cnt.second + 1, ans);
			}
			map.clear();
		}
		return ans;
	}
private :
	static long long getLineKey(int dx, int dy)
	{
		const int gcd = get_gcd(dx, dy);
		long long key = dx / gcd;
		key = key << 32;

		return  key + dy / gcd;
	}

	static int get_gcd(int a, int b) {
		return b == 0 ? a : get_gcd(b, a%b);
	}
};
์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)
'๐Ÿ“ Computer Science/โœ Algorithm' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [C++] ๋ฐฑ ํŠธ๋ž˜ํ‚น(Back Tracking)
  • [C++] ๋ธŒ๋ฃจํŠธ ํฌ์Šค(Brute Force)
  • [C++] ์ •๋ ฌ(Sort)
  • [C++] ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ ํ•˜๋…ธ์ด ํƒ‘
Blxxming
Blxxming
CS ์ง€์‹๊ณผ ๊ณต๋ถ€ํ•˜๋‹ค ๋ฐฐ์šด ๊ฒƒ, ๊ฒฝํ—˜ํ•œ ๊ฒƒ ๋“ฑ์„ ๊ธฐ๋กํ•˜๋Š” ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
  • Blxxming
    ๐Ÿ’ก๋ฒˆ๋œฉ๐Ÿ’ก
    Blxxming
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
  • ๊ณต์ง€์‚ฌํ•ญ

    • Tech Interview
    • ๐Ÿ“š Tech (246)
      • ๐Ÿ“ Computer Science (96)
        • โœ OS (12)
        • โœ Network & Web (10)
        • โœ Database (11)
        • โœ Data Structure (6)
        • โœ Algorithm (40)
        • โœ Design Pattern (9)
        • โœ Cloud Computing (3)
        • โœ (5)
      • ๐Ÿ“ Language (73)
        • โœ Language (6)
        • โœ C & C++ (11)
        • โœ C# (19)
        • โœ JAVA (37)
      • ๐Ÿ“ Game (43)
        • โœ Computer Graphics (2)
        • โœ Unity (14)
        • โœ Unreal (26)
        • โœ (1)
      • ๐Ÿ“ Book (34)
        • โœ Effective (3)
        • โœ Game Server (16)
        • โœ Clean Code (14)
        • โœ (1)
  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.0
Blxxming
[C++] ํ•ด์‹œ(Hash)
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”