<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>Scribbling</title>
    <link>https://focalpoint.tistory.com/</link>
    <description>구글 유튜브에서 개발을 하고 있는 주니어 개발자입니다.</description>
    <language>ko</language>
    <pubDate>Mon, 8 Jun 2026 23:52:43 +0900</pubDate>
    <generator>TISTORY</generator>
    <ttl>100</ttl>
    <managingEditor>focalpoint</managingEditor>
    <image>
      <title>Scribbling</title>
      <url>https://tistory1.daumcdn.net/tistory/4147667/attach/0757af988d7040cdb57432df9a8d3c9b</url>
      <link>https://focalpoint.tistory.com</link>
    </image>
    <item>
      <title>[C++] LRU Cache</title>
      <link>https://focalpoint.tistory.com/427</link>
      <description>&lt;p data-ke-size=&quot;size16&quot;&gt;&lt;a href=&quot;https://leetcode.com/problems/lru-cache/description/&quot; target=&quot;_blank&quot; rel=&quot;noopener&amp;nbsp;noreferrer&quot;&gt;https://leetcode.com/problems/lru-cache/description/&lt;/a&gt;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre id=&quot;code_1728102692178&quot; class=&quot;cpp&quot; data-ke-language=&quot;cpp&quot; data-ke-type=&quot;codeblock&quot;&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;list&amp;gt;
#include &amp;lt;map&amp;gt;


using namespace std;


class Node {
public:
    int key;
    int val;

    Node() {}
    Node(int key=-1, int val=-1) : key(key), val(val) {}
    ~Node() {}
};


class LRUCache {
public:
    list&amp;lt;Node&amp;gt; ll;
    map&amp;lt;int,list&amp;lt;Node&amp;gt;::iterator&amp;gt; dict;
    int capacity;
    int cnt;

    LRUCache(int capacity) : capacity(capacity), cnt(0) {}

    int get(int key) {
        if (dict.find(key) != dict.end()) {
            auto it = dict[key];
            Node node(*it);
            ll.erase(it);
            ll.push_back(node);
            dict[key] = prev(ll.end());
            return node.val;
        }
        else {
            return -1;
        }
    }

    void put(int key, int value) {
        if (dict.find(key) != dict.end()) {
            auto it = dict[key];
            Node node = Node(*it);
            node.val = value;
            ll.erase(it);
            ll.push_back(node);
            dict[key] = prev(ll.end());
        }
        else {
            if (cnt == capacity) {
                Node popped = ll.front();
                dict.erase(popped.key);
                ll.pop_front();
                Node node = Node(key, value);
                ll.push_back(node);
                dict[key] = prev(ll.end());
            }
            else {
                Node node = Node(key, value);
                ll.push_back(node);
                dict[key] = prev(ll.end());
                cnt++;
            }
        }
    }
};




int main() {
    LRUCache lru(2);
    lru.put(2, 1);
    lru.put(1, 1);
    lru.get(2);
}&lt;/code&gt;&lt;/pre&gt;</description>
      <category>Computer Science/C++</category>
      <author>focalpoint</author>
      <guid isPermaLink="true">https://focalpoint.tistory.com/427</guid>
      <comments>https://focalpoint.tistory.com/427#entry427comment</comments>
      <pubDate>Sat, 5 Oct 2024 13:31:29 +0900</pubDate>
    </item>
    <item>
      <title>[C++] Interface</title>
      <link>https://focalpoint.tistory.com/426</link>
      <description>&lt;pre id=&quot;code_1727804820825&quot; class=&quot;cpp&quot; data-ke-language=&quot;cpp&quot; data-ke-type=&quot;codeblock&quot;&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;


using namespace std;

class Printable {
	friend ostream&amp;amp; operator&amp;lt;&amp;lt;(ostream&amp;amp; os, const Printable&amp;amp; obj);
public:
	virtual void print(ostream&amp;amp; os) const = 0;
	virtual ~Printable() {}
};

ostream&amp;amp; operator&amp;lt;&amp;lt;(ostream&amp;amp; os, const Printable&amp;amp; obj) {
	obj.print(os);
	return os;
}

class class1 : public Printable {
public:
	virtual void print(ostream&amp;amp; os) const override {
		os &amp;lt;&amp;lt; &quot;class1&quot; &amp;lt;&amp;lt; endl;
	}
};

class class2 : public Printable {
public:
	virtual void print(ostream&amp;amp; os) const override {
		os &amp;lt;&amp;lt; &quot;class2&quot; &amp;lt;&amp;lt; endl;
	}
};

int main() {
	class1 cls1;
	class2 cls2;
	cout &amp;lt;&amp;lt; cls1 &amp;lt;&amp;lt; endl;
	cout &amp;lt;&amp;lt; cls2 &amp;lt;&amp;lt; endl;
}&lt;/code&gt;&lt;/pre&gt;</description>
      <category>Computer Science/C++</category>
      <author>focalpoint</author>
      <guid isPermaLink="true">https://focalpoint.tistory.com/426</guid>
      <comments>https://focalpoint.tistory.com/426#entry426comment</comments>
      <pubDate>Wed, 2 Oct 2024 02:46:56 +0900</pubDate>
    </item>
    <item>
      <title>[C++] Object Oriented Programming</title>
      <link>https://focalpoint.tistory.com/425</link>
      <description>&lt;p data-ke-size=&quot;size16&quot;&gt;&lt;a href=&quot;https://leetcode.com/problems/implement-trie-prefix-tree/description/&quot; target=&quot;_blank&quot; rel=&quot;noopener&amp;nbsp;noreferrer&quot;&gt;https://leetcode.com/problems/implement-trie-prefix-tree/description/&lt;/a&gt;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;Trie.h&lt;/p&gt;
&lt;pre id=&quot;code_1727737755853&quot; class=&quot;cpp&quot; data-ke-language=&quot;cpp&quot; data-ke-type=&quot;codeblock&quot;&gt;&lt;code&gt;#pragma once
#include &amp;lt;string&amp;gt;


class TrieNode
{
private:
	char ch;
	bool isWord;
	TrieNode* children[26] {nullptr};

public:
	TrieNode(char ch, bool isWord=false);
	TrieNode(const TrieNode&amp;amp; node);
	TrieNode(TrieNode&amp;amp;&amp;amp; node);
	TrieNode&amp;amp; operator=(const TrieNode&amp;amp; rhs);
	TrieNode&amp;amp; operator=(TrieNode&amp;amp;&amp;amp; rhs);
	
	char getCh() const;
	bool getIsWord() const;
	void setIsWord(bool isWord);
	TrieNode* getChild(int idx) const;
	void setChild(int idx, TrieNode* node);
	
	~TrieNode();
};


class Trie 
{
private:
	TrieNode* root;
	
public:
	Trie();
	Trie(const Trie&amp;amp; trie);
	Trie(Trie&amp;amp;&amp;amp; trie);
	Trie&amp;amp; operator=(const Trie&amp;amp; rhs);
	Trie&amp;amp; operator=(Trie&amp;amp;&amp;amp; rhs);
	
	TrieNode* getRoot() const;
	void setRoot(TrieNode* node);
	void insert(std::string word);
	bool search(std::string word);
	bool startsWith(std::string prefix);
	
	~Trie();
	
	friend void displayTrie(Trie&amp;amp; trie);
};


void displayTrie(Trie&amp;amp; trie);&lt;/code&gt;&lt;/pre&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;Trie.cpp&lt;/p&gt;
&lt;pre id=&quot;code_1727737773677&quot; class=&quot;cpp&quot; data-ke-language=&quot;cpp&quot; data-ke-type=&quot;codeblock&quot;&gt;&lt;code&gt;#include &amp;lt;string&amp;gt;
#include &amp;lt;vector&amp;gt;
#include &amp;lt;iostream&amp;gt;

#include &quot;Trie.h&quot;


TrieNode::TrieNode(char ch, bool isWord) : ch(ch), isWord(isWord) {}

TrieNode::TrieNode(const TrieNode&amp;amp; node) : ch(node.getCh()), isWord(node.getIsWord()) {
	for (int i = 0; i &amp;lt; 26; i++) {
		if (node.getChild(i) != nullptr) {
			children[i] = new TrieNode(*(node.getChild(i)));
		}	
		else {
			children[i] = nullptr;
		}
	}
}

TrieNode::TrieNode(TrieNode&amp;amp;&amp;amp; node) : ch(node.getCh()), isWord(node.getIsWord()) {
	for (int i = 0; i &amp;lt; 26; i++) {
		children[i] = node.getChild(i);
        node.setChild(i, nullptr);
	}
}

TrieNode&amp;amp; TrieNode::operator=(const TrieNode&amp;amp; rhs) {
	if(this == &amp;amp;rhs) return *this;
	this-&amp;gt;ch = rhs.getCh();
	this-&amp;gt;isWord = rhs.getIsWord();
	for (int i = 0; i &amp;lt; 26; i++) {
		if(rhs.getChild(i) != nullptr) {
			children[i] = new TrieNode(*rhs.getChild(i));
		} else {
			children[i] = nullptr;
		}
	}
	return *this;
}

TrieNode&amp;amp; TrieNode::operator=(TrieNode&amp;amp;&amp;amp; rhs) {
	if(this == &amp;amp;rhs) return *this;
	this-&amp;gt;ch = rhs.getCh();
	this-&amp;gt;isWord = rhs.getIsWord();
	for (int i = 0; i &amp;lt; 26; i++) {
		children[i] = rhs.getChild(i);
        rhs.setChild(i, nullptr);
	}
	return *this;
}

char TrieNode::getCh() const {
	return ch;
}

bool TrieNode::getIsWord() const {
	return isWord;
}

void TrieNode::setIsWord(bool isWord) {
	this-&amp;gt;isWord = isWord;
}

TrieNode* TrieNode::getChild(int idx) const {
	return children[idx];
}

void TrieNode::setChild(int idx, TrieNode* node) {
	children[idx] = node;
}

TrieNode::~TrieNode() {
	for (int i = 0; i &amp;lt; 26; i++) {
		if (children[i] != nullptr) {
			delete children[i];
		}
	}
}

Trie::Trie() {
	root = new TrieNode('R');
}

Trie::Trie(const Trie&amp;amp; trie) {
	root = new TrieNode(*trie.getRoot());
}

Trie::Trie(Trie&amp;amp;&amp;amp; trie) {
	root = trie.getRoot();
	trie.setRoot(nullptr);
}

Trie&amp;amp; Trie::operator=(const Trie&amp;amp; rhs) {
	if (this == &amp;amp;rhs) return *this;
	root = new TrieNode(*rhs.getRoot());
	return *this;
}

Trie&amp;amp; Trie::operator=(Trie&amp;amp;&amp;amp; rhs) {
	if (this == &amp;amp;rhs) return *this;
	root = rhs.getRoot();
	rhs.setRoot(nullptr);
	return *this;
}

TrieNode* Trie::getRoot() const {
	return root;
}

void Trie::setRoot(TrieNode* node) {
	root = node;
}

void Trie::insert(std::string word) {
	TrieNode* cur = getRoot();
	for (char c : word) {
		int idx = c - 'a';
		if (cur-&amp;gt;getChild(idx) == nullptr) {
			TrieNode* node = new TrieNode(c);
			cur-&amp;gt;setChild(idx, node);
		}
		cur = cur-&amp;gt;getChild(idx);
	}
	cur-&amp;gt;setIsWord(true);
}

bool Trie::search(std::string word) {
	TrieNode* cur = getRoot();
	for (char c : word) {
		int idx = c - 'a';
		if (cur-&amp;gt;getChild(idx) == nullptr) {
			return false;
		}
		cur = cur-&amp;gt;getChild(idx);
	}
	return cur-&amp;gt;getIsWord();
}

bool Trie::startsWith(std::string prefix) {
	TrieNode* cur = getRoot();
	for (char c : prefix) {
		int idx = c - 'a';
		if (cur-&amp;gt;getChild(idx) == nullptr) {
			return false;
		}
		cur = cur-&amp;gt;getChild(idx);
	}
	return true;
}

Trie::~Trie() {
	if (root != nullptr) delete root;
}

void displayTrieHelper(std::vector&amp;lt;std::string&amp;gt;&amp;amp; words, TrieNode&amp;amp; node, std::string word) {
	std::string cur = word + node.getCh();
	if (node.getIsWord() == true) {
		words.push_back(cur);
	}
	for (int i = 0; i &amp;lt; 26; i++) {
		TrieNode* child = node.getChild(i);
		if (child != nullptr) {
			displayTrieHelper(words, *child, cur);
		}
	}
}

void displayTrie(Trie&amp;amp; trie) {
	std::vector&amp;lt;std::string&amp;gt; words;
	TrieNode* root = trie.getRoot();
	for (int i = 0; i &amp;lt; 26; i++) {
		TrieNode* child = root-&amp;gt;getChild(i);
		if (child != nullptr) {
			displayTrieHelper(words, *child, &quot;&quot;);
		}
	}
	for (std::string word : words) {
		std::cout &amp;lt;&amp;lt; word &amp;lt;&amp;lt; std::endl;
	}
}&lt;/code&gt;&lt;/pre&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;main.cpp&lt;/p&gt;
&lt;pre id=&quot;code_1727737784307&quot; class=&quot;cpp&quot; data-ke-language=&quot;cpp&quot; data-ke-type=&quot;codeblock&quot;&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;string&amp;gt;
#include &amp;lt;vector&amp;gt;
#include &quot;Trie.h&quot;


using namespace std;


int main() {
	Trie trie1;
	trie1.insert(&quot;apple&quot;);
	cout &amp;lt;&amp;lt; trie1.search(&quot;apple&quot;) &amp;lt;&amp;lt; endl;   // return True
	cout &amp;lt;&amp;lt; trie1.search(&quot;app&quot;) &amp;lt;&amp;lt; endl;     // return False
	cout &amp;lt;&amp;lt; trie1.startsWith(&quot;app&quot;) &amp;lt;&amp;lt; endl; // return True
	trie1.insert(&quot;app&quot;);
	cout &amp;lt;&amp;lt; trie1.search(&quot;app&quot;) &amp;lt;&amp;lt; endl;   // return True
	
	Trie trie2(trie1);
	cout &amp;lt;&amp;lt; trie2.search(&quot;apple&quot;) &amp;lt;&amp;lt; endl;   // return True
	cout &amp;lt;&amp;lt; trie2.search(&quot;app&quot;) &amp;lt;&amp;lt; endl;     // return True
	cout &amp;lt;&amp;lt; trie2.startsWith(&quot;app&quot;) &amp;lt;&amp;lt; endl; // return True
	cout &amp;lt;&amp;lt; trie2.search(&quot;app&quot;) &amp;lt;&amp;lt; endl;   // return True
	
	vector&amp;lt;Trie&amp;gt; vec;
	vec.push_back(trie2);
	cout &amp;lt;&amp;lt; vec[0].search(&quot;apple&quot;) &amp;lt;&amp;lt; endl;
	
	displayTrie(trie1);
	
	Trie trie3;
	trie3 = trie1;
	displayTrie(trie3);
	
}&lt;/code&gt;&lt;/pre&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;</description>
      <category>Computer Science/C++</category>
      <author>focalpoint</author>
      <guid isPermaLink="true">https://focalpoint.tistory.com/425</guid>
      <comments>https://focalpoint.tistory.com/425#entry425comment</comments>
      <pubDate>Tue, 1 Oct 2024 08:09:51 +0900</pubDate>
    </item>
    <item>
      <title>[C++] Strings</title>
      <link>https://focalpoint.tistory.com/424</link>
      <description>&lt;p data-ke-size=&quot;size16&quot;&gt;&lt;a href=&quot;https://www.hackerrank.com/challenges/attribute-parser/problem?isFullScreen=true&quot; target=&quot;_blank&quot; rel=&quot;noopener&amp;nbsp;noreferrer&quot;&gt;https://www.hackerrank.com/challenges/attribute-parser/problem?isFullScreen=true&lt;/a&gt;&lt;/p&gt;
&lt;figure id=&quot;og_1727665129184&quot; contenteditable=&quot;false&quot; data-ke-type=&quot;opengraph&quot; data-ke-align=&quot;alignCenter&quot; data-og-type=&quot;website&quot; data-og-title=&quot;Attribute Parser | HackerRank&quot; data-og-description=&quot;Parse the values within various tags.&quot; data-og-host=&quot;www.hackerrank.com&quot; data-og-source-url=&quot;https://www.hackerrank.com/challenges/attribute-parser/problem?isFullScreen=true&quot; data-og-url=&quot;https://www.hackerrank.com/challenges/attribute-parser/problem&quot; data-og-image=&quot;https://scrap.kakaocdn.net/dn/cjatjp/hyXaGpcMvL/nVVP7p6eLBFqA02Y2nS500/img.jpg?width=1200&amp;amp;height=640&amp;amp;face=706_91_833_229&quot;&gt;&lt;a href=&quot;https://www.hackerrank.com/challenges/attribute-parser/problem?isFullScreen=true&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot; data-source-url=&quot;https://www.hackerrank.com/challenges/attribute-parser/problem?isFullScreen=true&quot;&gt;
&lt;div class=&quot;og-image&quot; style=&quot;background-image: url('https://scrap.kakaocdn.net/dn/cjatjp/hyXaGpcMvL/nVVP7p6eLBFqA02Y2nS500/img.jpg?width=1200&amp;amp;height=640&amp;amp;face=706_91_833_229');&quot;&gt;&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;og-text&quot;&gt;
&lt;p class=&quot;og-title&quot; data-ke-size=&quot;size16&quot;&gt;Attribute Parser | HackerRank&lt;/p&gt;
&lt;p class=&quot;og-desc&quot; data-ke-size=&quot;size16&quot;&gt;Parse the values within various tags.&lt;/p&gt;
&lt;p class=&quot;og-host&quot; data-ke-size=&quot;size16&quot;&gt;www.hackerrank.com&lt;/p&gt;
&lt;/div&gt;
&lt;/a&gt;&lt;/figure&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre id=&quot;code_1727665125722&quot; class=&quot;cpp&quot; data-ke-language=&quot;cpp&quot; data-ke-type=&quot;codeblock&quot;&gt;&lt;code&gt;#include &amp;lt;cmath&amp;gt;
#include &amp;lt;cstdio&amp;gt;
#include &amp;lt;vector&amp;gt;
#include &amp;lt;iostream&amp;gt;
#include &amp;lt;algorithm&amp;gt;
#include &amp;lt;map&amp;gt;
#include &amp;lt;sstream&amp;gt;

using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int n, q;
    cin &amp;gt;&amp;gt; n &amp;gt;&amp;gt; q;
    cin.ignore();
    
    // tag1.tag2~attr : val
    map&amp;lt;string, string&amp;gt; parsed;
    string tags, attr;
    // parse n lines of hrml
    for (int i=0; i&amp;lt;n; i++) {
        string line;
        getline(cin, line);
        line = line.substr(1, line.size() - 2);
        // opening tag
        if (line[0] != '/') {
            stringstream ss(line);
            string tag;
            getline(ss, tag, ' ');
            if (tags.empty()) {
                tags = tag;
            }
            else {
                tags += &quot;.&quot; + tag;
            }
            string word;
            while (getline(ss, word, ' ')) {
                if (word[0] == '=') {
                    continue;
                }
                // value
                else if (word[0] == '&quot;') {
                    parsed[tags + &quot;~&quot; + attr] = word.substr(1, word.size()-2);
                }
                // attr
                else {
                    attr = word;
                }
            }            
        }
        // closing tag
        else {
            line = line.substr(1, line.size() - 1);
            string tag(line);
            int idx = tags.find(&quot;.&quot; + tag);
            if (idx != string::npos) {
                tags = tags.substr(0, idx);
            }
            else {
                tags = &quot;&quot;;
            }
        }
    }
    
    // for (auto e : parsed) {
    //     cout &amp;lt;&amp;lt; e.first &amp;lt;&amp;lt; endl;
    //     cout &amp;lt;&amp;lt; e.second &amp;lt;&amp;lt; endl;
    // }
    
    // queries
    for (int i=0; i&amp;lt;q; i++) {
        string line;
        getline(cin, line);
        if (parsed.find(line) == parsed.end()) {
            cout &amp;lt;&amp;lt; &quot;Not Found!&quot; &amp;lt;&amp;lt; endl;
        }
        else {
            cout &amp;lt;&amp;lt; parsed[line] &amp;lt;&amp;lt; endl;
        }
    }
    
}&lt;/code&gt;&lt;/pre&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;</description>
      <category>Computer Science/C++</category>
      <author>focalpoint</author>
      <guid isPermaLink="true">https://focalpoint.tistory.com/424</guid>
      <comments>https://focalpoint.tistory.com/424#entry424comment</comments>
      <pubDate>Mon, 30 Sep 2024 11:59:10 +0900</pubDate>
    </item>
    <item>
      <title>[C++] Abstract Class, Polymorphism</title>
      <link>https://focalpoint.tistory.com/423</link>
      <description>&lt;p data-ke-size=&quot;size16&quot;&gt;&lt;a href=&quot;https://www.hackerrank.com/challenges/abstract-classes-polymorphism/problem?isFullScreen=true&quot; target=&quot;_blank&quot; rel=&quot;noopener&amp;nbsp;noreferrer&quot;&gt;https://www.hackerrank.com/challenges/abstract-classes-polymorphism/problem?isFullScreen=true&lt;/a&gt;&lt;/p&gt;
&lt;figure id=&quot;og_1727484509067&quot; contenteditable=&quot;false&quot; data-ke-type=&quot;opengraph&quot; data-ke-align=&quot;alignCenter&quot; data-og-type=&quot;website&quot; data-og-title=&quot;Abstract Classes - Polymorphism | HackerRank&quot; data-og-description=&quot;Given an abstract class Cache, write a class LRUCache which extends the class Cache and implement an LRU cache.&quot; data-og-host=&quot;www.hackerrank.com&quot; data-og-source-url=&quot;https://www.hackerrank.com/challenges/abstract-classes-polymorphism/problem?isFullScreen=true&quot; data-og-url=&quot;https://www.hackerrank.com/challenges/abstract-classes-polymorphism/problem&quot; data-og-image=&quot;https://scrap.kakaocdn.net/dn/33fyw/hyW6GcTvXC/krXYUVJgc437c87F4NuDK1/img.jpg?width=1200&amp;amp;height=640&amp;amp;face=706_91_833_229&quot;&gt;&lt;a href=&quot;https://www.hackerrank.com/challenges/abstract-classes-polymorphism/problem?isFullScreen=true&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot; data-source-url=&quot;https://www.hackerrank.com/challenges/abstract-classes-polymorphism/problem?isFullScreen=true&quot;&gt;
&lt;div class=&quot;og-image&quot; style=&quot;background-image: url('https://scrap.kakaocdn.net/dn/33fyw/hyW6GcTvXC/krXYUVJgc437c87F4NuDK1/img.jpg?width=1200&amp;amp;height=640&amp;amp;face=706_91_833_229');&quot;&gt;&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;og-text&quot;&gt;
&lt;p class=&quot;og-title&quot; data-ke-size=&quot;size16&quot;&gt;Abstract Classes - Polymorphism | HackerRank&lt;/p&gt;
&lt;p class=&quot;og-desc&quot; data-ke-size=&quot;size16&quot;&gt;Given an abstract class Cache, write a class LRUCache which extends the class Cache and implement an LRU cache.&lt;/p&gt;
&lt;p class=&quot;og-host&quot; data-ke-size=&quot;size16&quot;&gt;www.hackerrank.com&lt;/p&gt;
&lt;/div&gt;
&lt;/a&gt;&lt;/figure&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre id=&quot;code_1727484507239&quot; class=&quot;cpp&quot; data-ke-language=&quot;cpp&quot; data-ke-type=&quot;codeblock&quot;&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;vector&amp;gt;
#include &amp;lt;map&amp;gt;
#include &amp;lt;string&amp;gt;
#include &amp;lt;algorithm&amp;gt;
#include &amp;lt;set&amp;gt;
#include &amp;lt;cassert&amp;gt;
using namespace std;

struct Node{
   Node* next;
   Node* prev;
   int value;
   int key;
   Node(Node* p, Node* n, int k, int val):prev(p),next(n),key(k),value(val){};
   Node(int k, int val):prev(NULL),next(NULL),key(k),value(val){};
};

class Cache{
   
   protected: 
   map&amp;lt;int,Node*&amp;gt; mp; //map the key to the node in the linked list
   int cp;  //capacity
   Node* tail; // double linked list tail pointer
   Node* head; // double linked list head pointer
   virtual void set(int, int) = 0; //set function
   virtual int get(int) = 0; //get function

};

class LinkedList {
private:
    int cnt;
    Node* head;
    Node* tail;
    
public:
    LinkedList() : cnt(0), head(nullptr), tail(nullptr) {
        head = new Node(-1, -1);
        tail = new Node(-1, -1);
        head-&amp;gt;next = tail;
        tail-&amp;gt;prev = head;
    }
    
    void insertNodeToTail(Node* node) {
        Node* prev = tail-&amp;gt;prev;
        prev-&amp;gt;next = node;
        node-&amp;gt;prev = prev;
        node-&amp;gt;next = tail;
        tail-&amp;gt;prev = node;
        cnt++;
    }
    
    int popHead() {
        Node* node = head-&amp;gt;next;
        Node* next = node-&amp;gt;next;
        next-&amp;gt;prev = head;
        head-&amp;gt;next = next;
        int key = node-&amp;gt;key;
        delete node;
        cnt--;
        return key;
    }
    
    void removeNode(Node* node) {
        Node* prev = node-&amp;gt;prev;
        Node* next = node-&amp;gt;next;
        prev-&amp;gt;next = next;
        next-&amp;gt;prev = prev;
        delete node;
        cnt--;
    }
    
    int getCnt() {
        return cnt;
    }
    
    Node* getHead() {
        return head;
    }
    
    Node* getTail() {
        return tail;
    }
    
    ~LinkedList() {
        if (head != nullptr) delete head;
        if (tail != nullptr) delete tail;
    }
};

class LRUCache : public Cache {
protected:
    int cnt;
    LinkedList ll;
public:
    LRUCache(int capacity): Cache(), cnt(0) {
        this-&amp;gt;cp = capacity;
        tail = ll.getTail();
        head = ll.getHead();
    }
    
    void set(int key, int val) {
        if (mp.find(key) != mp.end()) {
            Node* node = mp[key];
            mp.erase(key);
            Node* newNode = new Node(key, val);
            mp[key] = newNode;
            ll.removeNode(node);
            ll.insertNodeToTail(newNode);
        } 
        else {
            // at max capacity
            if (cnt == cp) {
                int deletedKey = ll.popHead();
                mp.erase(deletedKey);
                Node* node = new Node(key, val);
                mp[key] = node;
                ll.insertNodeToTail(node);
            }
            else {
                Node* node = new Node(key, val);
                mp[key] = node;
                ll.insertNodeToTail(node);
                cnt++;
            }
        }
    }
    
    int get(int key) {
        // cache miss
        if (mp.find(key) == mp.end()) {
            return -1;
        } 
        // cache hit
        else {
            Node* node = mp[key];
            mp.erase(key);
            int val = node-&amp;gt;value;
            Node* newNode = new Node(key, val);
            mp[key] = newNode;
            ll.removeNode(node);
            ll.insertNodeToTail(newNode);
            return val;
        }
    }
};

int main() {
   int n, capacity,i;
   cin &amp;gt;&amp;gt; n &amp;gt;&amp;gt; capacity;
   LRUCache l(capacity);
   for(i=0;i&amp;lt;n;i++) {
      string command;
      cin &amp;gt;&amp;gt; command;
      if(command == &quot;get&quot;) {
         int key;
         cin &amp;gt;&amp;gt; key;
         cout &amp;lt;&amp;lt; l.get(key) &amp;lt;&amp;lt; endl;
      } 
      else if(command == &quot;set&quot;) {
         int key, value;
         cin &amp;gt;&amp;gt; key &amp;gt;&amp;gt; value;
         l.set(key,value);
      }
   }
   return 0;
}&lt;/code&gt;&lt;/pre&gt;</description>
      <category>Computer Science/C++</category>
      <author>focalpoint</author>
      <guid isPermaLink="true">https://focalpoint.tistory.com/423</guid>
      <comments>https://focalpoint.tistory.com/423#entry423comment</comments>
      <pubDate>Sat, 28 Sep 2024 09:48:34 +0900</pubDate>
    </item>
    <item>
      <title>[C++] Virtual Functions</title>
      <link>https://focalpoint.tistory.com/422</link>
      <description>&lt;p data-ke-size=&quot;size16&quot;&gt;&lt;a href=&quot;https://www.hackerrank.com/challenges/virtual-functions/problem?isFullScreen=true&quot; target=&quot;_blank&quot; rel=&quot;noopener&amp;nbsp;noreferrer&quot;&gt;https://www.hackerrank.com/challenges/virtual-functions/problem?isFullScreen=true&lt;/a&gt;&lt;/p&gt;
&lt;figure id=&quot;og_1727465630960&quot; contenteditable=&quot;false&quot; data-ke-type=&quot;opengraph&quot; data-ke-align=&quot;alignCenter&quot; data-og-type=&quot;website&quot; data-og-title=&quot;Virtual Functions | HackerRank&quot; data-og-description=&quot;Learn how to use virtual functions and solve the given problem.&quot; data-og-host=&quot;www.hackerrank.com&quot; data-og-source-url=&quot;https://www.hackerrank.com/challenges/virtual-functions/problem?isFullScreen=true&quot; data-og-url=&quot;https://www.hackerrank.com/challenges/virtual-functions/problem&quot; data-og-image=&quot;https://scrap.kakaocdn.net/dn/lE6b6/hyW6JHt0TA/whWfGz7KFzwpPe5jchKRk0/img.jpg?width=1200&amp;amp;height=640&amp;amp;face=706_91_833_229&quot;&gt;&lt;a href=&quot;https://www.hackerrank.com/challenges/virtual-functions/problem?isFullScreen=true&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot; data-source-url=&quot;https://www.hackerrank.com/challenges/virtual-functions/problem?isFullScreen=true&quot;&gt;
&lt;div class=&quot;og-image&quot; style=&quot;background-image: url('https://scrap.kakaocdn.net/dn/lE6b6/hyW6JHt0TA/whWfGz7KFzwpPe5jchKRk0/img.jpg?width=1200&amp;amp;height=640&amp;amp;face=706_91_833_229');&quot;&gt;&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;og-text&quot;&gt;
&lt;p class=&quot;og-title&quot; data-ke-size=&quot;size16&quot;&gt;Virtual Functions | HackerRank&lt;/p&gt;
&lt;p class=&quot;og-desc&quot; data-ke-size=&quot;size16&quot;&gt;Learn how to use virtual functions and solve the given problem.&lt;/p&gt;
&lt;p class=&quot;og-host&quot; data-ke-size=&quot;size16&quot;&gt;www.hackerrank.com&lt;/p&gt;
&lt;/div&gt;
&lt;/a&gt;&lt;/figure&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre id=&quot;code_1727465637005&quot; class=&quot;cpp&quot; data-ke-language=&quot;cpp&quot; data-ke-type=&quot;codeblock&quot;&gt;&lt;code&gt;#include &amp;lt;cmath&amp;gt;
#include &amp;lt;cstdio&amp;gt;
#include &amp;lt;vector&amp;gt;
#include &amp;lt;iostream&amp;gt;
#include &amp;lt;algorithm&amp;gt;
using namespace std;


class Person {
public:
    string name;
    int age;
    
    Person() {}
    virtual void getdata() {};
    virtual void putdata() {};
    virtual ~Person() {}
};


class Professor : public Person {
public:
    static int id;
    int cur_id;
    int publications;
    
    Professor() : Person() {
        Professor::id++;
        cur_id = Professor::id;
    }
    virtual void getdata() override {
        string _name;
        int _publications;
        int _age;
        cin &amp;gt;&amp;gt; _name;
        cin &amp;gt;&amp;gt; _publications;
        cin &amp;gt;&amp;gt; _age;
        this-&amp;gt;name = _name;
        this-&amp;gt;publications = _publications;
        this-&amp;gt;age = _age;
    }
    virtual void putdata() override {
        cout &amp;lt;&amp;lt; this-&amp;gt;name &amp;lt;&amp;lt; &quot; &quot;;
        cout &amp;lt;&amp;lt; this-&amp;gt;publications &amp;lt;&amp;lt; &quot; &quot;;
        cout &amp;lt;&amp;lt; this-&amp;gt;age &amp;lt;&amp;lt; &quot; &quot;;
        cout &amp;lt;&amp;lt; this-&amp;gt;cur_id &amp;lt;&amp;lt; endl;
    }
    
};


class Student : public Person {
public:
    static int id;
    int cur_id;
    int marks[6];
    int marks_total {0};
    
    Student() : Person() {
        Student::id++;
        cur_id = Student::id;
    }
    virtual void getdata() override {
        string _name;
        int _age;
        cin &amp;gt;&amp;gt; _name;
        cin &amp;gt;&amp;gt; _age;
        this-&amp;gt;name = _name;
        this-&amp;gt;age = _age;
        for (int i=0; i&amp;lt;6; i++) {
            cin &amp;gt;&amp;gt; marks[i];
            marks_total += marks[i];
        }
    }
    virtual void putdata() override {
        cout &amp;lt;&amp;lt; this-&amp;gt;name &amp;lt;&amp;lt; &quot; &quot;;
        cout &amp;lt;&amp;lt; this-&amp;gt;age &amp;lt;&amp;lt; &quot; &quot;;
        cout &amp;lt;&amp;lt; this-&amp;gt;marks_total &amp;lt;&amp;lt; &quot; &quot;;
        cout &amp;lt;&amp;lt; this-&amp;gt;cur_id &amp;lt;&amp;lt; endl;
    }
};


int Professor::id {0};
int Student::id {0};


int main(){

    int n, val;
    cin&amp;gt;&amp;gt;n; //The number of objects that is going to be created.
    Person *per[n];

    for(int i = 0;i &amp;lt; n;i++){

        cin&amp;gt;&amp;gt;val;
        if(val == 1){
            // If val is 1 current object is of type Professor
            per[i] = new Professor;

        }
        else per[i] = new Student; // Else the current object is of type Student

        per[i]-&amp;gt;getdata(); // Get the data from the user.

    }

    for(int i=0;i&amp;lt;n;i++)
        per[i]-&amp;gt;putdata(); // Print the required output for each object.

    return 0;

}&lt;/code&gt;&lt;/pre&gt;</description>
      <category>Computer Science/C++</category>
      <author>focalpoint</author>
      <guid isPermaLink="true">https://focalpoint.tistory.com/422</guid>
      <comments>https://focalpoint.tistory.com/422#entry422comment</comments>
      <pubDate>Sat, 28 Sep 2024 04:34:04 +0900</pubDate>
    </item>
    <item>
      <title>[C++] Regular Expression Matching</title>
      <link>https://focalpoint.tistory.com/421</link>
      <description>&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;C++ Regular Expressions Library&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&lt;a href=&quot;https://en.cppreference.com/w/cpp/regex&quot; target=&quot;_blank&quot; rel=&quot;noopener&amp;nbsp;noreferrer&quot;&gt;https://en.cppreference.com/w/cpp/regex&lt;/a&gt;&lt;/p&gt;
&lt;figure id=&quot;og_1727303608559&quot; contenteditable=&quot;false&quot; data-ke-type=&quot;opengraph&quot; data-ke-align=&quot;alignCenter&quot; data-og-type=&quot;website&quot; data-og-title=&quot;Regular expressions library (since C++11) - cppreference.com&quot; data-og-description=&quot;Regular expressions library The regular expressions library provides a class that represents regular expressions, which are a kind of mini-language used to perform pattern matching within strings. Almost all operations with regexes can be characterized by &quot; data-og-host=&quot;en.cppreference.com&quot; data-og-source-url=&quot;https://en.cppreference.com/w/cpp/regex&quot; data-og-url=&quot;https://en.cppreference.com/w/cpp/regex&quot; data-og-image=&quot;&quot;&gt;&lt;a href=&quot;https://en.cppreference.com/w/cpp/regex&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot; data-source-url=&quot;https://en.cppreference.com/w/cpp/regex&quot;&gt;
&lt;div class=&quot;og-image&quot; style=&quot;background-image: url();&quot;&gt;&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;og-text&quot;&gt;
&lt;p class=&quot;og-title&quot; data-ke-size=&quot;size16&quot;&gt;Regular expressions library (since C++11) - cppreference.com&lt;/p&gt;
&lt;p class=&quot;og-desc&quot; data-ke-size=&quot;size16&quot;&gt;Regular expressions library The regular expressions library provides a class that represents regular expressions, which are a kind of mini-language used to perform pattern matching within strings. Almost all operations with regexes can be characterized by&lt;/p&gt;
&lt;p class=&quot;og-host&quot; data-ke-size=&quot;size16&quot;&gt;en.cppreference.com&lt;/p&gt;
&lt;/div&gt;
&lt;/a&gt;&lt;/figure&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&lt;a style=&quot;background-color: #f0f0f0; color: #000000; text-align: start;&quot; href=&quot;https://leetcode.com/problems/string-to-integer-atoi/&quot;&gt;8. String to Integer (atoi)&lt;/a&gt;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&lt;a href=&quot;https://leetcode.com/problems/string-to-integer-atoi/description/&quot; target=&quot;_blank&quot; rel=&quot;noopener&amp;nbsp;noreferrer&quot;&gt;https://leetcode.com/problems/string-to-integer-atoi/description/&lt;/a&gt;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre id=&quot;code_1727305564755&quot; class=&quot;cpp&quot; data-ke-language=&quot;cpp&quot; data-ke-type=&quot;codeblock&quot;&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;regex&amp;gt;
#include &amp;lt;stdexcept&amp;gt;
#include &amp;lt;climits&amp;gt;

class Solution {
public:
    int myAtoi(string s) {
        const std::regex pat(&quot;^ *([+-]?)([0-9]+)&quot;);
		std::smatch match;
		std::regex_search(s, match, pat);
		if (match.size() == 0) return 0;
		string sign = match.str(1);
		int parsed;
		try {
			parsed = stoi(match.str(2));
		} catch (std::out_of_range&amp;amp; e) {
			if (!sign.empty() and sign == &quot;-&quot;)
                return INT_MIN;
            return INT_MAX;
		} 
		if (!sign.empty() and sign == &quot;-&quot;) 
			return -parsed;
		return parsed;
    }
};&lt;/code&gt;&lt;/pre&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;</description>
      <category>Computer Science/C++</category>
      <author>focalpoint</author>
      <guid isPermaLink="true">https://focalpoint.tistory.com/421</guid>
      <comments>https://focalpoint.tistory.com/421#entry421comment</comments>
      <pubDate>Thu, 26 Sep 2024 07:34:24 +0900</pubDate>
    </item>
    <item>
      <title>[Programmers] 택배 배달과 수거하기</title>
      <link>https://focalpoint.tistory.com/420</link>
      <description>&lt;p data-ke-size=&quot;size16&quot;&gt;&lt;a href=&quot;https://school.programmers.co.kr/learn/courses/30/lessons/150369&quot; target=&quot;_blank&quot; rel=&quot;noopener&amp;nbsp;noreferrer&quot;&gt;https://school.programmers.co.kr/learn/courses/30/lessons/150369&lt;/a&gt;&lt;/p&gt;
&lt;figure id=&quot;og_1721425707232&quot; contenteditable=&quot;false&quot; data-ke-type=&quot;opengraph&quot; data-ke-align=&quot;alignCenter&quot; data-og-type=&quot;website&quot; data-og-title=&quot;프로그래머스&quot; data-og-description=&quot;코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.&quot; data-og-host=&quot;programmers.co.kr&quot; data-og-source-url=&quot;https://school.programmers.co.kr/learn/courses/30/lessons/150369&quot; data-og-url=&quot;https://programmers.co.kr/&quot; data-og-image=&quot;https://scrap.kakaocdn.net/dn/bfIGvh/hyWztFpin5/wBNZHARD58xbkbfZi6Naw1/img.png?width=1200&amp;amp;height=630&amp;amp;face=0_0_1200_630,https://scrap.kakaocdn.net/dn/fgnz4/hyWCGiNJDz/FStS3aKbNVuVBKi5w76EPK/img.png?width=1200&amp;amp;height=630&amp;amp;face=0_0_1200_630&quot;&gt;&lt;a href=&quot;https://school.programmers.co.kr/learn/courses/30/lessons/150369&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot; data-source-url=&quot;https://school.programmers.co.kr/learn/courses/30/lessons/150369&quot;&gt;
&lt;div class=&quot;og-image&quot; style=&quot;background-image: url('https://scrap.kakaocdn.net/dn/bfIGvh/hyWztFpin5/wBNZHARD58xbkbfZi6Naw1/img.png?width=1200&amp;amp;height=630&amp;amp;face=0_0_1200_630,https://scrap.kakaocdn.net/dn/fgnz4/hyWCGiNJDz/FStS3aKbNVuVBKi5w76EPK/img.png?width=1200&amp;amp;height=630&amp;amp;face=0_0_1200_630');&quot;&gt;&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;og-text&quot;&gt;
&lt;p class=&quot;og-title&quot; data-ke-size=&quot;size16&quot;&gt;프로그래머스&lt;/p&gt;
&lt;p class=&quot;og-desc&quot; data-ke-size=&quot;size16&quot;&gt;코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.&lt;/p&gt;
&lt;p class=&quot;og-host&quot; data-ke-size=&quot;size16&quot;&gt;programmers.co.kr&lt;/p&gt;
&lt;/div&gt;
&lt;/a&gt;&lt;/figure&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;The key is to eliminate the farthest houses.&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;To identify the farthest ones, we may use max heaps.&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;1. Python&lt;/p&gt;
&lt;pre id=&quot;code_1721425817949&quot; class=&quot;python&quot; data-ke-language=&quot;python&quot; data-ke-type=&quot;codeblock&quot;&gt;&lt;code&gt;import heapq
def solution(cap, n, deliveries, pickups):
    h1 = []
    h2 = []
    for i, d in enumerate(deliveries):
        if d &amp;gt; 0:
            heapq.heappush(h1, (-i-1, d))
    for i, p in enumerate(pickups):
        if p &amp;gt; 0:
            heapq.heappush(h2, (-i-1, p))
    ret = 0
    while h1 or h2:
        if not h1:
            ret += 2 * -h2[0][0]
        elif not h2:
            ret += 2 * -h1[0][0]
        else:
            ret += 2 * max(-h1[0][0], -h2[0][0])
        c1 = cap
        while h1 and c1 &amp;gt; 0:
            i, d = heapq.heappop(h1)
            if d &amp;gt; c1:
                heapq.heappush(h1, (i, d - c1))
                c1 = 0
            elif d == c1:
                break
            else:
                c1 -= d
        c2 = cap
        while h2 and c2 &amp;gt; 0:
            i, d = heapq.heappop(h2)
            if d &amp;gt; c2:
                heapq.heappush(h2, (i, d - c2))
                c2 = 0
            elif d == c2:
                break
            else:
                c2 -= d
    return ret&lt;/code&gt;&lt;/pre&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;2. C++&lt;/p&gt;
&lt;pre id=&quot;code_1721428018405&quot; class=&quot;cpp&quot; data-ke-language=&quot;cpp&quot; data-ke-type=&quot;codeblock&quot;&gt;&lt;code&gt;struct comparator {
	bool operator()(pair&amp;lt;int, int&amp;gt; p1, pair&amp;lt;int, int&amp;gt; p2) {
		return p1.first &amp;lt; p2.first;
	}
};


long long solution(int cap, int n, vector&amp;lt;int&amp;gt; deliveries, vector&amp;lt;int&amp;gt; pickups) {
	auto comp = [](pair&amp;lt;int, int&amp;gt; p1, pair&amp;lt;int, int&amp;gt; p2) {
		return p1.first &amp;lt; p2.first;
	};
	//priority_queue &amp;lt;pair&amp;lt;int, int&amp;gt;, vector&amp;lt;pair&amp;lt;int, int&amp;gt;&amp;gt;, decltype(comp)&amp;gt; pq1;
	//priority_queue &amp;lt;pair&amp;lt;int, int&amp;gt;, vector&amp;lt;pair&amp;lt;int, int&amp;gt;&amp;gt;, decltype(comp)&amp;gt; pq2;
	priority_queue &amp;lt;pair&amp;lt;int, int&amp;gt;, vector&amp;lt;pair&amp;lt;int, int&amp;gt;&amp;gt;, comparator&amp;gt; pq1;
	priority_queue &amp;lt;pair&amp;lt;int, int&amp;gt;, vector&amp;lt;pair&amp;lt;int, int&amp;gt;&amp;gt;, comparator&amp;gt; pq2;
	for (int i = 0; i &amp;lt; deliveries.size(); i++) {
		if (deliveries[i] &amp;gt; 0) {
			pq1.push({ i + 1, deliveries[i] });
		}
	}
	for (int i = 0; i &amp;lt; pickups.size(); i++) {
		if (pickups[i] &amp;gt; 0) {
			pq2.push({ i + 1, pickups[i] });
		}
	}
	long long ret = 0;
	while (!pq1.empty() or !pq2.empty()) {
		if (pq1.empty())
			ret += 2 * pq2.top().first;
		else if (pq2.empty())
			ret += 2 * pq1.top().first;
		else
			ret += 2 * max(pq1.top().first, pq2.top().first);
		int c1 = cap, c2 = cap;
		while (!pq1.empty() and c1 &amp;gt; 0) {
			auto[i, d] = pq1.top();
			pq1.pop();
			if (d &amp;gt; c1) {
				pq1.push({ i, d - c1 });
				c1 = 0;
			}
			else if (d == c1) {
				break;
			}
			else {
				c1 -= d;
			}
		}
		while (!pq2.empty() and c2 &amp;gt; 0) {
			auto [i, d] = pq2.top();
			pq2.pop();
			if (d &amp;gt; c2) {
				pq2.push({ i, d - c2 });
				c2 = 0;
			}
			else if (d == c2) {
				break;
			}
			else {
				c2 -= d;
			}
		}
	}
	return ret;
}&lt;/code&gt;&lt;/pre&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;</description>
      <category>Computer Science/Coding Test</category>
      <author>focalpoint</author>
      <guid isPermaLink="true">https://focalpoint.tistory.com/420</guid>
      <comments>https://focalpoint.tistory.com/420#entry420comment</comments>
      <pubDate>Sat, 20 Jul 2024 06:52:37 +0900</pubDate>
    </item>
    <item>
      <title>[LeetCode] 2781. Length of the Longest Valid Substring</title>
      <link>https://focalpoint.tistory.com/419</link>
      <description>&lt;p data-ke-size=&quot;size16&quot;&gt;&lt;a href=&quot;https://leetcode.com/problems/length-of-the-longest-valid-substring/description/&quot; target=&quot;_blank&quot; rel=&quot;noopener&amp;nbsp;noreferrer&quot;&gt;https://leetcode.com/problems/length-of-the-longest-valid-substring/description/&lt;/a&gt;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;The main idea is to use Trie data structure.&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;1. Python&lt;/p&gt;
&lt;pre id=&quot;code_1720541518513&quot; class=&quot;python&quot; data-ke-language=&quot;python&quot; data-ke-type=&quot;codeblock&quot;&gt;&lt;code&gt;class Solution:
    def longestValidSubstring(self, word: str, forbidden: List[str]) -&amp;gt; int:
        ret = 0
        trie = {}
        for f in forbidden:
            f = f[::-1]
            t = trie
            for c in f:
                if c not in t:
                    t[c] = {}
                t = t[c]
            t['#'] = len(f)
        j = 0
        for i in range(len(word)):
            valid = True
            nxtJ = None
            t = trie
            for k in range(i, j - 1, -1):
                c = word[k]
                if c not in t:
                    break
                t = t[c]
                if '#' in t:
                    valid = False
                    nxtJ = i - t['#'] + 2
                    break
            if valid is True:
                ret = max(ret, i - j + 1)
            else:
                j = nxtJ
        return ret&lt;/code&gt;&lt;/pre&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;2. C++&lt;/p&gt;
&lt;pre id=&quot;code_1720714200262&quot; class=&quot;cpp&quot; data-ke-language=&quot;cpp&quot; data-ke-type=&quot;codeblock&quot;&gt;&lt;code&gt;class TrieNode {
	public:
		char ch;
		bool isWord;
		int length;
		TrieNode* children[26] {nullptr};
		
		TrieNode() {}
		
		TrieNode(char ch, bool isWord=false, int length=0): ch(ch), isWord(isWord), length(length) {}
		
		~TrieNode() {}
};


class Trie {
	public:
		TrieNode* root = new TrieNode();
		
		Trie () {}
		
		void insert(string word) {
			TrieNode* cur = root;
			for (char c : word) {
				int idx = c - 'a';
				if (cur-&amp;gt;children[idx] == nullptr) {
					cur-&amp;gt;children[idx] = new TrieNode(c);
				}
				cur = cur-&amp;gt;children[idx];
			}
			cur-&amp;gt;isWord = true;
			cur-&amp;gt;length = word.size();
		}
		
		~Trie() {
			delete root;
		}		
};


class Solution {
public:
    int longestValidSubstring(string word, vector&amp;lt;string&amp;gt;&amp;amp; forbidden) {
        int ret = 0;
		Trie trie = Trie();
		for (string&amp;amp; word : forbidden) {
            reverse(word.begin(), word.end());
			trie.insert(word);
		}
		int j = 0;
		for (int i=0; i&amp;lt;word.size(); i++) {
			bool valid = true;
			int nxtJ = -1;
			TrieNode node = *trie.root;
			for (int k=i; k&amp;gt;j-1; k--) {
				char c = word[k];
				int idx = c - 'a';
				if (node.children[idx] == nullptr) {
					break;
				}
				node = *node.children[idx];
				if (node.isWord) {
					valid = false;
					nxtJ = i - node.length + 2;
					break;
				}
			}
			if (valid) {
				ret = max(ret, i - j + 1);
			} else {
				j = nxtJ;
			}
		}
		return ret;
    }
};&lt;/code&gt;&lt;/pre&gt;</description>
      <category>Computer Science/Algorithms &amp;amp; Data Structures</category>
      <author>focalpoint</author>
      <guid isPermaLink="true">https://focalpoint.tistory.com/419</guid>
      <comments>https://focalpoint.tistory.com/419#entry419comment</comments>
      <pubDate>Wed, 10 Jul 2024 03:03:54 +0900</pubDate>
    </item>
    <item>
      <title>[LeetCode] 2340. Minimum Adjacent Swaps to Make a Valid Array</title>
      <link>https://focalpoint.tistory.com/418</link>
      <description>&lt;p data-ke-size=&quot;size16&quot;&gt;&lt;a href=&quot;https://leetcode.com/problems/minimum-adjacent-swaps-to-make-a-valid-array/description/&quot; target=&quot;_blank&quot; rel=&quot;noopener&amp;nbsp;noreferrer&quot;&gt;https://leetcode.com/problems/minimum-adjacent-swaps-to-make-a-valid-array/description/&lt;/a&gt;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;1. Python&lt;/p&gt;
&lt;pre id=&quot;code_1719904579259&quot; class=&quot;python&quot; data-ke-language=&quot;python&quot; data-ke-type=&quot;codeblock&quot;&gt;&lt;code&gt;class Solution:
    def minimumSwaps(self, nums: List[int]) -&amp;gt; int:
        if len(nums) &amp;lt;= 1:
            return 0

        minVal = min(nums)
        maxVal = max(nums)

        for i in range(len(nums)):
            if nums[i] == minVal:
                minIdx = i
                break

        for i in range(len(nums)-1, -1, -1):
            if nums[i] == maxVal:
                maxIdx = i
                break

        if minIdx == maxIdx:
            return 0
        elif minIdx &amp;lt; maxIdx:
            return minIdx + len(nums) - maxIdx - 1
        else:
            return minIdx + len(nums) - maxIdx - 2&lt;/code&gt;&lt;/pre&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;2. C++&lt;/p&gt;
&lt;pre id=&quot;code_1719904762842&quot; class=&quot;cpp&quot; data-ke-language=&quot;cpp&quot; data-ke-type=&quot;codeblock&quot;&gt;&lt;code&gt;class Solution {
public:
    int minimumSwaps(vector&amp;lt;int&amp;gt;&amp;amp; nums) {
        if (nums.size() &amp;lt;= 1) return 0;
		int minVal = *min_element(nums.begin(), nums.end());
		int maxVal = *max_element(nums.begin(), nums.end());
		int minIdx, maxIdx;
		for (int i=0; i&amp;lt;nums.size(); i++) {
			if (nums[i] == minVal) {
				minIdx = i;
				break;
			}
		}
		for (int i=nums.size()-1; i&amp;gt;=0; i--) {
			if (nums[i] == maxVal) {
				maxIdx = i;
				break;
			}
		}
		if (minIdx == maxIdx) return 0;
		else if (minIdx &amp;lt; maxIdx) return minIdx + nums.size() - maxIdx - 1;
		else return minIdx + nums.size() - maxIdx - 2;
    }
};&lt;/code&gt;&lt;/pre&gt;</description>
      <category>Computer Science/Algorithms &amp;amp; Data Structures</category>
      <author>focalpoint</author>
      <guid isPermaLink="true">https://focalpoint.tistory.com/418</guid>
      <comments>https://focalpoint.tistory.com/418#entry418comment</comments>
      <pubDate>Tue, 2 Jul 2024 16:19:26 +0900</pubDate>
    </item>
  </channel>
</rss>