Skip to content

Commit

Permalink
Modify merge intervals
Browse files Browse the repository at this point in the history
  • Loading branch information
sangaryousmane committed Jan 8, 2024
1 parent 0a4727d commit 10109f4
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 26 deletions.
Binary file modified out/production/java-interview-questions/Main.class
Binary file not shown.
2 changes: 1 addition & 1 deletion src/advance/TicTacToe.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static void main(String[] args) {
}

char player = 'X'; // a player
boolean isGameOver = false; // the haven't start yet
boolean isGameOver = false; // the game haven't start yet

while (!isGameOver) {
printBoard(board);
Expand Down
43 changes: 40 additions & 3 deletions src/advance/WorkingWithStrings.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ else if (stack.isEmpty() || stack.pop() != chr)
// https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/description/
public int minAddToMakeValid(String s) {
Stack<Character> stack = new Stack<>();
for (char chr: s.toCharArray()){
for (char chr : s.toCharArray()) {
if (chr == ')')
if(!stack.isEmpty() && stack.peek() == '(')
stack.pop();
if (!stack.isEmpty() && stack.peek() == '(')
stack.pop(); // Remove from the stack because there is a match
else
stack.push(chr);
else
Expand Down Expand Up @@ -204,6 +204,43 @@ public int accountBalanceAfterPurchase(int purchaseAmount) {
return 100 - purchaseAmount;
}

static int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> seen = new HashMap<>();

for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];

// Check if the complement is in the map
if (seen.containsKey(complement)) {
return new int[]{seen.get(complement), i};
}

// Add the current number and its index to the map
seen.put(nums[i], i);
}
return new int[]{-1, -1};
}

// https://leetcode.com/problems/merge-intervals/
public int[][] merge(int[][] intervals) {
// First sort the array - O(nlogn)
Arrays.sort(intervals, Comparator.comparingInt(num -> num[0]));
List<int[]> merged = new ArrayList<>();


for (int[] interval : intervals) {
int start = interval[0], end = interval[1];

if (merged.isEmpty() || start > merged.get(merged.size() - 1)[1]) {
merged.add(new int[]{start, end});
} else {
int[] lastInterval = merged.get(merged.size() - 1);
lastInterval[1] = Math.max(lastInterval[1], end);
}
}
return merged.toArray(new int[0][]);
}

// https://leetcode.com/problems/integer-to-roman/description/
public String intToRoman(int num) {
String[] ones = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
Expand Down
4 changes: 3 additions & 1 deletion src/advance/annotation/Getter.java
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
package advance.annotation;public class Getter {
package advance.annotation;

public class Getter {
}
2 changes: 1 addition & 1 deletion src/games/snake/GameFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class GameFrame extends JFrame {

public GameFrame() throws HeadlessException {
this.add(new GamePanel());
this.setTitle("SNAKE");
this.setTitle("SNAKE GAME");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.pack();
Expand Down
165 changes: 146 additions & 19 deletions src/games/snake/GamePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import javax.swing.*;

import java.awt.Graphics;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;

public class GamePanel extends JPanel implements ActionListener {

Expand All @@ -15,51 +16,177 @@ public class GamePanel extends JPanel implements ActionListener {
private static final int UNIT_SIZE = 25;
private static final int GAME_UNITS = (SCREEN_WIDTH * SCREEN_HEIGHT) / UNIT_SIZE;
private static final int DELAY = 75;

public GamePanel() {}
private static final int[] y = new int[GAME_UNITS];
private static final int[] x = new int[GAME_UNITS];
int bodyParts = 6;
int applesEaten;
int appleX;
int appleY;
char direction = 'R';
boolean isRunning = false;
Timer timer;
Random random;

public GamePanel() {
random = new Random();
this.setPreferredSize(new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT));
this.setBackground(Color.black);
this.setFocusable(true);
this.addKeyListener(new MyKeyAdapter());
startGame();
}

// Starts game
public void startGame(){

public void startGame() {
newApple();
isRunning = true;
timer = new Timer(DELAY, this);
timer.start();
}

// paint the component
public void paintComponent(Graphics graphics){

public void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
draw(graphics);
}

// Draw on the screen
public void draw(Graphics graphics){
// Draw snake on the screen
public void draw(Graphics graphics) {
if (isRunning) {
// for (int i = 0; i < SCREEN_HEIGHT / UNIT_SIZE; i++) {
// graphics.drawLine(i * UNIT_SIZE, 0, i * UNIT_SIZE, SCREEN_HEIGHT);
// graphics.drawLine(0, i * UNIT_SIZE, SCREEN_WIDTH, i * UNIT_SIZE);
// }
graphics.setColor(Color.red);
graphics.fillOval(appleX, appleY, UNIT_SIZE, UNIT_SIZE);

// Iterate through the body parts of the snake
for (int i = 0; i < bodyParts; i++) {
if (i == 0) {
graphics.setColor(Color.GREEN);
graphics.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
} else {
//graphics.setColor(new Color(45, 180, 0));
graphics.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
graphics.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
}
}
graphics.setColor(Color.red);
graphics.setFont( new Font("Ink Free",Font.BOLD, 40));
FontMetrics metrics = getFontMetrics(graphics.getFont());
graphics.drawString("Score: "+applesEaten,
(SCREEN_WIDTH - metrics.stringWidth("Score: "+applesEaten))/2, graphics.getFont().getSize());
} else {
gameOver(graphics);
}
}

public void newApple() {
appleX = random.nextInt(SCREEN_WIDTH / UNIT_SIZE) * UNIT_SIZE;
appleY = random.nextInt(SCREEN_HEIGHT / UNIT_SIZE) * UNIT_SIZE;
}

// Move the snake
public void move(){

public void move() {
for (int i = bodyParts; i > 0; i--) {
x[i] = x[i - 1];
y[i] = y[i - 1];
}
switch (direction) {
case 'U' -> y[0] = y[0] - UNIT_SIZE;
case 'D' -> y[0] = y[0] + UNIT_SIZE;
case 'L' -> x[0] = x[0] - UNIT_SIZE;
case 'R' -> x[0] = x[0] + UNIT_SIZE;
}
}

// Eat the apple
public void checkApple(){
public void checkApple() {
if (x[0] == appleX && y[0] == appleY) {
bodyParts++;
applesEaten++;
newApple();
}

}

// Check for collisions
public void checkCollisions(){
public void checkCollisions() {
for (int i = bodyParts; i > 0; i--) {
if ((x[0] == x[i]) && (y[0] == y[i])) {
isRunning = false;
break;
}
}
// Check if head touches left border
if (x[0] < 0) {
isRunning = false;
}

// If head touches right
if (x[0] > SCREEN_WIDTH) {
isRunning = false;
}

// If head touches top border
if (y[0] < 0)
isRunning = false;
if (y[0] > SCREEN_HEIGHT) {
isRunning = false;
}
if (!isRunning)
timer.stop();
}
// Game over
public void gameOver(Graphics graphics){

// Game over
public void gameOver(Graphics graphics) {
// Score at the game over screen#
graphics.setColor(Color.red);
graphics.setFont( new Font("Ink Free",Font.BOLD, 40));
FontMetrics metrics1 = getFontMetrics(graphics.getFont());
graphics.drawString("Score: "+applesEaten,
(SCREEN_WIDTH - metrics1.stringWidth("Score: "+applesEaten))/2, graphics.getFont().getSize());

// Set game over text
String gameOverMsg = "Game Over";
graphics.setColor(Color.RED);
graphics.setFont(new Font("Ink Free", Font.BOLD, 75));
FontMetrics metrics=getFontMetrics(graphics.getFont());
graphics.drawString(gameOverMsg,
(SCREEN_WIDTH - metrics.stringWidth(gameOverMsg))/2, SCREEN_HEIGHT / 2);
}

@Override
public void actionPerformed(ActionEvent e) {

if (isRunning) {
move();
checkApple();
checkCollisions();
}
repaint();
}

public class MyKeyAdapter extends KeyAdapter{

public class MyKeyAdapter extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
if (direction != 'R')
direction = 'L';
break;
case KeyEvent.VK_RIGHT:
if (direction != 'L')
direction = 'R';
break;
case KeyEvent.VK_DOWN:
if (direction != 'U')
direction = 'D';
break;
case KeyEvent.VK_UP:
if (direction != 'D')
direction = 'U';
break;
}
}
}
}
2 changes: 1 addition & 1 deletion src/system_design/impl/BlackJackDealer.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public BlackJackDealer(String name) {


public void dealCard(Player player){
player.receive(cards.remove(0));
player.receive(cards.remove(suit));

}

Expand Down

0 comments on commit 10109f4

Please sign in to comment.