# Tag Archives: update

### Question

Define a stack structure with “min” function — a function to get the minimum value within the stack. The time complexity of min, push and pop functions must be O(1).

### Solution

```0: 10 -> NULL (MIN=10, POS=0)
1: 7  -> [0]  (MIN=7,  POS=1) // 用數組表示堆棧，第0個元素表示棧底
2: 3  -> [1]  (MIN=3,  POS=2)
3: 3  -> [2]  (MIN=3,  POS=3)
4: 8  -> NULL (MIN=3,  POS=3) // 技巧在這裡，因為8比當前的MIN大，所以彈出8不會影響當前的MIN
5: 5  -> NULL (MIN=3,  POS=3)
6: 2  -> [2]  (MIN=2,  POS=6) // 如果2出棧了，那麼3就是MIN
7: 6  -> [6]```

### Samples

The following is an optimized implementation.  Repeated minimum value will not be stored through comparing values while pushing or popping values.

```#include <vector>
#include <cassert>

using namespace std;

/**
* Class defining a new type of stack supporting any datatype.
*/
template <typename T>
class StackWithMin
{
private:
vector<T> dataStack; // stacks to store data
vector<size_t> minStack; // stack to store position of minimum value

public:

/**
* Push data to the stack at the same time update the minimum stack if the
* new data is smaller then the current minimum value. Throw exception if
*  stack is empty.
*/
void push(T data) {
dataStack.push_back(data);
if (minStack.empty() || data < dataStack[minStack.back()])
minStack.push_back(dataStack.size()-1);
}

/**
* Pop data to the stack at the same time pop from the min stack if it is
* popping the minimum value. Throw exception if stack is empty.
*/
void pop() {
assert(!dataStack.empty());
if (dataStack.back() == dataStack[minStack.back()])
minStack.pop_back();
dataStack.pop_back();
}

/**
* return the current minimum value.
*/
T min() {
assert(!dataStack.empty() && !minStack.empty());
return dataStack[minStack.back()];
}
};

/**
* Main program
*/
int main()
{
StackWithMin<int> stack = StackWithMin<int>();
stack.push(10);
stack.push(7);
stack.push(3);
stack.push(3);
stack.push(8);
stack.push(5);
stack.push(2);
stack.push(6);
printf("Minimum value: %dn", stack.min());
}```
```import java.util.Stack;

public class Pactice02 {

public static void main(String[] args) {
System.out.println("Hello World!");
stack.push(10);
stack.push(7);
stack.push(3);
stack.push(3);
stack.push(8);
stack.push(5);
stack.push(2);
stack.push(6);
System.out.println("Minimum value: "+ stack.getMinimum());
stack.pop();
System.out.println("Minimum value: "+ stack.getMinimum());
stack.pop();
System.out.println("Minimum value: "+ stack.getMinimum());
stack.pop();
System.out.println("Minimum value: "+ stack.getMinimum());
}

public static class AdvancedStack extends Stack {

private Stack mMinimumStack = new Stack();

@Override
public E push(E item) {
if (mMinimumStack.empty() || item.doubleValue()  0) {
return mMinimumStack.peek();
}
return null;
}
}
}
```

##### What’s happening

One day, when I opened up terminal as usual, it showed [Process completed] and just terminated. I could not type any thing, run any scripts and work on my project. Even worse, this made me unable to install programs into my computer because many installations need to run shell scripts. Okay, I searched over the internet and there was no solution for that. I even peaked many parts in the Mac OSX system to see if there were any mis-configuration, of course nothing helps. After all, I though if there is nothing wrong, it must be  something done with my /bin/bash. And after I upgraded my bash, my lovely terminal came back!

[expand title=”Let’s fix it” tag=”h5″ trigclass=”arrowright”]

##### Change shell’s default execution

The truth is, when you open terminal, it execute /bin/bash. As it is not working now, we need another environment for us to execute stuff. Open “Terminal->Preference”, and change “Shells open with” manually to “/bin/sh”.

##### Update /bin/bash

Now we are going to download the latest version of bash and replace the old one. Open up a new terminal and now you are able to execute commands. Copy and paste the following codes into the terminal, they will automatically handle everything for you.

```	curl -LO ftp://ftp.cwru.edu/pub/bash/bash-4.2.tar.gz
tar zxvf bash-4.2.tar.gz
cd bash-4.2
./configure &amp;&amp; make &amp;&amp; sudo make install
chsh -s /usr/local/bin/bash {user_name}
sudo bash -c &quot;echo /usr/local/bin/bash &gt;&gt; /private/etc/shells&quot;
cd /bin
sudo mv bash bash-old
sudo ln -s /usr/local/bin/bash bash
```
##### Once done

Go to “Terminal->Preference” again and change :Shells open with: back to “Default login shell”. Enjoy!

[/expand]

[expand title=”Detailed explanations” tag=”h5″ trigclass=”arrowright”]

##### What I did?

Okay, I admit that I did something to the system sometimes ago. I was doing some experiments on “sandboxing” and played with “chroot jail” stuff before. That is, I need to create an environment with restricted support to the program I run. So I wrote a sandbox, configured it’s root to a ‘secure’ place (anywhere not the actual root), and copy essential executables to that new root. Well, so far I think I didn’t do anything harm to the system, but I might corrupt the /bin/bash when I copied it to the sandbox root.

##### Diagnosis

Shell will run the following files before letting user to do anything. Check everyone to see if there are any misconfigurations.

• ~/.bash_profile (for /bin/bash)
• ~/.profile (for /bin/sh)
• /etc/profile (for /bin/sh)
• /etc/bashrc (for /bin/bash)
• (google for more)

To play with, add some echoes to see if they works. Btw, I love nano more than vi, so try out “sudo nano /etc/bashrc”.

##### Another thing

The code above updates your bash to version 4.2. To check if there is any new version, go to the ftp.
ftp://ftp.cwru.edu/pub/bash/

##### Reference

http://techscienceinterest.blogspot.com/2010/05/change-to-new-bash-shell-41-for-mac-os.html

[/expand]

Hi there, its been a long time that InstaMusic haven’t been updated. Sorry for that because I had been so busy in working on a game project in my company. Anyway, I am quitting for the sake of pursuing a master degree. And finally I have time to implement what I thought up and designed!

An new application “InstaMusic Beta Channel” will show up in Android Play Store soon. This application aims to provide a cutting-edge version of InstaMusic. This app could be unstable, lots of force closes, however this helps me kill bugs in the program. For those who only wants a stable music playback, please download the original “InstaMusic”.

As a new version is coming out, I have created a feature list presenting the main improvements done or working on.

1. Stability
InstaMusic has been unstable for a long time. Music stops suddenly without telling. After a deep diagnosis, the core reason leading this bug is found, and a fix is patched! Music never stops now!
Media loading speed has been a big concern for me while generating the music list. Long loading time affects user experience and might hinder other operations. Anyway, I have designed a new architecture specially for the list generation. This assured an optimised, low memory usage, synchronised and dynamic list generation.
3. Jelly Bean Support (Working in Progress)
There are lots of new features and supports from platforms beginning from Android 4.0 such as notification controls and new theme styles. I am working on making use of those new features and to create a whole new user experience for users!
4. Editable Playlists integrating with Favourite List (Working in Progress)
In the next version of InstaMusic, Favorite List will be integrated into Playlist. The reason is… actually there is no reason to separate them lol.

Thank you for your supporting so far. Please comment below and tell me what you think on InstaMusic!

Happy lunar new year to all.

I am flattered. In less than a month since I put the InstaMusic to the Android Market on 27 Dec 2011, today it has already more than 10,000 downloads. To be honest, I never expected this.

If you are uninitialized, InstaMusic is a music player android application with simple and stylish outlook.

There are all kinda music player in the world, in my opinion, a player with style and an pleasant appearance would always become the famous one. Here I would like to thank you for downloading and trying InstaMusic. Especially those fans of minimalism who supported me and AndroidStatic who gave InstaMusic a pleasant review in the post “Instamusic Is A Minimal, Slick Music Player That You Should Download Now“.

Looking back the day that I began my developmement, the reason for me to kick started is that I was really unstatisfied with the music players on the Android Market. Admittedly, I am a perfectionist and I want every part of a music player to meet user’s ways of thinking. So, I hate album artworks to be scratched and unable to tell what album is it. I also hate a song sung by different artists will be will be isolated from the album. And millions of things to list.

Okay, let’s get back to talk about the my next build of InstaMusic. I am sorry for the long waited update. In code level, I really did an upside down on InstaMusic. And as expected, lock screen player is implemented already, but there are just some bug and pressure testing work to do before it can really come out to meet its users.

Caffeine has been a very useful app to instantly disable or enable the screensaver on mac. At least for me, I am using multiple computers in office and all of them except the one I am using will usually get idle. Getting into screensaver, however, is bad sometimes especially I was reading from that screen and type things on another screen, and not to mention the password input after the screen lock. Well anyway, I highly recommend it.

# Mac

On mac, we can install it easily with the app store.

# Ubuntu

But in ubuntu, it is not available on the software center. But you can still get it through command line. Turn on terminal and input the following one by one.

```sudo add-apt-repository ppa:caffeine-developers/ppa
sudo apt-get update
sudo apt-get install caffeine
```