Aleksiej's Blog

Blog

Welcome to my blog about programming, machine learning, philosophy, and sociology.
Tag cloud  All words

Print FooBar Alternately

                       
                
                
/*

#----------------------------------------------------------------------#
#                                                                      #
#  version 0.0.1                                                       #
#  https://leetcode.com/problems/print-foobar-alternately/submissions/ # 
#                                                                      #
#  Aleksiej Ostrowski, 2020                                            #
#                                                                      #
#  https://aleksiej.com                                                #
#                                                                      #
#----------------------------------------------------------------------#  

*/


#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>

std::mutex m;
std::condition_variable cv;

int flag = 1;

void task1(const int n)
{
    int i = 1;

    for(;;) {

        std::unique_lock<std::mutex> lk(m);

        if (flag == 1) { 
           std::cout << "foo";
           i++;
           flag = 2;
           lk.unlock();
           cv.notify_all();
        } else {
           cv.wait(lk, []{return flag == 1;});
        }   

        if (i > n) return;
   }
}

void task2(const int n)
{
    int i = 1;

    for(;;) {

        std::unique_lock<std::mutex> lk(m);

        if (flag == 2) { 
           std::cout << "boo";
           i++;
           flag = 1;
           lk.unlock();
           cv.notify_all();
        } else {
           cv.wait(lk, []{return flag == 2;});
        }   

        if (i > n) return;
   }
}

int main()
{
    flag = 1;

    std::thread t1(task1, 3);
    std::thread t2(task2, 3);

    t1.join();
    t2.join();
}

                
                
                

Longest Substring Without Repeating Characters

                       
                
                
/*
#----------------------------------------------------------------------------------------------#
#                                                                                              #
#  version 0.0.1                                                                               #
#  https://leetcode.com/problems/longest-substring-without-repeating-characters/submissions/   #
#                                                                                              #
#  Aleksiej Ostrowski, 2020                                                                    #
#                                                                                              #
#  https://aleksiej.com                                                                        #
#                                                                                              #
#----------------------------------------------------------------------------------------------#  
*/

#include <string>
#include <iostream>


int max(const int a, const int b) {
    if (a > b) return a; else return b;
}

class Solution {
public:
    int lengthOfLongestSubstring(std::string s) {

        auto len_s = s.length();

        if (len_s <= 1) return len_s;

        std::string t;

        int max_len = -1;

        int i = 0;
        
        for (;;) {
            
           auto found = t.find(s[i]);

           if (found != -1) t = t.substr(found + 1);

           t += s[i];

           max_len = max(max_len, t.length());

           std::cout << "i = " << i <<  " s[" << i << "] = " << s[i] << " max_len = " << max_len << " string = " << t << std::endl;
          
           if (++i >= len_s) break;
        }        
        
        return max_len;
    
    }
};

int main() {

    // std::string s = "aabac"; // 3
    // std::string s = "pwwkew"; // 3
    // std::string s = "bbbbb"; // 1
    // std::string s = " "; // 1
    std::string s = "aab"; // 2

    std::cout << "input " << s << std::endl;
    auto S = Solution();
    std::cout << S.lengthOfLongestSubstring(s) << std::endl;  

    return 0;
}