This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Deque; | |
import java.util.LinkedList; | |
public class SlidingWindowRateLimiter { | |
private final int maxRequests; | |
private final long windowSizeInMillis; | |
private final Deque<Long> requestTimestamps; | |
public SlidingWindowRateLimiter(int maxRequests, long windowSizeInMillis) { | |
this.maxRequests = maxRequests; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from diagrams import Diagram | |
from diagrams.aws.compute import EC2 | |
from diagrams.aws.database import RDS | |
from diagrams.aws.network import ELB | |
with Diagram("Grouped Workers", show=False, direction="TB"): | |
ELB("lb") >> [EC2("worker1"), | |
EC2("worker2"), | |
EC2("worker3"), | |
EC2("worker4"), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun checkStraightLine(coordinates: Array<IntArray>): Boolean { | |
if (coordinates[0][1] == coordinates[1][1]) { | |
for (i in 2 until coordinates.size) { | |
if (coordinates[i][1] != coordinates[0][1]) { | |
return false | |
} | |
} | |
return true | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TreeNode(var `val`: Int) { | |
var left: TreeNode? = null | |
var right: TreeNode? = null | |
} | |
var xHeight = -1 | |
var yHeight = -1 | |
var xParentNode : TreeNode? = null | |
var yParentNode : TreeNode? = null | |
fun isCousins(root: TreeNode?, x: Int, y: Int): Boolean { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun rotate(nums: IntArray, k: Int): Unit { | |
var pivot = k % nums.size | |
reverse(nums, 0, nums.size - 1) | |
reverse(nums,0,pivot-1) | |
reverse(nums,pivot,nums.size-1) | |
} | |
private fun reverse(nums: IntArray, start: Int, end: Int) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun containsDuplicate(nums: IntArray): Boolean { | |
val set = HashSet<Int>() | |
nums.forEach{it -> | |
if(set.contains(it)){ | |
return true | |
} | |
else{ | |
set.add(it) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun middleNode(head: ListNode?): ListNode? { | |
var slowPointer = head | |
var fastPointer = head?.next | |
while (fastPointer != null){ | |
slowPointer = slowPointer?.next | |
fastPointer = fastPointer?.next?.next | |
} | |
return slowPointer | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class SinglyLinkedList { | |
public SinglyLinkedListNode head; | |
public SinglyLinkedListNode tail; | |
public SinglyLinkedList() { | |
this.head = null; | |
this.tail = null; | |
} | |
public void insertNode(int nodeData) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun countingValleys(n: Int, s: String): Int { | |
var count = 0 | |
var numberOfValley = 0 | |
s.forEach { | |
if (it == 'U') { | |
count += 1 | |
if (count == 0) { | |
numberOfValley += 1 | |
} | |
} else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun firstUniqChar(s: String): Int { | |
if (s.isEmpty()) { | |
return -1 | |
} | |
val arr = IntArray(26) { 0 } | |
for (element in s) { | |
arr[element - 'a']++ |
NewerOlder