首页 > 其他分享 >LeetCode 1114. Print in Order

LeetCode 1114. Print in Order

时间:2022-10-14 13:22:10浏览次数:69  
标签:third thread Order second Semaphore Print LeetCode public first

原题链接在这里:https://leetcode.com/problems/print-in-order/

题目:

Suppose we have a class:

public class Foo {
  public void first() { print("first"); }
  public void second() { print("second"); }
  public void third() { print("third"); }
}

The same instance of Foo will be passed to three different threads. Thread A will call first(), thread B will call second(), and thread C will call third(). Design a mechanism and modify the program to ensure that second() is executed after first(), and third() is executed after second().

Note:

We do not know how the threads will be scheduled in the operating system, even though the numbers in the input seem to imply the ordering. The input format you see is mainly to ensure our tests' comprehensiveness. 

Example 1:

Input: nums = [1,2,3]
Output: "firstsecondthird"
Explanation: There are three threads being fired asynchronously. The input [1,2,3] means thread A calls first(), thread B calls second(), and thread C calls third(). "firstsecondthird" is the correct output.

Example 2:

Input: nums = [1,3,2]
Output: "firstsecondthird"
Explanation: The input [1,3,2] means thread A calls first(), thread B calls third(), and thread C calls second(). "firstsecondthird" is the correct output.

Constraints:

  • nums is a permutation of [1, 2, 3].

题解:

Have semaphore to make sure order is called based on order.

Time Complexity: O(1).

Space: O(1).

AC Java:

 1 class Foo {
 2     Semaphore run1;
 3     Semaphore run2;
 4     Semaphore run3;
 5     public Foo() {
 6         run1 = new Semaphore(1);
 7         run2 = new Semaphore(0);
 8         run3 = new Semaphore(0);
 9     }
10 
11     public void first(Runnable printFirst) throws InterruptedException {
12         run1.acquire();
13         // printFirst.run() outputs "first". Do not change or remove this line.
14         printFirst.run();
15         run2.release();
16     }
17 
18     public void second(Runnable printSecond) throws InterruptedException {
19         run2.acquire();
20         // printSecond.run() outputs "second". Do not change or remove this line.
21         printSecond.run();
22         run3.release();
23     }
24 
25     public void third(Runnable printThird) throws InterruptedException {
26         run3.acquire();
27         // printThird.run() outputs "third". Do not change or remove this line.
28         printThird.run();
29         run1.release();
30     }
31 }

 

标签:third,thread,Order,second,Semaphore,Print,LeetCode,public,first
From: https://www.cnblogs.com/Dylan-Java-NYC/p/16791308.html

相关文章

  • LeetCode 1115. Print FooBar Alternately
    原题链接在这里:https://leetcode.com/problems/print-foobar-alternately/题目:Supposeyouaregiventhefollowingcode:classFooBar{publicvoidfoo(){f......
  • leetcode每日一题:940.不同的子序列Ⅱ
    题目描述给定一个字符串s,计算s的不同非空子序列的个数。因为结果可能很大,所以返回答案需要对10^9+7取余。字符串的子序列是经由原字符串删除一些(也可能不删除)字......
  • leetcode必备算法:聊聊滑动窗口
    前言我们刷leetcode的时候,经常会遇到滑动窗口类型题目。滑动窗口问题非常经典,也很有技巧性,一般大厂也喜欢问。今天跟大家一起来学习滑动窗口的套路,文章如果有不正确的地方,......
  • LeetCode 1116. Print Zero Even Odd
    原题链接在这里:https://leetcode.com/problems/print-zero-even-odd/题目:Youhaveafunction printNumber thatcanbecalledwithanintegerparameterandprints......
  • leetcode-62. 不同路径 初级dp
    62.不同路径首先,机器人每次走路只能向下或者向右走一步根据网格是m*n,初始化动态规划数组,dp[m][n],那么如果机器人走到i,j位置,有多少种情况呢?首先分成子问题,机器人怎么走......
  • unordered_map
    Ⅰ.unordered_map从C++11开始,标准库又引入了一类容器,即无序关联式容器。无序关联式容器,又称哈希容器(unordered_map容器),直译过来就是“无序map容器”的意思。所谓“......
  • 【算法训练营day2】LeetCode977. 有序数组的平方 209. 长度最小的子数组 59. 螺旋矩阵
    【算法训练营day2】LeetCode977.有序数组的平方209.长度最小的子数组59.螺旋矩阵IILeetCode977.有序数组的平方题目链接:977.有序数组的平方初次尝试上来看到建......
  • 刷题 LeetCode 977 209 59
    代码随想录LeetCode977 有序数组的平方carl数组#双指针思路利用有序条件,新的大值在旧数组的两端产生,因此考虑相向指针细节涉及3个指针,注意每个指针的更新时机......
  • leetcode-88-easy
    MergeSortedArray思路一:比较两个数组前面最小值,依次插入到新数组中,最后复制新数组到num1中publicvoidmerge(int[]nums1,intm,int[]nums2,intn){int......
  • leetcode-83-easy
    RemoveDuplicatesfromSortedList思路一:双指针,左指针记录链表最后有效位置,右指针向前扫描,遇到不重复的值,加到左指针后面,双指针依次向前publicListNodedeleteDupl......